iOS占位文字placeholder设置技巧

来源:互联网 发布:中传在线 网络教育 编辑:程序博客网 时间:2024/06/05 09:56


@interface BSPublishTextView : UITextView

/** 对外属性占位字符 placeholder */

@property (nonatomic, copy) NSString *placeholder;

 

/** 对外属性占位符颜色 */

@property (nonatomic, strong) UIColor *placeholderColor;

@end

@interface BSPublishTextView ()

/** 占位字符 label */

@property (nonatomic, weak) UILabel *placeholderLabel;

@end

 

@implementation BSPublishTextView

- (UILabel *)placeholderLabel{

    if (!_placeholderLabel) {

        UILabel *placeholderLabel = [[UILabel alloc]init];

        placeholderLabel.numberOfLines = 0;

     CGRect frame = placeholderLabel.frame;

        frame.origin.x = 4;

        frame.origin.y = 7;

        placeholderLabel.frame = frame;

        [self addSubview:placeholderLabel];

        _placeholderLabel = placeholderLabel;

    }

    return _placeholderLabel;

}

 

- (instancetype)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // 永久垂直弹簧效果

        self.alwaysBounceVertical = YES;

        self.font = [UIFont systemFontOfSize:15];

        self.placeholderColor = [UIColor grayColor];

     

        // 注册textView文字发生改变通知  UITextViewTextDidChangeNotification

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

    }

    return self;

}

 

//通知调用方法

- (void)textChange{

    self.placeholderLabel.hidden = self.hasText;

}

 

- (void)dealloc{

    [[NSNotificationCenter defaultCenter]removeObserver:self];

}

 

- (void)layoutSubviews{

    [super layoutSubviews];

    //在 layoutSubviews 拿到 UITextView的宽度一定是正确的 ,这里如果外界即使更改了UITextView的宽度这里也是能算正确的

    //先设置placeholderLabel占位字符的宽度

    CGRect frame = self.placeholderLabel.frame;

    frame.size.width = self.frame.size.width - 2 * self.placeholderLabel.frame.origin.x;

    self.placeholderLabel.frame = frame;

 

    // placeholderLabel占位字符的大小size随里面的内容大小而变化

    [self.placeholderLabel sizeToFit];

}

 

// setNeedsDisplay方法 : 会在恰当的时刻自动调用drawRect:方法

// setNeedsLayout方法 : 会在恰当的时刻调用layoutSubviews方法

 

- (void)setPlaceholderColor:(UIColor *)placeholderColor{

    _placeholderColor = placeholderColor;

    self.placeholderLabel.textColor = placeholderColor;

}

 

- (void)setPlaceholder:(NSString *)placeholder{

    _placeholder = [placeholder copy];

    self.placeholderLabel.text = placeholder;

    [self setNeedsLayout]; //会在恰当的时刻调用layoutSubviews方法 这句代码可不写

}

 

- (void)setFont:(UIFont *)font{

    [super setFont:font];

    self.placeholderLabel.font = font;

    [self setNeedsLayout];  //会在恰当的时刻调用layoutSubviews方法 这句代码可不写

}

 

- (void)setText:(NSString *)text{

    [super setText:text];

    [self textChange];

}

 

- (void)setAttributedText:(NSAttributedString *)attributedText{

    [super setAttributedText:attributedText];

    [self textChange];

}

@end

 

@interface BSPublishTextView : UITextView

/** 对外属性占位字符 placeholder */

@property (nonatomic, copy) NSString *placeholder;

 

/** 对外属性占位符颜色 */

@property (nonatomic, strong) UIColor *placeholderColor;

@end

@interface BSPublishTextView ()

/** 占位字符 label */

@property (nonatomic, weak) UILabel *placeholderLabel;

@end

 

@implementation BSPublishTextView

- (UILabel *)placeholderLabel{

    if (!_placeholderLabel) {

        UILabel *placeholderLabel = [[UILabel alloc]init];

        placeholderLabel.numberOfLines = 0;

     CGRect frame = placeholderLabel.frame;

        frame.origin.x = 4;

        frame.origin.y = 7;

        placeholderLabel.frame = frame;

        [self addSubview:placeholderLabel];

        _placeholderLabel = placeholderLabel;

    }

    return _placeholderLabel;

}

 

- (instancetype)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // 永久垂直弹簧效果

        self.alwaysBounceVertical = YES;

        self.font = [UIFont systemFontOfSize:15];

        self.placeholderColor = [UIColor grayColor];

     

        // 注册textView文字发生改变通知  UITextViewTextDidChangeNotification

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

    }

    return self;

}

 

//通知调用方法

- (void)textChange{

    self.placeholderLabel.hidden = self.hasText;

}

 

- (void)dealloc{

    [[NSNotificationCenter defaultCenter]removeObserver:self];

}

 

- (void)layoutSubviews{

    [super layoutSubviews];

    //在 layoutSubviews 拿到 UITextView的宽度一定是正确的 ,这里如果外界即使更改了UITextView的宽度这里也是能算正确的

    //先设置placeholderLabel占位字符的宽度

    CGRect frame = self.placeholderLabel.frame;

    frame.size.width = self.frame.size.width - 2 * self.placeholderLabel.frame.origin.x;

    self.placeholderLabel.frame = frame;

 

    // placeholderLabel占位字符的大小size随里面的内容大小而变化

    [self.placeholderLabel sizeToFit];

}

 

// setNeedsDisplay方法 : 会在恰当的时刻自动调用drawRect:方法

// setNeedsLayout方法 : 会在恰当的时刻调用layoutSubviews方法

 

- (void)setPlaceholderColor:(UIColor *)placeholderColor{

    _placeholderColor = placeholderColor;

    self.placeholderLabel.textColor = placeholderColor;

}

 

- (void)setPlaceholder:(NSString *)placeholder{

    _placeholder = [placeholder copy];

    self.placeholderLabel.text = placeholder;

    [self setNeedsLayout]; //会在恰当的时刻调用layoutSubviews方法 这句代码可不写

}

 

- (void)setFont:(UIFont *)font{

    [super setFont:font];

    self.placeholderLabel.font = font;

    [self setNeedsLayout];  //会在恰当的时刻调用layoutSubviews方法 这句代码可不写

}

 

- (void)setText:(NSString *)text{

    [super setText:text];

    [self textChange];

}

 

- (void)setAttributedText:(NSAttributedString *)attributedText{

    [super setAttributedText:attributedText];

    [self textChange];

}

@end