Placeholder text in UITextView iOS


As we all know UITextView does not have a Placeholder unlike a UITextField.
So it is really a painful job to have a placeholder text in a UITextView.
I got stuck on the same issue and did some work around.
This trick will make you think that you are actually having a placeholder text inside a UITextView.

So here we go:
1. Place a UILabel on nib (xib) file at the position where you want UITextView.
2. Now set text what you want on the placeholder and change it text color to lightGraycolor.
3. Take a UITextView and place it above the UILabel and set its background color to clearcolor.
4. It will appear as a placeholder inside UITextView.
5. Set delegates of UITextView as follows:

In Header file:
#import <UIKit/UIKit.h>


@interface LoginViewController : UIViewController <UITextViewDelegate>

@end

In Implementation file:

- (void)viewDidLoad {

    [super viewDidLoad];
    [myTextView setDelegate:self];
}

6. Take textViewDidChange delegate method of UITextView.
6. Here set UILabel to hidden YES and set background color of UITextView to white when UITextView does not have text.
7. And in else condition set UILabel to hidden NO and set background color of UITextView to clearcolor when UITextView.

- (void)textViewDidChange:(UITextView *)textView {
    // Enable and disable lblPlaceHolderText
    if ([textView.text length] > 0) {
        [textView setBackgroundColor:[UIColor whiteColor]];
        [lblPlaceHolderText setHidden:YES];
    } else {
        [textView setBackgroundColor:[UIColor clearColor]];
        [lblPlaceHolderText setHidden:NO];
    }
}


Sample code: HERE



Image: Before typing the placeholder is visible.






Image: After typing placeholder hides.



Comments

  1. Thanks! Oct 19, 2014. I checked many sites and found many solutions (even one posted 6 days ago), but didn't understand them. Your step by step solution helped clear everything up. Note to idiots like me: To set the delegate (xcode 6, using storyboard), click on the Text View, and in the Utilities panel (right panel) click on the Connections Inspector and drag the Delegate radio button to the yellow icon for the View Controller the Text View is in (I don't know how to do this programmatically). From there, in your .m implementation file of the view controller, you can type "-(void)TextView" and several options will show up. Hopefully from there, you're good to go.

    ReplyDelete
    Replies
    1. Oh, also add in the .h file, after the @interface MyViewControllerName : UIViewController

      Like this:

      @interface MyViewControllerName : UIViewController

      If you already have a delegate in brackets, add a comma and start typing UITextV. . . and it'll autopopulate.

      Delete
    2. bogus. Brackets don't show up in these replies.

      This is what the post should look like, but instead of $ it should be < and >

      Oh, also add $UITextViewDelegate$ in the .h file, after the @interface MyViewControllerName : UIViewController

      Like this:

      @interface MyViewControllerName : UIViewController $UITextViewDelegate$

      If you already have a delegate in brackets, add a comma and start typing UITextV. . . and it'll autopopulate.

      Delete

Post a Comment

Popular posts from this blog

UITableView - Add or Remove Rows Dynamically (Objective C & Swift Source codes)

payUmoney Payment Gateway Integration in iOS App (Objective C)

Check Internet Connectivity Using AFNetworking 2.0