TextView自定义

来源:互联网 发布:济宁知豆电动汽车电话 编辑:程序博客网 时间:2024/05/20 18:20
#import <UIKit/UIKit.h>@interface HMTextView : UITextView@property (nonatomic, copy) NSString *placehoder;@property (nonatomic, strong) UIColor *placehoderColor;@end
#import "HMTextView.h"@interface HMTextView() <UITextViewDelegate>@property (nonatomic, weak) UILabel *placehoderLabel;@end@implementation HMTextView- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        self.backgroundColor = [UIColor clearColor];        // 添加一个显示提醒文字的label(显示占位文字的label)        UILabel *placehoderLabel = [[UILabel alloc] init];        placehoderLabel.numberOfLines = 0;        placehoderLabel.backgroundColor = [UIColor clearColor];        [self addSubview:placehoderLabel];        self.placehoderLabel = placehoderLabel;        // 设置默认的占位文字颜色        self.placehoderColor = [UIColor lightGrayColor];        // 设置默认的字体        self.font = [UIFont systemFontOfSize:14];#warning 不要设置自己的代理为自己本身        // 监听内部文字改变//        self.delegate = self;        /**         监听控件的事件:         1.delegate         2.- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;         3.通知         */        // 当用户通过键盘修改了self的文字,self就会自动发出一个UITextViewTextDidChangeNotification通知        // 一旦发出上面的通知,就会调用self的textDidChange方法        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];    }    return self;}- (void)dealloc{    [[NSNotificationCenter defaultCenter] removeObserver:self];}#pragma mark - 监听文字改变- (void)textDidChange{//    if (self.text.length == 0) { // 显示占位文字//        self.placehoderLabel.hidden = NO;//    } else { // 隐藏占位文字//        self.placehoderLabel.hidden = YES;//    }// text:属性 包含普通的文字字符串// attributedText:只要显示在textview里面所有的内容(表情,text)    self.placehoderLabel.hidden = (self.attributedText.length != 0);}// 重写方法-(void)setAttributedText:(NSAttributedString *)attributedText {[super setAttributedText:attributedText];[self textDidChange];}#pragma mark - 公共方法- (void)setText:(NSString *)text{    [super setText:text];    [self textDidChange];}- (void)setPlacehoder:(NSString *)placehoder{#warning 如果是copy策略,setter最好这么写    _placehoder = [placehoder copy];    // 设置文字    self.placehoderLabel.text = placehoder;    // 重新计算子控件的fame    [self setNeedsLayout];}- (void)setPlacehoderColor:(UIColor *)placehoderColor{    _placehoderColor = placehoderColor;    // 设置颜色    self.placehoderLabel.textColor = placehoderColor;}- (void)setFont:(UIFont *)font{    [super setFont:font];    self.placehoderLabel.font = font;    // 重新计算子控件的fame    [self setNeedsLayout];}- (void)layoutSubviews{    [super layoutSubviews];    self.placehoderLabel.y = 8;    self.placehoderLabel.x = 5;    self.placehoderLabel.width = self.width - 2 * self.placehoderLabel.x;    // 根据文字计算label的高度    CGSize maxSize = CGSizeMake(self.placehoderLabel.width, MAXFLOAT);    CGSize placehoderSize = [self.placehoder sizeWithFont:self.placehoderLabel.font constrainedToSize:maxSize];    self.placehoderLabel.height = placehoderSize.height;}@end

使用

// 添加输入控件- (void)setupTextView{    // 1.创建输入控件    HMTextView *textView = [[HMTextView alloc] init];    textView.frame = self.view.bounds;    [self.view addSubview:textView];    self.textView = textView;    // 2.设置提醒文字(占位文字)    textView.placehoder = @"分享新鲜事...";    // 3.设置字体    textView.font = [UIFont systemFontOfSize:15];}
1 0
原创粉丝点击