34

How can I call some code upon entering a UITextView (user taps to edit it) and leaving the view (user taps to leave it)?

Appreciate any help.

MrSmith42
  • 9,961
  • 6
  • 38
  • 49
Josh Kahane
  • 16,765
  • 45
  • 140
  • 253

4 Answers4

43

http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextViewDelegate_Protocol/Reference/UITextViewDelegate.html#//apple_ref/occ/intf/UITextViewDelegate

Here you can find several useful methods to investigate:

  • textViewDidBeginEditing:
  • textViewDidEndEditing:

Moreover to leave UITextView you often should implement action that calls [yourTextView resignFirstResponder];

Objective-C example

//you may specify UITextViewDelegate protocol in .h file interface, but it's better not to expose it if not necessary
@interface ExampleViewController()<UITextViewDelegate> 

@end

@implementation ExampleViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //assuming _textView is already instantiated and added to its superview
    _textView.delegate = self;
}


//it's nice to separate delegate methods with pragmas but it's up to your local code style policy
#pragma mark UITextViewDelegate

- (void)textViewDidBeginEditing:(UITextView *)textView {
    //handle user taps text view to type text
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    //handle text editing finished    
}

@end

Swift Example

class TextViewEventsViewController: UIViewController, UITextViewDelegate {
    
    @IBOutlet weak var exampleTextView: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.exampleTextView.delegate = self
    }
    
    func textViewDidBeginEditing(_ textView: UITextView) {
        print("exampleTextView: BEGIN EDIT")
    }
    
    func textViewDidEndEditing(_ textView: UITextView) {
        print("exampleTextView: END EDIT")
    }
}
knuku
  • 6,082
  • 1
  • 35
  • 43
6

You could also implement the UITextViewDidChange delegate methods. I use the code below in my app for taking quick notes. Whenever the user has entered a character, the observer catches this from the notification center and calls the saveText method.

Here's how:

Add this line to the viewDidLoad method:

[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(textViewDidChange:) name:UITextViewTextDidChangeNotification object:nil];

and these lines at the appropriate place in your code (like in the section that handles the text view delegate methods. TIP: Use pragma marks for this (#pragma mark - TextView Delegate Methods):

- (void)textViewDidChange:(UITextView *)textView{

    NSLog(@"textViewShouldEndEditing"); // Detect in log output if the method gets called
    [self saveText:nil]; // Call any method you like

}
  • The method signature should be `- (void)textViewDidChange:(NSNotification *)notification`. From the [documentation](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextView_Class/#//apple_ref/c/data/UITextViewTextDidChangeNotification): "The affected view is stored in the `object` parameter of the notification." – pejalo Dec 29 '15 at 17:52
2

use the delegate and use:

- (void) textViewDidBeginEditing:(UITextView *) textView {
    // Your code here
}
Quentin Hayot
  • 7,786
  • 6
  • 45
  • 62
0
txtviewaddress.text="Address"

txtViewAddress.TextColor = UIColor.LightGray;
txtViewAddress.ShouldBeginEditing += (textView) =>
        {
 txtViewAddress.Text = "";
            txtViewAddress.TextColor = UIColor.Black;
            return true;
};
txtViewAddress.ShouldEndEditing += (textView) =>
        {
            if (textView.Text == "")
            {
                txtViewAddress.Text = " Address";
                txtViewAddress.TextColor = UIColor.LightGray;

            }
            return true;
        };