Placeholder in UITextView

前端 未结 30 2815
野趣味
野趣味 2020-11-22 16:01

My application uses an UITextView. Now I want the UITextView to have a placeholder similar to the one you can set for an UITextField.<

相关标签:
30条回答
  • 2020-11-22 16:01

    First take a label in .h file.

    Here I take

    UILabel * lbl;
    

    Then in .m under viewDidLoad declare it

    lbl = [[UILabel alloc] initWithFrame:CGRectMake(8.0, 0.0,250, 34.0)];
    
    lbl.font=[UIFont systemFontOfSize:14.0];
    
    [lbl setText:@"Write a message..."];
    
    [lbl setBackgroundColor:[UIColor clearColor]];
    
    [lbl setTextColor:[UIColor lightGrayColor]];
    
    [textview addSubview:lbl];
    

    textview is my TextView.

    Now declare

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

    And your Textview placeholder is ready !

    0 讨论(0)
  • 2020-11-22 16:02

    Here's a way easier solution that behaves exactly like UITextField's placeholder but doesn't require drawing custom views, or resigning first responder.

    - (void) textViewDidChange:(UITextView *)textView{
    
        if (textView.text.length == 0){
            textView.textColor = [UIColor lightGrayColor];
            textView.text = placeholderText;
            [textView setSelectedRange:NSMakeRange(0, 0)];
            isPlaceholder = YES;
    
        } else if (isPlaceholder && ![textView.text isEqualToString:placeholderText]) {
            textView.text = [textView.text substringToIndex:1];
            textView.textColor = [UIColor blackColor];
            isPlaceholder = NO;
        }
    
    }
    

    (the second check in the else if statement is for the case where nothing is entered and the user presses backspace)

    Just set your class as a UITextViewDelegate. In viewDidLoad you should initialize like

    - (void) viewDidLoad{
        // initialize placeholder text
        placeholderText = @"some placeholder";
        isPlaceholder = YES;
        self.someTextView.text = placeholderText;
        self.someTextView.textColor = [UIColor lightGrayColor];
        [self.someTextView setSelectedRange:NSMakeRange(0, 0)];
    
        // assign UITextViewDelegate
        self.someTextView.delegate = self;
    }
    
    0 讨论(0)
  • 2020-11-22 16:02

    Sorry to add another answer, But I just pulled something like this off and this created the closest-to-UITextField kind of placeholder.

    Hope this helps someone.

    -(void)textViewDidChange:(UITextView *)textView{
        if(textView.textColor == [UIColor lightGrayColor]){
            textView.textColor  = [UIColor blackColor]; // look at the comment section in this answer
            textView.text       = [textView.text substringToIndex: 0];// look at the comment section in this answer
        }else if(textView.text.length == 0){
            textView.text       = @"This is some placeholder text.";
            textView.textColor  = [UIColor lightGrayColor];
            textView.selectedRange = NSMakeRange(0, 0);
        }
    }
    
    -(void)textViewDidChangeSelection:(UITextView *)textView{
        if(textView.textColor == [UIColor lightGrayColor] && (textView.selectedRange.location != 0 || textView.selectedRange.length != 0)){
            textView.selectedRange = NSMakeRange(0, 0);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 16:04

    I made a few minor modifications to bcd's solution to allow for initialization from a Xib file, text wrapping, and to maintain background color. Hopefully it will save others the trouble.

    UIPlaceHolderTextView.h:

    #import <Foundation/Foundation.h>
    IB_DESIGNABLE
    @interface UIPlaceHolderTextView : UITextView
    
    @property (nonatomic, retain) IBInspectable NSString *placeholder;
    @property (nonatomic, retain) IBInspectable UIColor *placeholderColor;
    
    -(void)textChanged:(NSNotification*)notification;
    
    @end
    

    UIPlaceHolderTextView.m:

    #import "UIPlaceHolderTextView.h"
    
    @interface UIPlaceHolderTextView ()
    
    @property (nonatomic, retain) UILabel *placeHolderLabel;
    
    @end
    
    @implementation UIPlaceHolderTextView
    
    CGFloat const UI_PLACEHOLDER_TEXT_CHANGED_ANIMATION_DURATION = 0.25;
    
    - (void)dealloc
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    #if __has_feature(objc_arc)
    #else
        [_placeHolderLabel release]; _placeHolderLabel = nil;
        [_placeholderColor release]; _placeholderColor = nil;
        [_placeholder release]; _placeholder = nil;
        [super dealloc];
    #endif
    }
    
    - (void)awakeFromNib
    {
        [super awakeFromNib];
    
        // Use Interface Builder User Defined Runtime Attributes to set
        // placeholder and placeholderColor in Interface Builder.
        if (!self.placeholder) {
            [self setPlaceholder:@""];
        }
    
        if (!self.placeholderColor) {
            [self setPlaceholderColor:[UIColor lightGrayColor]];
        }
    
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
    }
    
    - (id)initWithFrame:(CGRect)frame
    {
        if( (self = [super initWithFrame:frame]) )
        {
            [self setPlaceholder:@""];
            [self setPlaceholderColor:[UIColor lightGrayColor]];
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
        }
        return self;
    }
    
    - (void)textChanged:(NSNotification *)notification
    {
        if([[self placeholder] length] == 0)
        {
            return;
        }
    
        [UIView animateWithDuration:UI_PLACEHOLDER_TEXT_CHANGED_ANIMATION_DURATION animations:^{
        if([[self text] length] == 0)
        {
            [[self viewWithTag:999] setAlpha:1];
        }
        else
        {
            [[self viewWithTag:999] setAlpha:0];
        }
        }];
    }
    
    - (void)setText:(NSString *)text {
        [super setText:text];
        [self textChanged:nil];
    }
    
    - (void)drawRect:(CGRect)rect
    {
        if( [[self placeholder] length] > 0 )
        {
            if (_placeHolderLabel == nil )
            {
                _placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(8,8,self.bounds.size.width - 16,0)];
                _placeHolderLabel.lineBreakMode = NSLineBreakByWordWrapping;
                _placeHolderLabel.numberOfLines = 0;
                _placeHolderLabel.font = self.font;
                _placeHolderLabel.backgroundColor = [UIColor clearColor];
                _placeHolderLabel.textColor = self.placeholderColor;
                _placeHolderLabel.alpha = 0;
                _placeHolderLabel.tag = 999;
                [self addSubview:_placeHolderLabel];
            }
    
            _placeHolderLabel.text = self.placeholder;
            [_placeHolderLabel sizeToFit];
            [self sendSubviewToBack:_placeHolderLabel];
        }
    
        if( [[self text] length] == 0 && [[self placeholder] length] > 0 )
        {
            [[self viewWithTag:999] setAlpha:1];
        }
    
        [super drawRect:rect];
    }
    
    @end
    
    0 讨论(0)
  • 2020-11-22 16:05

    this is how I did it:

    UITextView2.h

    #import <UIKit/UIKit.h>
    
    @interface UITextView2 : UITextView <UITextViewDelegate> {
     NSString *placeholder;
     UIColor *placeholderColor;
    }
    
    @property(nonatomic, retain) NSString *placeholder;
    @property(nonatomic, retain) UIColor *placeholderColor;
    
    -(void)textChanged:(NSNotification*)notif;
    
    @end
    

    UITextView2.m

    @implementation UITextView2
    
    @synthesize placeholder, placeholderColor;
    
    - (id)initWithFrame:(CGRect)frame {
        if (self = [super initWithFrame:frame]) {
            [self setPlaceholder:@""];
            [self setPlaceholderColor:[UIColor lightGrayColor]];
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
        }
        return self;
    }
    
    -(void)textChanged:(NSNotification*)notif {
        if ([[self placeholder] length]==0)
            return;
        if ([[self text] length]==0) {
            [[self viewWithTag:999] setAlpha:1];
        } else {
            [[self viewWithTag:999] setAlpha:0];
        }
    
    }
    
    - (void)drawRect:(CGRect)rect {
        if ([[self placeholder] length]>0) {
            UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(8, 8, 0, 0)];
            [l setFont:self.font];
            [l setTextColor:self.placeholderColor];
            [l setText:self.placeholder];
            [l setAlpha:0];
            [l setTag:999];
            [self addSubview:l];
            [l sizeToFit];
            [self sendSubviewToBack:l];
            [l release];
        }
        if ([[self text] length]==0 && [[self placeholder] length]>0) {
            [[self viewWithTag:999] setAlpha:1];
        }
        [super drawRect:rect];
    }
    
    - (void)dealloc {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
        [super dealloc];
    }
    
    
    @end
    
    0 讨论(0)
  • 2020-11-22 16:06

    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];
        }
        }
    

    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 *)theTextView
    {
         if (![textView hasText]) {
         lbl.hidden = NO;
    }
    }
    
    - (void) textViewDidChange:(UITextView *)textView
    {
        if(![textView hasText]) {
          lbl.hidden = NO;
    }
    else{
        lbl.hidden = YES;
     }  
    }
    
    0 讨论(0)
提交回复
热议问题