disable textfield editing without blocking the clear button - ios

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.

Related

How do I use IQKeyboard in such a way that it doesn't hide the validation texts too?

I am using IQKeyboard (https://github.com/hackiftekhar/IQKeyboardManager) to avoid keyboard hiding the textfields while writing on them. However they hide validation texts below it (which is obvious). How do I avoid the same too?
I am using the following code in AppDelegate on an order to use the IQKeyboard:
IQKeyboardManager.sharedManager().enable = true
IQKeyboardManager.sharedManager().enableAutoToolbar = false
IQKeyboardManager.sharedManager().shouldShowToolbarPlaceholder = false
IQKeyboardManager.sharedManager().previousNextDisplayMode = IQPreviousNextDisplayMode.alwaysHide
Use keyboardDistanceFromTextField to add distance between keyboard and textfield.
Example:
IQKeyboardManager.sharedManager().keyboardDistanceFromTextField = 40

How to enable a button when all textfield is filled

Initially button.isHidden = true, however, I would like to set the button.isHidden = false, when all textfields are filled. Where should I put the If-Else condition in, most probably not in viewDidLoad, please advise.
You might want to take a look at this post UITextField text change event
and then in textFieldDidChange function you should check texts of each textfield

UITextView autocapitalization is not working when keyboard is active

Currently, I am setting autocapitalization in a button target like this
//This method is fired when keyboard is still active
- (IBAction)changeAutoCapitalization:(id)sender
{
self.textView.autocapitalizationType = UITextAutocapitalizationTypeWords;
}
But this won't make keyboard to capital.
You have to call [self.textView reloadInputViews] after modifying the UITextAutocapitalizationType in order to change keyboard immediately.
If you find that you're doing a lot of text-related customizations, it might be helpful to subclass UITextView, to keep the functionality encapsulated.
Note: If testing on the simulator, make sure you are using the
Simulator's keyboard, by going to the menu: I/O -> Keyboard -> Toggle
Software Keyboard
If you are adding text automatically, for instance in response to a button tap, and the shift key turns off, then you can use this pattern:
autocapitalizationType = .allCharacters //Activate shift key
autocorrectionType = .no //Don't show all-caps auto suggest
reloadInputViews() //Apply changes
Then, to restore to sentences capitalization, for instance when a line is cleared:
if (autocapitalizationType = .allcharacters) {
autocapitalizationType = .sentences //Activate sentence capitalization
autocorrectionType = .yes //Show auto suggest
reloadInputViews() //Apply changes
}
Other capitalization options:
.none
.words
You may also need to do this when any non-enter key is pressed. To do so, override insertText from UIKeyInput, and check the entered text string:
if (text != "\n") {
if (autocapitalizationType == .allCharacters) {
autocapitalizationType = .sentences //Activate sentence capitalization
autocorrectionType = .yes //Show auto suggest
reloadInputViews() //Apply changes
}
//Respond to any character sequences
//...
}
Try to add this after initialization:
self.textView.autocapitalizationType = UITextAutocapitalizationTypeWords;
OR
It's very likely that the "Auto-Capitalization" option is turned off on that device. You can find that option in Settings > General > Keyboard.

Disable editing of a text field

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()
}

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

Resources