Click spacebar on the keyboard programmatically - ios

I have an issue with a resizing UITextView
It resizes correctly when user is typing and resizes incorrectly, when i set its text programmatically
with [textView setText:]
I want to set its text to blank by doing setText:#"" and then clicking the spacebar programmatically
How do i click the space bar programmatically ?
Here are screenshots of my problem

More than likely you've put code in (void)textViewDidChange:(UITextView *)textView that doesn't belong there.
The text view calls this method in response to user-initiated changes
to the text. This method is not called in response to programmatically
initiated changes.
The solution is to create a separate method that runs regardless of whether the text was changed programmatically or by the user. It will probably looks something like this.
- (void)setText:(NSString *)text {
self.myTextView.text = text;
[self updateTextViewSize];
}
- (void)textViewDidChange:(UITextView *)textView {
[self updateTextViewSize];
}
- (void)updateTextViewSize {
//sizing logic goes here
}

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 Stop keyBoard Appearence at click event on UITextview?

In my IPad Application i am using TextView only for Text Displaying.As i need to display a Larger Text Thats Why i am using UITextview due to its Scrolling Property instead of using UILabel.
In my application i do not need to edit Text in UITextview ,but problem for me is that when i click on Textview for scrolling the keyboard appear its hide my textview so i want that my keyboard is never appear on click event.i make a search but not find any Suitable solution.Any help will be appriated.Thanx
NEW ANSWER (previous one was not working properly)
OK so since that is not working because it disables scrolling also, you should try to:
Implement UITextFieldDelegate protocol
In your view controller add the text
#interface YourViewController () <UITextViewDelegate>
In viewDidLoad set yourself as a delegate:
yourUITextView.delegate = self;
Implement the delegate method below:
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
return NO;
}
When the textview is about to edit the text, this method will be called automatically. It returns no, so the editing won't start.
It is very important that you undo the changes from the previous answers: Do not set the editable field to NO
I tried it and it's working. Hope it helps!
OLD ANSWER
when you declare the variable, or in your viewdidload method, set the editable property to NO:
yourUITextView.editable = NO;
or
[yourUITextView setEditable:NO]
That should prevent the keyboard from appearing.
Go to .XIB file and you can uncheck behavior editable or programmatically
textView.editable = NO;

iOS: "Message sent to deallocated instance" when resign first responder on a UITextView when its auto correction pop-up is shown

I have a custom toolbar with a "Done" button for the input accessary view of my text view. When this "Done" button is tapped I want to resign the text view from the first responder, so I call:
[textView resignFirstResponder];
This will throw an error:
Thread 1: Program received signal: "EXC_BAD_ACCESS".
when the "Done" button is tapped while the auto correction is shown (See image below). The error still even I call:
if ([textView isFirstResponder] && [textView canResignFirstResponder]) [textView resignFirstResponder];
It seems like the text view is the first responder and can be resigned but I cannot resign it. How can I solve this error? Thank you.
Edit 1: I still want to enable auto correction.
Edit 2: Please take a look at the capture image below.
Edit 3: After turning on Zombies in the scheme settings, the logged message is:
-[TIZephyrCandidate wordOriginFeedbackID]: message sent to deallocated instance 0x52bbc50
but I don't know what is the meaning of this message and what to do next.
Edit 4: The method to resign first responder will be called when the "Done" button is touched up inside the button is added target and action by the following line of code:
[doneButton addTarget:self action:#selector(resignAllFirstResponders) forControlEvents:UIControlEventTouchUpInside];
which the resignAllFirstResponders is:
- (void)resignAllFirstResponders
{
...
if ([textView canResignFirstResponder] && [textView isFirstResponder])
[textView resignFirstResponder];
...
}
if you using :
- (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementText:(NSString *)aText
UITextViewDelegate and change directly text in the method like :
- (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementText:(NSString *)aText
{
aTextView.text = #"hi";
return YES;
}
is caused crash.
spellchecking view was appeared,
resignFirstResponder of UITextView,
change text directly textView:shouldChangeTextInRange:replacementText delegate,
app will be crashed.
Let me answer my own question. Anyway please note that I'm not sure this is a good enough solution but I just want to share my current progress and still waiting for better solution.
The concept is to find out whether subviews of the text view contains a view of UIAutocorrectInlinePrompt which is the auto correction pop-up that cause the error or not. Then call the method resignFirstResponder only when the set of subviews not contain UIAutocorrectInlinePrompt. My code are like this:
NSMutableString *subviewMutableString = [[NSMutableString alloc] init];
[subviewMutableString setString:#""];
for (UIView *subview in textView.subviews)
{
[subviewMutableString appendFormat:#"%#", subview];
}
if ([subviewMutableString rangeOfString:#"UIAutocorrectInlinePrompt"].location == NSNotFound)
{
[textView resignFirstResponder];
}
This will not allow to resign text view from first responder when the auto correction pop-up is shown.

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;
}

Force keyboard to show up via button (iOS)

I have an UITextView and I don't want check editable option, how can call keyboard via a button?
This code doesn't work for me!
-(IBAction) yourButtonClick
{
[myTextView becomeFirstResponder];
[self.view addSubview:myTextView];
}
From the iPhone Application Programming Guide
However, you can programmatically
display the keyboard for an editable
text view by calling that view’s
becomeFirstResponder method. Calling
this method makes the target view the
first responder and begins the editing
process just as if the user had tapped
on the view.
So to show the keyboard programmatically,
[textView becomeFirstResponder];
However, the keyboard will never show if the textView is not editable.
The purpose of showing the keyboard is to allow editing. I assume you just don't want the keyboard to appear when the user taps the text view. In this case, you can enable editable programmatically when the button is tapped.
-(IBAction) yourButtonClick
{
myText.editable = YES;
[myText becomeFirstResponder];
}
Then in the UITextViewDelegate, disable editable when the user finishes editing.
- (void)textViewDidEndEditing:(UITextView *)textView {
textView.editable = NO;
}

Resources