uitextview的占位字

占坑

 

 

#import <UIKit/UIKit.h>

 

@interface LRFeedBackTextView : IQTextView

 

/**placeholder占位文字*/

@property (nonatomic, copy) NSString *placeholder;

/**placeholderColor占位文字颜色*/

@property (nonatomic, strong) UIColor *placeholderColor;

 

@end

 

 

 

#import "LRFeedBackTextView.h"

@interface LRFeedBackTextView()

 

/**UILabel*/

@property (nonatomic, strong) UILabel *placeholderLabel;

@end

 

@implementation LRFeedBackTextView

 

 

/**

 *  懒加载属性,并设置属性的值

 */

-(UILabel *)placeholderLabel

{

    if (!_placeholderLabel) {

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

        label.font = [UIFont systemFontOfSize:14];

        label.textColor = [UIColor grayColor];

        label.numberOfLines = 0;

        [self addSubview:label];

        _placeholderLabel = label;

    }

    return _placeholderLabel;

}

 

/**

 *  设置自己的属性

 */

-(instancetype)initWithFrame:(CGRect)frame

{

    if (self = [super initWithFrame:frame]) {

        self.alwaysBounceVertical = YES;

        self.textColor = [UIColor blackColor];

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

        

    }

    return self;

}

 

- (void)awakeFromNib{

 

}

 

- (instancetype)initWithCoder:(NSCoder *)aDecoder{

    if (self = [super initWithCoder:aDecoder]) {

        self.scrollEnabled = NO;

        self.alwaysBounceVertical = YES;

        self.textColor = [UIColor blackColor];

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

    }

    return self;

}

 

- (void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

 

/**

 *  监听有文字输入

 */

-(void)texting

{

    [self setPlaceholderTextShow];

}

/**

 *  设置占位文字的显示

 */

-(void)setPlaceholderTextShow

{

    self.placeholderLabel.hidden = self.hasText;

}

 

-(void)layoutSubviews

{

    [super layoutSubviews];

    

    self.placeholderLabel.frame = CGRectMake(4, 8, SCREEN_WIDTH - 16, 0);

 

    [self.placeholderLabel sizeToFit];//这一步很重要,不能遗忘

}

 

-(void)setPlaceholder:(NSString *)placeholder

{

    placeholder = placeholder;

    self.placeholderLabel.text = placeholder;

    [self setNeedsLayout];

}

 

-(void)setPlaceholderColor:(UIColor *)placeholderColor

{

    self.placeholderLabel.textColor = placeholderColor;

    [self setNeedsLayout];

}

 

-(void)setFont:(UIFont *)font

{

    [super setFont:font];

    self.placeholderLabel.font = font;

    [self setNeedsLayout];

}

 

-(void)setText:(NSString *)text

{

    [super setText:text];

    [self setPlaceholderTextShow];

}

 

-(void)setAttributedText:(NSAttributedString *)attributedText

{

    [super setAttributedText:attributedText];

    [self setPlaceholderTextShow];

}

转载于:https://www.cnblogs.com/lazVy/p/5192216.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值