iOS toggle default keyboard from ABC mode to 123 mode via code? - ios

I can see how to set the keyboard's overall type via:
self.myTextView.keyboardType = UIKeyboardTypeDefault;
How can I toggle the default keyboard's mode from ABC to 123 and back again via code? Basically the moment the user taps the # character (the # symbol is available when they're in 123 mode) I want to switch their keyboard back to ABC mode.
Any ideas?

You might be able to accomplish this in by using the UITextViewDelegate. It allows you to intercept the keys as they are pressed in the UITextView. This will allow you to switch keyboards when the user presses a certain key. In order to revert back to the default state of the UIKeyboardTypeDefault keyboard, you'll need to change the keyboard type to another, then back to the default.
In your ViewController.h file, make it implement the UITextViewDelegate protocol:
#interface ViewController : UIViewController <UITextViewDelegate>
In your ViewController's viewDidLoad method in the ViewController.m, set the textField's delegate to the view controller:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.myTextView.delegate = self;
}
Finally, we need to capture the key as it is being entered and change the keyboard appropriately. We do this in the shouldChangeCharactersInRange: method.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if( [#"#" isEqualToString:text] )
{
NSLog( #"Toggling keyboard type");
textView.inputView = nil;
[textView resignFirstResponder];
[textView setKeyboardType:UIKeyboardTypeEmailAddress];
[textView becomeFirstResponder];
[textView reloadInputViews];
textView.inputView = nil;
[textView resignFirstResponder];
[textView setKeyboardType:UIKeyboardTypeDefault];
[textView becomeFirstResponder];
[textView reloadInputViews];
}
return YES;
}
So I was originally confused and thought you actually wanted to change the keyboard type. Now that I'm reading your comment, I realize that you're actually wanting to reset the keyboard to it's default state of showing the letters, instead of numbers and special characters. The above code now does that.

Related

iOS: Disable keyboard when begin editing TextField

How can I disable the keyboard when I touch-up inside a UITextField?
What I want to do is show a custom digital keyboard instead of the default one.
If I understand correctly that you are looking to create a custom keyboard in the app, I don't think we need to disable the default keyboard when we touch-up inside a UITextField.
We just need to create a custom view and assign it to the inputView property of the UITextField to replace the default keyboard.
For example, something like this:
yourTextField.inputView = yourCustomKeyboardView
See more here.
Note: For Objective-C (Xcode)
In your viewDidLoad: set delegate for textfields which you want to disable.
self.textfield.delegate = self;
and insert this delegate function:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (textField == yourTextfiledOutletInstance) {
[self showCustomkeyboard];
return NO;
}
return YES;
}
//Show custom keyboard
-(void)showCustomkeyboard{
// Handle your operation here to show custom keyboard
}
set the UITextField delegate in the ViewController class, and then add this method in the class
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
Use resignFirstResponder to dismiss your keyboard.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
set inputView on the UITextView to the custom view you want to be used in place of the system keyboard.
myTextView.inputView = myCustomView;

How to always hide keyboard but still can select textField value?

I want my UITextField doesn't show keyboard but the value still can be selected (for example, user wants to move cursor to any index in UITextField)
I have tried to do it this way:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self numberInput].delegate = self;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
[[self numberInput] resignFirstResponder];
}
but the result is, I can't select value from textField. Is there any workaround for this?
You can change the input of the textField.
If you pass an empty UIView, the keyboard will not appear and you should be able to select the text anyway. In swift 2.0:
textField.inputView = UIView()

Show Custom Keyboard on Launch

I am using the following custom keyboard:
https://github.com/kulpreetchilana/Custom-iOS-Keyboards
How do you make it so that they keyboard is displayed at the beginning without having to touch the text view?
Depending on the version of iOS you're using (not sure if it works on 8), you can do:
- (void)viewDidLoad;
{
[super viewDidLoad];
[self.textField becomeFirstResponder];
}
Or
- (void)viewDidLayoutSubviews;
{
[super viewDidLayoutSubviews];
[self.textField becomeFirstResponder];
}
In your ViewController.
Tapping a text view will call becomeFirstResponder on it.
So you should be able to make the keyboard appear with...
[textView becomeFirstResponder];
I've never used that framework though so not 100% sure.

