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.
Related
There are classes with pre-defined functions such as viewDidLoad or touchesBegan:. Is there one I could use for when keys on the software keyboard are pressed?
This question is not specific to either Swift or Xcode. It's an iOS question.
The answer is "sort of".
There is not a special function that gets called for a key press.
Instead, if the keyboard is attached to a UITextField, you can set up an object to be the text field's delegate, and then the delegate's textField:shouldChangeCharactersInRange:replacementString: method will be called as the user enters text. (At each key press, or on a paste). There is a similar method textView:shouldChangeTextInRange:replacementText: if the input field is a UITextView.
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 need to manually turn caps lock on, on the keyboard.
Using autocapitalizationType property won't do the job. The problem is I am implementing a bullet list feature, where after the user inserts a bullet I need to turn on the capLock right away. This doesn't happen using standard autocapitalization because the first letter in my paragraph is #"•" rather than the text the user needs to enter.
Any way to turn capitalization on manually?
I had a similar issue a while back and what I ended up doing was using the UITextFieldDelegate or UITextViewDelegate depending on which control you are using they both contain textField:shouldChangeCharactersInRange:replacementString: or textView:shouldChangeCharactersInRange:replacementString: which is called every time the content of the field is changed. This allows you to change the character values automatically.
Hope this helps.
You can check out the documentation here: https://developer.apple.com/library/ios/documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html#//apple_ref/occ/intfm/UITextFieldDelegate/textField:shouldChangeCharactersInRange:replacementString:
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.
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.