how to disable userinteractionenable of uitextview without hiding the keyboard - ipad

i use uitextview to show some text, but i have use some animation too. During this animation appear I want to disable the keyboard but not hide it. I tried to use self.textview.userInteractionEnabled = NO;, self.textview.editable = NO;, and also [self.textview resignFirstResponder]; but it still make the keyboard hide, and also make my animation didn't show the right animation.
Can somebody give me suggestion?
Thank You

Declare (BOOL)isAnimating and set it to YES when the animation begins and set it to NO when the animation end. Then implement the following:
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
{
return !isAnimating;
}
The keyboard stays up and nothing happens in the textfield when the animation is running. After it ends, everything goes back to normal.

Related

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
}

how to check if focus switches from one text view to another

I've got 2 UITextFields. I can detect when the user in inside either of them with:
textFieldDidBeginEditing:(UITextField *)textField
Which works great. But i want the textfields to animate out when the user is not inside either of them. Currently I'm calling: (void)textFieldDidEndEditing:(UITextField *)textField
However, this is called even if i switch from on textfield to the other. Is there a better way to call do this?
What I would do is add a BOOL flag to detect if you should animate out your text fields or not. It would work something like this:
-(void)hideTextFields {
if (self.shouldHideTextFields) {
self.textField1.hidden = YES; // Or whatever you want to do with
self.textField2.hidden = YES; // your text fields
}
}
Declare a method that checks the BOOL flag and decides whether to hide or not the text fields
Whenever either of the textFieldDidBeginEditing:(UITextField *)textField methods are called set the BOOL flag (you can call it 'shouldHideTextFields') to NO.
Whenever either of the textFieldDidEndEditing:(UITextField *)textField methods are called set the BOOL flag to 'YES'. Also, call [self performSelector:#selector(hideTextFields) withObject:nil afterDelay:1]; to give the user a little time to select the other text field. If he/she does, the flag will be set to NO thanks to the above step.
Hope this helps!
On didend check if either of your textviews is currently the first responder with [textfield isFirstResponder] or [textfield isEditing]. There may be a slight delay when one ends and the other takes control. If that's the case then you could do this check after a slight delay using performSelector:afterDelay.
On textFieldDidEndEditing delegate method, do not just perform your disappearing animations, but do that in dispatch_async, checking if there is no textField editing right now.
In case if user just ended editing of one text field, there will be no editing textfield. But if user had switched to another textfield, it will already start editing and it can be easily checked by isFirstResponder method.

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 =/

iOS 6 - Responding to Keyboard Done Button State

In the iOS Apprentice Series eBooks, you are supposed to build a Checklist app. The tutorials have you go through and make a Done button on a button bar that is disabled and enabled based on the text inside of a UITextField object.
- (BOOL)textField:(UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newText = [theTextField.text stringByReplacingCharactersInRange:range withString:string];
self.doneBarButton.enabled = ([newText length] > 0);
return YES;
}
Now, the keyboard is set to disable the Done button (on the keyboard) if the text field is empty. Is there a way to observe the state of the keyboard done button and have the done button bar button reflect it accordingly?
IE, when the keyboard done button is enabled, the button bar done button is enabled.
UITextField supports what you want through the enablesReturnKeyAutomatically property. This property is from the UITextInputTraits protocol.
When you create your UITextField, set this property.
self.textField.enablesReturnKeyAutomatically = YES;
This means the Return key (whatever it is labeled) will automatically becomes disabled if the text field is empty. And it automatically becomes enabled when text is entered.
There is no way to observe the state of this so you must implement the code you already have for textField:shouldChangeCharactersInRange:replacementString: to update your other Done button.
You may add a target on your text field with the following events.
UIControlEventEditingDidBegin
UIControlEventEditingChanged
UIControlEventEditingDidEnd
UIControlEventEditingDidEndOnExit
UIControlEventAllEditingEvents
Example:
In viewDidLoad
[_textField addTarget:self action:#selector(textFieldEditing:) forControlEvents:UIControlEventAllEditingEvents];
Action Method:
- (void)textFieldEditing:(id)sender
{
_doneButton.enable = ([[sender text] length]>0);
}

How to prevent the keyboard from showing (not dismissing) on a TextView?

I would like to know how to DISABLE (not how to dismiss) the iOS keyboard in a TextView. I don`t even want the keyboard to show up when the user touches the TextView.
All of the examples I found were to make the keyboard disappear AFTER it appears.
The closest thing I got was to set textView.userInteractionEnabled = NO; but that gets rid of the scrolling as well (I want to keep the scrolling).
Thank you in advance!!
Try to implement the following method in text view's delegate:
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
return NO;
}

Resources