How can I block keyboard for input (something like userInteractionEnabled = NO) in iOS >= 4 ?
Keyboard is used for TextEdit.
If I disable interaction for textedit then keyboard disappears, but I want to see the keyboard.
have you tried this delegate?
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
Le Coeur, I think you need to consider a different approach to whatever you're trying to achieve by restricting the keyboard input for two good reasons:
A UIElement that engages the user to type but doesn't react to any of the user's gestures is sure to be rejected by Apple.
Because this keyboard is on the OS level, it is private API and I am pretty sure there is nothing you can do to the keyboard to block access to the keys.
Hope this helps
You can always disable userInteractionEnabled on the keyboard view, or less-dangerously add an invisible subview to the keyboard that blocks interaction. See iOS: How to access the `UIKeyboard`? on how to get the UIView for the keyboard.
Related
I have to implement a custom autosuggestion for UITextField>
I have an array of words and when I start typing in a textfield then all words similar to the typed text should appear as auto suggestion, like normal auto suggestion in ios but more than one word. And When I tap on one of them it should trigger an event. I know how to done this with a tableview anybody know a method to done this with ios's look and feel?
For this purpose I used the project linked below:
http://cocoacontrols.com/platforms/ios/controls/autocompletiontableview
A sexy look for a autocomplete:
UITextField + UITextFieldDelegate
http://www.cocoacontrols.com/search?utf8=%E2%9C%93&q=wepopover
Using UITableView you keep the data and apply an predicate on:
(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
I didn't attach source code because isn't general and might confuse you.
Make an array of NSString, and search the typed string in UITextfield delegate method textChanged:, then print strings in a tableView.
In fact this will work like a UISearchBar.
I am wondering why I am not able to find any help regarding the keyboard keyup/keydown events, I actually want to detect which key is been pressed by the user via keyboard on iphone while inputting the text in the UITextField.
You can use this delegate method to get the character that's being entered into a UITextField:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
For example:
if ([string isEqualToString:#"z"]){
// z is being typed.
}
Just make sure you've said your UITextField's delegate to the class file where you implement this.
How do I know when a user taps on the Join button on the UIKeyboard?
Also can this text be changed? I know there are several built options but I nee done that says 'Login' if I can.
I'm not a big fan of using UITextField delegate just for knowing when keyboard entry is completed by pressing the "return" on the keyboard (whatever it's labeled). UITextField actually works well with a Target/Action pattern which is much simpler and far more direct than using the delegate for this.
I've pointed this out before in an answer to "How to call a method when the Done Button in the KeyBoard is Clicked?". Please take a look at that for the details of using the traditional target/action pattern to know when keyboard entry is complete. Unfortunately, Apple's documentation isn't as clear is it could be on this subject.
UITextField delegate has its place, but if all you need is to know when entry is complete then using the delegate likely isn't the best approach. Use target/action.
For the UIReturnKeyType choices, see the returnKeyType property in the UITextInputTraits Protocol Reference.
You should set your delegate of your UITextField (I'm assuming that's a UITextField) and check for that delegate call :
- (BOOL)textFieldShouldReturn:(UITextField *)textField
Also, you return a BOOL to tell the UITextField to do its default behavior or not. You can check the documentation here.
Also, the options Apple gives you for the UIReturnKeyType are your only options.
Is there a way to prevent the user from moving the cursor in a UITextField? I'd like it to stay at the end of the string.
This is an old question, but I was looking for an answer to the same question, and found a solution.
It is actually quite simple to prevent the user from moving the cursor. Just subclass UITextField and provide the following implementation of caretRectForPosition:
- (CGRect)caretRectForPosition:(UITextPosition *)position
{
return [super caretRectForPosition:self.endOfDocument];
}
NO SUBCLASS needed.
You Could use UITextFieldDelegate. It will make disable magnifying glass & text selection.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
textField.userInteractionEnabled = NO;
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
textField.userInteractionEnabled = YES;
}
NB: This is just a bypass.
There's no way to prevent them from moving the cursor. You can, however, prevent them from editing the text except at the end by implementing the
– textField:shouldChangeCharactersInRange:replacementString:
method in your text field's delegate.
Edit: you can also set userInteractionEnabled to NO so that the user can't tap the field. Call becomeFirstResponder manually so that the field gets focus since the user can't tap to focus.
I'd suggest to check the gestureRecognizers property.
You will find a lot of them in the array and might want to either remove them all or to find the ones that triggers the event you want to intercept and remove/replace it.
I used it to remove copy/paste and magnifying glass functionalities from an UITextField
I haven't check if you can disable the magnifying glass, but the recommended way to selectively disable editing behavior in a UIResponder (and thus a UITextField) is to implement canPerformAction:withSender of UIResponder.
See UIResponder documentation.
Maybe if you return "NO" for the select and selectAll action you can disable it.
Another, more brutal, way is to intercept any touch event and reset the cursor to the end.
On the iPhone, I've tried to find a way to trap and act upon the backspace key pressed event. But cannot find a way to do that!! There sure must be one such way documented in the SDK right?
/John
For your UITextField which is taking the input, set your controller object to be the text field's delegate and implement the
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
delegate method in your controller.
This method should be called for every editing event (keypress). I believe that you should be able to look for a blank replacement string to tell that characters are being removed by the backspace key.