Muhammad Rehan
Muhammad Rehan

Reputation: 45

TextView Placeholder in ios objective c

i am using textview in iOS application but i am facing some problem when i am set placeholder in my textview the text not hide when click in textview so can any here who work placeholder in textview

Upvotes: 0

Views: 3294

Answers (4)

Super Developer
Super Developer

Reputation: 897

It is not possible to create placeholder in UITextView but you can generate effect like place holder by this.

- (void)viewDidLoad{
    commentTxtView.text = @"Comment";
    commentTxtView.textColor = [UIColor lightGrayColor];
    commentTxtView.delegate = self;
}

- (BOOL) textViewShouldBeginEditing:(UITextView *)textView
{
    commentTxtView.text = @"";
    commentTxtView.textColor = [UIColor blackColor];
    return YES;
}

-(void) textViewDidChange:(UITextView *)textView
{
    if(commentTxtView.text.length == 0){
        commentTxtView.textColor = [UIColor lightGrayColor];
        commentTxtView.text = @"Comment";
        [commentTxtView resignFirstResponder];
    }
}

-(void) textViewShouldEndEditing:(UITextView *)textView
{
    if(commentTxtView.text.length == 0){
        commentTxtView.textColor = [UIColor lightGrayColor];
        commentTxtView.text = @"Comment";
        [commentTxtView resignFirstResponder];
    }
}

OR you can add label in textview just like

lbl = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 0.0,textView.frame.size.width - 10.0, 34.0)];


[lbl setText:kDescriptionPlaceholder];
[lbl setBackgroundColor:[UIColor clearColor]];
[lbl setTextColor:[UIColor lightGrayColor]];
textView.delegate = self;

[textView addSubview:lbl];

and set

- (void)textViewDidEndEditing:(UITextView *) textView
{
     if (![textView hasText]) {
         lbl.hidden = NO;
     }
}

- (void) textViewDidChange:(UITextView *)textView
{
    if(![textView hasText]) {
       lbl.hidden = NO;
    }
    else{
       lbl.hidden = YES;
    }  
}

Upvotes: 1

Ramesh_iOS
Ramesh_iOS

Reputation: 11

Try This

1.Create a label and show or hide

-(void)viewdidLoad
{
   UILabel*placeHolderLabel = [UILabel alloc]initWithFrame:CGRectMake(0,0,300,30)];
   placeHolderLabel.text = @"PlaceHolderText";
   placeHolderLabel.textColour = [UIColour lightGrayColor]; 
   [yourTextView addSubView:placeHolderLabel];
}

//2.TextViewDelegate

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    placeHolderLabel.hidden = YES;
    [textView becomeFirstResponder];
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:@""]) {
        placeHolderLabel.hidden = NO;
    }
    [textView resignFirstResponder];
}

Upvotes: 1

caldera.sac
caldera.sac

Reputation: 5108

NOTE: There isn't any inbuilt function for placeholder in textviews like textfields.

create an IBOutlet to your textView in .h file like below

enter image description here

and add <UITextViewDelegate> like below

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *mytextView;


@end

then in your .m file, implement textview delegate methods like below.

enter image description here

and if you want some addition, try following method also with above code

- (void)textViewDidChange:(UITextView *)textView
{
    NSString *mtvt = textView.text;
    if ([mtvt isEqualToString:@""]) {
        self.mytextView.text = @"Your message here";
    }

}

Upvotes: 3

Charmi Gheewala
Charmi Gheewala

Reputation: 828

You can use textView delegate methods to manage placeholder text programatically

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:@"myPlaceHolderText"])       
    {
     textView.text = @"";
    }
    [textView becomeFirstResponder];
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:@""]) {
        textView.text = @"myPlaceHolderText";
    }
    [textView resignFirstResponder];
}

Upvotes: 5

Related Questions