I was wondering if there was a way to detect if a textField.text is currently highlighted. I am trying to format a phone number, and it works under the exception that the user highlights the field, then starts typing a new number immediately instead of clearing it first. The first ( does not get set because i try to detect if the field is length 0 before adding it. On a text highlight, then keypress, the length is larger than 0 so it doesnt work.
thanks
Yes, since UITextField (and I believe UITextView) adopt the UITextInput protocol, you can send messages to those with any of that protocols methods, included selectedTextRange. See UITextInput Protocol Reference.
Related
I would like to create grouping with kerning in a UITextField. I use shouldChangeCharactersIn delegate method to be able to react to every text change. I format my text then assign it back to the attributedText property of the textfield. It works great but I have a small bug which I don't understand. When I leave the cursor after a character which has different kerning (lets say 4) then the default (around 1) and the textfield loses the focus that default kerning changes to the one which was before the cursor (4). Any idea why is this happening? How to solve this problem nicely?
I have noticed that if I nil out the property selectedTextRange in textFieldShouldEndEditing then this bug disappears but It just doesn't seem nice to implement this method just because of this.
Source code of this ViewController can be found here.
I'm writing a keyboard extension for iOS (hence overriding UIInputViewController) and I'm trying to figure out how to detect when the first responder changes. Is this even possible?
My motivation is that when the user selects a different text input field (while the keyboard is active) the style of the keyboard might need to change to suit the attributes of that input. This can happen when there are several text fields displayed on a UI and the user first selects one (causing the keyboard to be initialized) then the user selects another with different attributes (keyboard doesn't know it).
I've looked through the methods exposed by UIInputViewController and the delegates it implements but nothing I've seen really fits the bill. The closest thing I've found is selectionDidChange on UITextInputDelegate.
I found the best way to get this information is to override the UITextInputDelegate textDidChange method (UIInputViewController implements UITextInputDelegate). It turns out that textDidChange is called whenever the user switches the input text field (first responder), or when the text changes for some reason (luckily not when it is your keyboard that initiated the change).
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
That should tell you when it expects to become firstResponder. A couple things to keep in mind;
*This will only be called when a UITextFied is the thing becoming firstResponder. If some other object decides to, this won't be called. It'll probably call the method below.
-(BOOL)becomeFirstResponder
*Your class must conform to the UITextFieldDelegate and all of your UITextFields must set their delegates to self. Without that they won't call shouldBeginEditing on your class.
I'm new to objective-c, and I've done this in Flex/Actionscript, but hitting a wall with IOS. Is it possible to get the index of a character under a point(x,y) in a TextField or Label and then be able to modify that single character in IOS?
I have a TapGestureRecognizer coming in with a point in my view. I need to now be able to isolate and highlight just the character at the point coming in.
For example if I have the text "Hello" on screen and I ask them to click on the 'e', I need to know which letter they touch and highlight it green/red depending on if they were right or not.
UITextField (and UITextView) conforms to the UITextInput protocol which has the method characterRangeAtPoint:.
Once you have the CGPoint from the gesture, use this method on the text field to get a UITextRange. Then use the textInRange: method to get the text associated with that range.
dose anyone know how to detect a delete press from a numberpad in ios?
If you're using a UITextField, your delegate's textField:shouldChangeCharactersInRange:replacementString: will be called with a range of length 1 and an empty replacement string. If nothing is deleted, however, you will get no notification.
If you're using a UITextView, your delegate's textView:shouldChangeTextInRange:replacementText: will be called with a range of length 1 and an empty replacement string. If nothing is deleted, however, you will get no notification.
If you're using your own class that implements UIKeyInput, deleteBackward is supposed to be called. I don't know whether a class implementing UITextInput (which itself includes UIKeyInput) might ever have replaceRange:withText: or setMarkedText:selectedRange: called instead with a range of length 1 and an empty replacement string, or what might happen in those cases if there is nothing to delete.
If you are implementing something like a passcode, I think a hidden textfield would be preferable, and more controls.
If I want to handle changes to a UITextField, such as the user typing in it; it seems like this can be done either by assigning a delegate to that text field, and then having the delegate implement shouldChangeCharactersInRange, or by adding a target to the textField, and handling the UIControlEventEditingChanged event.
Aside from the fact that with the delegate method, you can return NO and therefor stop the user from making the edit, is there any difference between these 2 things?
Same question for handling the beginning of editing or the ending of editing. It could be done either with the appropriate delegate methods or with the appropriate events. What is the textField delegate actually for if the control events can do the necessary work?
shouldChangeCharactersInRange is called before a change occurs, and gives you opportunity to 'cancel' the change. UIControlEventEditingChanged is called after the change occurred.
You can determine the resulting value of the textField in shouldChangeCharactersInRange, but you have to manually apply the replacementString to the existing text, using the supplied range. (via NSString stringByReplacingCharactersInRange). If you want to know the resulting text, it's easier and more efficient to use UIControlEventEditingChanged.
shouldChangeCharactersInRange is often used to implement validation checking of input - that is, you can filter characters/pasted text as it is entered. If a field is for phone numbers, for example, you can return FALSE if the user types a non numeric character, or attempts to paste in text that isn't numeric.
You might find a case where you can reuse code for multiple controls if you can stick with the UIControlEvent-methods.
You're right; you can essentially do the same thing via both, but UIControl is lower level and lets you siphon off each particular UIEvent to different targets via [UIControl addTarget:action:forControlEvents:] where as there is only a single delegate.
I would also say that the UITextField delegate protocol is simply there as a more convenient, higher level alternative to UIControl/UIEvent as a way to manage the behaviour of a UITextField.
The most common delegate pattern is UITableView DataSource and Delegate and I would say that using the UITextField delegate protocol is quite similar and therefore looks far more straight forward with more defined intentions than handing the messages from UIControl directly.
One key difference I've found between the two approaches posed in the original question is that the delegate "shouldChangeCharactersInRange" gets called BEFORE the value in the UITextField changes. The target for UIControlEventEditingChanged gets called AFTER the value in the UITextField changes.
In the case that you're using these events to make sure (for example) that all fields in a dialog are completely filled in before enabling a "Done" button, the target approach may work better for you. It did for me.
The delegation approach is the way to homogenize UITextField and UITextView behavior.
UITextView does not have control events. In contrast, UITextFieldDelegate and UITextviewDelegate provide parallel methods.
I have found out that shouldChangeCharactersInRange passes the same NSRange for insertion and deletion of text. You append a space and then delete it, and the parameters from shouldChangeCharactersInRange are indistinguishable from duplication of the text.
So shouldChangeCharactersInRange actually cannot predict the resulting text.