Focus of cursor is not shifting to next UITextView in ios

This is my First question on ios
I am using two UITextView objects (textView1 and textView2) in a View, Each of them has some character limit with some following Scenario:
Initially user can only enter into textView1.
When the entered character limit of textView1 is over, the cursor will automatically shift to textView2.
After building the project, If user tap the textView2 and try to write into it, Cursor must shifted to textView1 (because it is empty).
I wrote the code and everything works fine except the third scenario, User can only enter into textView1 but focus is still on textView2
Steps to reproduce:
Build the project
user tap the textView2 first and try to write something.
According to written code, Focus remain in textView2 but user are writing into textView1 (see the attachment)
Here is the snapshot:
Here is the written code:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.textView1 becomeFirstResponder];
}
- (void)textViewDidChange:(UITextView *)textView{
NSInteger restrictedLengthForTextView1 = 110;
NSInteger restrictedLengthForTextView2 = 130;
NSString *temp=textView.text;
if(textView == self.textView1){
if([[textView text] length] > restrictedLengthForTextView1){
textView.text=[temp substringToIndex:[temp length]-1];
[textView resignFirstResponder];
[self.textView2 becomeFirstResponder];
}
}else{
if([[textView text] length] > restrictedLengthForTextView2){
textView.text=[temp substringToIndex:[temp length]-1];
[self.textView2 resignFirstResponder];
}
}}
- void()textViewDidBeginEditing:(UITextView *)textView{
NSInteger restrictedLengthForTextView1 = 110;
NSLog(#"dalknwdlakwd");
if([[self.textView1 text] length] < restrictedLengthForTextView1){
if(textView == self.textView2){
[self.textView2 resignFirstResponder];
[self.textView1 becomeFirstResponder];
}
}}
Please help me here..
please do as per following:
in .h file
#interface ViewController : UIViewController<UITextViewDelegate>
{
IBOutlet UITextView *txtView1;
IBOutlet UITextView *txtView2;
}
#end
in .m file
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[txtView1 becomeFirstResponder];
txtView2.editable=NO;
}
implement the textView delegate method like below:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if(textView.tag==1)
{
if([textView.text length]>25)
{
txtView2.editable=YES;
[txtView2 becomeFirstResponder];
}
}
return YES;
}
i have taken text length limit in first textview as 24 characters as an example.
i hope this will help you.
It is a known bug, with resigning and becoming first responder within the same runloop. Try the following
[textView2 resignFirstResponder];
[textView1 performSelector:#selector(becomeFirstResponder) withObject:nil afterDelay:0.0];
An update to Annadurai's answer: it's still a bug in 2016! After trying others' suggestions to deselect the surrounding tableview cell, and/or change the tint color only this answer worked for me:
[textView1 performSelector:#selector(becomeFirstResponder) withObject:nil afterDelay:0.0];
FYI My case was that I was resigning a textView, reloading the table, and returning to the same textView. Before this fix - no cursor on the reloaded textView (Yes I could type and see characters.) After the performSelector... afterDelay - the cursor was visible again.

How can i hide the keyboard when the uitextView is in focus~~

I hava a UITextView ,and after some characters' input, I want to use a button click to hide the keyboard and the UITextView is still in focus....[Attention:the button is not a return key or a done key];
Have you set delegate to your UITextView instance?
If so, try using [_textView resignFirstResponder];
else
Try using [self.view endEditing:YES];
mplement following code to your view controller .m file & .h file add delegate of textView
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if([text isEqualToString:#"\n"])
[textView resignFirstResponder];
return YES;
}
Now goto interface builder, select your textview & set return key type done.

Resources