Clearing text on UITextView while begin edit - ios

On a UIViewController I have placed an editable UITextView with some text to be displayed while loading say "Some comment". My requirement is that when I click inside the text view, the pre-defined text should get cleared i.e. the text view should be blank and should no more show "Some Comment" text on it.
I used textViewDidBeginEditing as follows, but is not hitting the method itself:
- (void)textViewDidBeginEditing:(UITextView *)textView
{
textView.text = #"";
}
Do I need to raise any events for this or I need to define anything else for making the program to hit that method when the user clicks inside the text view.
Thanks!!

Set the delegate for the UITextView inside the .Xib File by connecting it to the File's Owner.
myTextView.delegate = self;

Related

Click spacebar on the keyboard programmatically

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
}

How to create UIView to show text like in Terminal?

I'm making telnet program and I have everything resolved but the text output.
I want it to have console look and feel, and basic controls like UITextField or UILabel do not work at all for this.
Is there any custom control to do this?
How can I write one myself?
You can use UITextView to display text and UITextField to input the text and override textFieldShouldReturn: method:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSString* inputString = textField.text;
[inputString lowercaseString];
// when you type 'clear' clear output view
if ([inputString isEqualToString:#"clear"])
{
// Your text view outlet to display the data
[self.outputTextView clear];
}
else
{
[self.outputTextView setText:inputString concatenate:YES];
// Your text field outlet to input the data
[self.inputTextField setText:#""];
return YES;
}
Remember to set up text field delegate for input text field.

Why can't I CTRL drag an action for storyboard?

I am new to iOS programming and using iOS 6. I see that, using XCode I can CTRL+drag action and outlets for button sand text field but not for the storyboard. I want to do some action when user clicks on the storyboard (taps away from the text field).
Here is my code:
- (IBAction)editingEnded:(UITextField *)sender {
NSLog(#"%#", #"in editingEnded");
[sender resignFirstResponder];
}
- (IBAction)buttonSelected:(UIButton *)sender {
if(_firstClick) {
[_textField resignFirstResponder];
}
}
I guess, you asked me to implement something like editingEnded? This is my delegate for editing did end action (how can I confirm this, there is no such annotation/attribute attached to this method?). However, this method alone didn't work. When i added the 2nd method buttonSelected as a delegate for another button on the story board, then editingEnded is also called due to [_textField resignFirstResponder];.
A storyboard is a container which contains your various UI elements, including your text field.
What you really want to do is set a delegate for your text field, which you can do with your view controller.
Then, when you click away from the text field, you can catch that happening via the delegate method "textFieldDidEndEditing:". You implement that function in your view controller, make certain your text field has your view controller set as the delegate, and you should be able to do whatever you want within your view controller's implementation of the "textFieldDidEndEditing:" function.

keep contents of textview till a button is pressed

I have been looking for a code to keep the contents of the textview ,till i click on a back button or a done button on the same page. But when i gave the code:
(void)textViewDidBeginEditing:(UITextView *)textView{
myTextView.text = nil;
}
Every time(even before completing the text) , if i press the return button of keyboard and click on text view again to type the rest, the already existed text clears away.
I have kept the above code for clearing the default text "Enter your text", when the textview is shown for the first time.
I have been working by setting booleans, but could't get the result! Please if someone could clear me out of this . Thanks in advance.
Do it like this
(void)textViewDidBeginEditing:(UITextView *)textView{
if ([myTextView isEqualToString:#"Enter your text"]) {
myTextView.text = nil;
}
}
This code will check that is your text is equal to Enter your text and if it is than it will clear otherwise not.

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;

Resources