1. 程式人生 > >程式碼待封裝的textview

程式碼待封裝的textview

@interface HATextView : UITextView

@property(nonatomic,copy) NSString *myPlaceholder;  //文字

 

@property(nonatomic,strong) UIColor *myPlaceholderColor; 

@end

@interface HATextView()

 

@property (nonatomic,weak) UILabel *placeholderLabel;

@end

 

 

@implementation HATextView

 

- (instancetype)initWithFrame:(CGRect)frame{

    

    self = [super initWithFrame:frame];

    

    if(self) {

        

        self.backgroundColor= [UIColor clearColor];

        

        UILabel *placeholderLabel = [[UILabel alloc]init];//新增一個佔位label

        placeholderLabel.font = [UIFont systemFontOfSize:14];

        placeholderLabel.backgroundColor = [UIColor clearColor];

        

        placeholderLabel.numberOfLines = 0; //設定可以輸入多行文字時可以自動換行

        

        [self addSubview:placeholderLabel];

        

        self.placeholderLabel= placeholderLabel; //賦值儲存

        

        self.myPlaceholderColor= [UIColor lightGrayColor]; //設定佔位文字預設顏色

        

        self.font= [UIFont systemFontOfSize:14]; //設定預設的字型

        

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self]; //通知:監聽文字的改變

     }

    return self;

}

 

- (void)textDidChange {

    

    self.placeholderLabel.hidden = self.hasText;

    

}

 

- (void)layoutSubviews{

    

    [super layoutSubviews];

    

    self.placeholderLabel.frame = CGRectMake(8, 6, 200, 16);

    

}

 

- (void)setMyPlaceholder:(NSString*)myPlaceholder{

    

    _myPlaceholder= [myPlaceholder copy];

    

    //設定文字

    

    self.placeholderLabel.text = myPlaceholder;

    

    //重新計運算元控制元件frame

    

    [self setNeedsLayout];

    

}

- (void)setMyPlaceholderColor:(UIColor*)myPlaceholderColor{

    

    _myPlaceholderColor= myPlaceholderColor;

    

    self.placeholderLabel.textColor= myPlaceholderColor;

    

}

 

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

    

    [super setFont:font];

    

    self.placeholderLabel.font= font;

    

    //重新計運算元控制元件frame

    

    [self setNeedsLayout];

    

}

 

- (void)dealloc{

    [[NSNotificationCenter defaultCenter]removeObserver:UITextViewTextDidChangeNotification];

}

 

- (void)awakeFromNib{

    [super awakeFromNib];

    self.backgroundColor= [UIColor clearColor];

    

    UILabel *placeholderLabel = [[UILabel alloc]init];//新增一個佔位label

    placeholderLabel.font = [UIFont systemFontOfSize:14];

    placeholderLabel.backgroundColor = [UIColor clearColor];

    

    placeholderLabel.numberOfLines = 0; //設定可以輸入多行文字時可以自動換行

    

    [self addSubview:placeholderLabel];

    

    self.placeholderLabel= placeholderLabel; //賦值儲存

    

    self.myPlaceholderColor= [UIColor lightGrayColor]; //設定佔位文字預設顏色

    

    self.font= [UIFont systemFontOfSize:14]; //設定預設的字型

    

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self]; //通知:監聽文字的改變

    

}