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
            Asked
            
        
        
            Active
            
        
            Viewed 3,082 times
        
    0
            
            
        - 
                    Please show us what you have tried – Arun Sep 07 '16 at 05:37
- 
                    Just check it first: http://stackoverflow.com/questions/4835576/how-to-add-placeholder-text-in-uitextview-in-iphone-sdk – Vishal Sep 07 '16 at 05:38
- 
                    You have to play with TextView Delegate to hide the text when click in textview... – Vishal Sep 07 '16 at 05:40
- 
                    3UITextView doesnot have a placeholder property by default. You need to handle it from your code. Please refer this http://stackoverflow.com/questions/1328638/placeholder-in-uitextview – Arun Sep 07 '16 at 05:40
- 
                    1Possible duplicate of [How to insert placeholder in UITextView?](http://stackoverflow.com/questions/7038876/how-to-insert-placeholder-in-uitextview) – Saurabh Jain Sep 07 '16 at 05:41
- 
                    https://github.com/devxoul/UITextView-Placeholder – Rushabh Sep 07 '16 at 06:48
4 Answers
5
            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];
}
 
    
    
        Charmi Gheewala
        
- 818
- 7
- 9
3
            
            
        NOTE: There isn't any inbuilt function for placeholder in textviews like textfields.
create an IBOutlet to your textView in .h file like below
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.
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";
    }
}
 
    
    
        caldera.sac
        
- 4,918
- 7
- 37
- 69
1
            
            
        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];
}
 
    
    
        Ramesh_iOS
        
- 11
- 2
1
            
            
        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;
    }  
}
 
    
    
        Super Developer
        
- 891
- 11
- 20
 
    
