Disable Keyboard for UITextfield - ios

I'm wondering how to disable the inputview of a UITextfield. Setting textField.inputView = nil; or [textField setInputView:nil] in ShouldBeginEditing doesn't do anything, and using the userInteraction property removes the ability to interact with the field. Ideally, I'd like to remove both the cursor and the keyboard while still being able interact with and switch between textfield methods, using ShouldBeginEditing and ShouldEndEditing. Is there any way to accomplish this?

You should do this:
myTextField.inputView = UIView.new; //Empty UIView
Setting it to nil just means the default keyboard is used.
To get rid of the caret, subclass the UITextField and override caretRectForPosition:
- (CGRect) caretRectForPosition:(UITextPosition*)position
{
return CGRectZero;
}

Try this :
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return NO; // Hide both keyboard and blinking cursor.
}
or
-(void)textFieldDidBeginEditing:(UITextField *)textField{
[textField resignFirstResponder]; // hides keyboard
}

Related

How to raise a table cell above keyboard level with textfield inside that cell is focused?

I'm using the following to raise a textfield inside a table cell above keyboard level. But this only works if I tap the textfield. If I focus the text field programmatically ,i.e., [textField becomeFirestResponder], it doesn't work. Edit: also textFieldShouldBeginEditing: is being called but the code inside isn't performing as expected.
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
CGPoint pointInTable = [textField.superview convertPoint:textField.frame.origin toView:_tableView];
CGPoint contentOffset = _tableView.contentOffset;
contentOffset.y = (pointInTable.y - textField.inputAccessoryView.frame.size.height);
[_tableView setContentOffset:contentOffset animated:YES];
}
return YES;
}
Try calling the [self textFieldShouldBeginEditing:textField] after setting [textField becomeFirstResponder]
call [textField becomeFirstResponder] and implement your keyboard avoiding code in textFieldDidBeginEditing as it is called after editing has begun. On the other hand textFieldShouldBeginEditing is called before beginning editing. You can properly obtain the keyboard height after editing has begun.
You can use third party class TPKeyboardAvoidingTableView
Which will automatically adjust and scroll TableView when keyboard get opens.
https://github.com/michaeltyson/TPKeyboardAvoiding

Detect when a user presses the space button

I currently have my keyboard set so the type is Numbers and Punctuation [so users can type in a hypen symbol]. Does anyone know how to detect if the user presses the space bar so I can automatically set the style to a different keyboard type [in my case letters] so I don't have to build a custom keyboard?
Try this code - I am sure that it will detect if a space character is coded in - I have not really tested how well the keyboard switch works. You should also try it out without the resignfirstresponder becomefirstresponder code once the code below is working. Good Luck! (PS your viewCOntroller needs to be declared as a UITextview or textfield Delegate)
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText: (NSString *)text_message {
if ([text_message isEqual:#" "]) {
// Put your code to change the keyboard then refresh the screen.
[textView resignFirstResponder]; //close textview or textfield
[textView setKeyboardType:UIKeyboardTypeDefault];
[textView becomeFirstResponder]; // opens keyboard for textfield or textview
[self.view setNeedsRefresh]; // refresh view
// return NO;
}
return YES; //go back to editing
}

Using next and previous button with UITextField, problems with firstresponder

I am using a subclass of a scrollview that moves the keyboard out of the way. (TPKeyboardAvoidingScrollView)
I think this is conflicting with my implementation of the next and previous buttons. I have built an inputaccessoryview
I have a category that sets the next and previous textfields for each field
when i edit a textfield, i set the current, previous and next textfields
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
_currentTextField = textField;
_prevTextField = [textField prevTextField];
_nextTextField = [textField nextTextField];
return YES;
}
when they click next or previous i call this method
- (void)selectAdjacentResponder:(id)sender{
UISegmentedControl *segmented = sender;
if(segmented.selectedSegmentIndex == 0){
[_prevTextField becomeFirstResponder];
} else {
[_nextTextField becomeFirstResponder];
}
}
this works fine.. but when i close the keyboard. my scrollview is messed up. if i add the line
[_currentTextField resignFirstResponder];
to the first line of my selectadjacent method it solves the problem. but the problem is it makes the screen focus in a funky way since i'm dismissing and accessing the next textfield at the same time.
i have tried resigning first responder when i close the keyboard. but i think my scrollview is calculated before that point.. any idea what to do =/

resignFirstResponder programmatically [duplicate]

This question already has answers here:
Understanding resignFirstResponder with UITextField
(3 answers)
Closed 9 years ago.
How do I remove a keyboard from the view as a result of the return key being touched when the UITextField was created programmatically.
If the UITextField was called in the viewDidLoad I know how to do this, but the UITextField was created as a result of an -(IBAction).
I created my UITextField programmatically. I know the resignFirstResponder removes the keyboard. I have it set up to do so when the screen is taped outsie the keyboard. I also have it working to where if the user triggers the IBAction with the UIButton related to the UITextField the keyboard goes away. I also want to be able to hide the keyboard when the user selects return from the keyboard.
You need to make yourself a UITextFieldDelegate and implement:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
Make sure you set the textField's delegate to self when you create it.
You can use this:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
You'll need to set your text field's delegate to self first, though:
self.textField.delegate = self;
Or, you could right-click-drag from the text field in IB to little orange circle at the bottom.

UITextView setSelectedRange changes editable property

From the start, my UITextView with editable property to true is not editable (via settings in a NIB). The behaviour is such that a user can select and copy text but not edit. This is the way things should be.
However, if I make a call to setSelectedRange, a side effect is that the editable property is set to YES.
Setting it back to NO [textview setEditable:NO] scrolls to the bottom of the textView and undoes my programmatic selection. It also doesn't work, as editing becomes enabled anyway. The keyboard appears and everything.
I need to be able to select something programmatically and keep the textView in a state where users can only copy and select text.
[textView select:self];
[textView setSelectedRange:selectedText];
I'm stuck. Looking for any advice you can give.
This doesn't work:
[textView select:self];
[textView setSelectedRange:selectedText];
[textView setEditable:NO];
I've also tried setting the delegate function textViewShouldBeginEditing to return NO:
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
return NO;
}
That just locks everything down and I can't select any text.
I realize this has already been answered, but here is an improvement I made to keep the popout menu, just disable certain options;
-(BOOL) canPerformAction:(SEL)action withSender:(id)sender {
bool response = [super canPerformAction:action withSender:sender];
if(response && (action == #selector(cut:) || action == #selector(paste:) || action == #selector(delete:) || action == #selector(_promptForReplace:))) {
return NO;
}
return response;
}
canPerformAction is called per defined action. Calling the parent method will take care of most of these cases, but I have also decided to disable cut,paste,delete and spelling suggestions (_promptForReplace).
This appears to work for me in a similar situation:
just let the textview be editable
[textView setDelegate:self];
[textView select:self];
[textView setSelectedRange:range];
add a function (BOOL)textView:shouldChangeTextInRange:replacementText: returning NO
and the final trick: assign an empty view as a keyboard for the textview, using:
textView.inputView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
The textView should be editable:
[textView setEditable:YES];
Do the selection:
[textView select:self];
[textView setSelectedRange:range];
Have these in the delegate:
To disable the menu (not ideal for me but it's ok):
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
[UIMenuController sharedMenuController].menuVisible = NO;
return NO;
}
To disable the keyboard:
textView.inputView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
To disable editing:
- (BOOL)textView:(UITextView*)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text {
return NO;
}

Resources