Disable editing of a text field - ios

I want to start by saying this is my first project and I am trying hard to find answers myself before posting here. I thought I had found the code to accomplish this. I have no errors but when I run, the field is still editable. So, how can I disable editing of my field when the rule I have set is true?
if sport.count == 1 {
enterSport.text = sport[0] as String //need to convert to a string
enterSport.editing; false //do not allow editing
} else {
//do nothing
}
I have defined the array previously so the if statement is true. Thank you for you assistance.

enterSport.userInteractionEnabled = false instead of editing.
editing is a read-only property to indicate if the field is currently being edited or not.
Swift 5:
enterSport.isUserInteractionEnabled = false

To summarize the answers and comments above:
editing is a readonly property. You can't set it.
If it's a UITextView, it has an editable property which you can set to false.
If it's a UITextField, you need to use the enabled property or the userInteractionEnabled property. Either of those will prevent editing. As

With Swift 3 Apple changed the property userInteractionEnabled to isUserInteractionEnabled.
The code looks like this:
textfield.isUserInteractionEnabled = true

There is one more way of avoiding keyboard appearing
You can set an UITapGestureRecognizer like this:
let tap = UITapGestureRecognizer(target: self, action: #selector(self.textFieldEditing))
self.enterSport.addGestureRecognizer(tap)
and
#objc func textFieldEditing() {
self.enterSport.resignFirstResponder()
}

Related

set isTouchInside to false

I need to check if UITextField has received some kind of focus such as isTouchInside then after the code in the if statement is executed i want to set the isTouchInside to false. Is this possible? Or something else should be used here?
I want to add 3 different values to the array when the textfield is selected by user. When i do it like below and touch the txt1 and after that txt2. txt 1 is also added to the array and i don't want that to happen
if self.txt1.isTouchInside{ //this sets isTouchInside to true
print("txt1 touched")
self.array.append(value1)
//how to set isTouchInside to false here?
}else if self.txt2.isTouchInside{
print("txt2 touched")
self.array.append(value2)
//how to set isTouchInside to false here?
}else if self.txt3.isTouchInside{
print("txt3 touched")
self.array.append(value3)
//how to set isTouchInside to false here?
}
That is a read-only property. You can't change the value of isTouchInside. Full stop. not possible.
Explain what you are trying to accomplish at a higher level and perhaps we can help you.

disable textfield editing without blocking the clear button

do you know how I can make an edittext get non-editable for the user, but allow it to use the textfield's own clearbutton to delete it?
I tried using the:
textField.isEnabled = false
and
textField.isUserInteractionEnabled = false
but it did not work very well because it disables everything
Return false from the text field delegate’s textFieldShouldBeginEditing.

How to unclear UITextField secure text entry in Swift?

When I use default Security text Entry in UITextField in Swift Language after type type text once UITextField.
Once loss focus from UITextField after try to edit Secure text then UITextField is first reset and after it start put new text in UITextField.
How to edit old Secure Text without Storing data into Any Kind of String object
I'd suggest to create a custom UITextField class and override become​First​Responder() method do add your desired functionality:
You can override this method in your custom responders to update your
object's state or perform some action such as highlighting the
selection. If you override this method, you must call super at some
point in your implementation.
The custom Class should be similar to:
class CustomSecureTextField: UITextField {
override func becomeFirstResponder() -> Bool {
super.becomeFirstResponder()
if !isSecureTextEntry { return true }
if let currentText = text { insertText(currentText) }
return true
}
}
The logic of the implementation of becomeFirstResponder as follows:
By default, the secured-entry text field clears the inserted text when it is become first responder text, so what's happening in CustomSecureTextField that if the text field is secured-entry, it will re-insert the current inserted text -after clearing it-, but you have to make sure that the text field input is secured (that's the purpose of adding if !isSecureTextEntry { return true }) or the text will be duplicated (re-inserted) each time the text field becomes first responder.
Output:
Note that both of text fields are types of CustomSecureTextField:
This answer helped me to figure out this problem.
textField.isSecureTextEntry = true
following property not gonna work if you make testField isSecureTextEntrysecure property makes true .
textField.clearsOnBeginEditing = false
There is an issue with the #Ahmd F solution when you simply tap on the field it will automatically add the text to the field I have resolved that in the below code thanks
override open func becomeFirstResponder() -> Bool {
super.becomeFirstResponder()
if !isSecureTextEntry { return true}
if let currrentText = text {
self.text = ""
insertText(currrentText)
}
return true
}

How to make UITextView selectable in Swift

I have this UITextView in Swift:
let contactText = UITextView(frame: CGRectMake(0,0,200,50))
contactText.selectable = true
contactText.dataDetectorTypes = UIDataDetectorTypes.Link
contactText.userInteractionEnabled = true
contactText.editable = false
contactText.text = "Some text goes here and some website here www.google.com"
self.view.addSubview(contactText)
I want to be able to double tap or tap and hold to select the text and then give the user the option to select all or copy (just like most apps do). The above code didn't work although it seems like it should, is there another way of doing it?
EDIT:
The above code actually works, the reason why is not working (not 100% sure yet) is probably because the UITextView is within a UIView that has a PanGestureRecognizer attached to it, so maybe that is blocking it? any ideas?
To make a UITextView selectable you only need to add these two lines:
yourTextView.selectable = true
yourTextView.userInteractionEnabled = true

iPhone/iPad: How to make UITextField readonly (but not disabled)?

I understand this question has been asked before, but I'm not satisfied with the answers, i.e. by making it disabled. There is a fundamental difference: Disabled view doesn't fire events, but for a read-only view, it should still fire event like (e.g. TouchUpInside), and I need it. Only thing I don't want is the keyboard input.
The reason is that I have several input fields, some can useUITextField directly, others are not. I want to have them look similar. So, I'd like to use UITextField to display all of them. Some of them need to be read-only so that I can use touch up event for alternative input.
Or there might be a completely different way to do it?
EDIT: This is for my MonoTouch project. I have very limited knowledge of Objective-c.
Say you have 2 text field instance variables connected to text fields you created in the Interface Builder. Lets call them myReadOnlyTextField and myEditableTextField. Make sure you connect the delegate property of each text field in the Interface Builder to the view controller ("File's Owner")[1]. Now, in the view controller #implementation (.m file), use the method textFieldShouldBeginEditing: and put in some logic to determine which text field you want to allow editing and which to not allow editing; something like this:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
BOOL editable;
if (textField == myReadOnlyTextField) {
editable = NO;
} else if (textField == myEditableTextField) {
editable = YES;
} else {
// editable = YES/NO/Other Logic
}
return editable;
}
From the UITextFieldDelegate Documentation:
textFieldShouldBeginEditing:
Asks the delegate if editing should begin
in the specified text field.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
Parameters
textField - The text field for which editing is about to
begin.
Return Value
YES if an editing session should be initiated;
otherwise, NO to disallow editing.
Discussion
When the user performs an action that would normally
initiate an editing session, the text field calls this method first to
see if editing should actually proceed. In most circumstances, you
would simply return YES from this method to allow editing to proceed.
Implementation of this method by the delegate is optional. If it is
not present, editing proceeds as if this method had returned YES.
UITextField Documentation is a good read also.
[1] You can do this programmatically as well. Here is an example:
- (void)viewDidLoad {
[super viewDidLoad];
// .....
myReadOnlyTextField.delegate = self;
myEditableTextField.delegate = self;
}
Despite the fact you need no keyboard, textField:shouldChangeCharactersInRange:replacementString: is very useful. It prevents text field from editing but still leaves it selectable in contrast to textFieldShouldBeginEditing:.
In monotouch:
var txt = new UITextField();
txt.ShouldChangeCharacters += (field, range, replacementString) => false;
You can have two options:
a) to use ShouldBeginEditing with return false, but you can't use PickerView as InputView in your text field.
b) to use ShouldChangeCharacters that will prevent the editing but will allow to use InputView with PickerView.
Objective C:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return NO;
}
Swift :
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return false
}
because you are working in MonoTouch you could use the following line of code:
myReadOnlyButton.ShouldBeginEditing = t =>
{
//event code
return false;
};
I personally use borderStyle = .none + userInteractionEnabled = false to make it look like a regular label.

Resources