1.首先創(chuàng)建一個(gè)繼承與UITextView的ZDTextView
2.在#import "ZDTextView.h"文件里面創(chuàng)建
@property(nonatomic,copy) NSString *myPlaceholder; ?//文字
@property(nonatomic,strong) UIColor *myPlaceholderColor; //文字顏色
@property ?UILabel *placeholderLabel;
3.#import "ZDTextView.m"
//重寫初始化方法
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if(self) {
self.backgroundColor= [UIColor clearColor];
UILabel *placeholderLabell = [[UILabel alloc]init];//占位label
placeholderLabell.backgroundColor= [UIColor clearColor];
placeholderLabell.numberOfLines=0; //多行顯示
[self addSubview:placeholderLabell];
self.placeholderLabel= placeholderLabell; //賦值
self.myPlaceholderColor= [UIColor lightGrayColor]; //字體顏色
self.font= [UIFont systemFontOfSize:15]; //給定一個(gè)默認(rèn)字體大小
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self]; //通知:監(jiān)聽文字的改變的狀態(tài)
}
return self;
}
//實(shí)現(xiàn)監(jiān)聽方法
#pragma mark -監(jiān)聽文字改變
- (void)textDidChange {
self.placeholderLabel.hidden = self.hasText;
}
/**
//設(shè)置lable的顯示位置
*/
- (void)layoutSubviews{
[super layoutSubviews];
self.placeholderLabel.mj_y=8;
self.placeholderLabel.mj_x=5;
self.placeholderLabel.mj_w=self.mj_w-self.placeholderLabel.mj_x*2.0;
CGSize maxSize =CGSizeMake(self.placeholderLabel.mj_w,MAXFLOAT);
self.placeholderLabel.mj_h= [self.myPlaceholder boundingRectWithSize:maxSize options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.placeholderLabel.font} context:nil].size.height;
}
/**
重新賦值
@param myPlaceholder 重新賦值
*/
- (void)setMyPlaceholder:(NSString*)myPlaceholder{
_myPlaceholder= [myPlaceholder copy];
//設(shè)置文字
self.placeholderLabel.text= myPlaceholder;
//重新計(jì)算子控件frame
[self setNeedsLayout];
}
- (void)setMyPlaceholderColor:(UIColor*)myPlaceholderColor{
_myPlaceholderColor= myPlaceholderColor;
//設(shè)置顏色
self.placeholderLabel.textColor= myPlaceholderColor;
}
//重寫這個(gè)set方法保持font一致
- (void)setFont:(UIFont*)font{
[super setFont:font];
self.placeholderLabel.font= font;
//重新計(jì)算子控件frame的大小位置
[self setNeedsLayout];
}
- (void)setText:(NSString*)text{
[super setText:text];
[self textDidChange]; // UITextViewTextDidChangeNotification 通知的回調(diào)方法
}
- (void)setAttributedText:(NSAttributedString*)attributedText{
[super setAttributedText:attributedText];
[self textDidChange]; //這里調(diào)用的就是UITextViewTextDidChangeNotification 通知的回調(diào)
}
//操作完成銷毀通知
- (void)dealloc{
[[NSNotificationCenter defaultCenter]removeObserver:UITextViewTextDidChangeNotification];
}
最后我們?cè)谑褂玫臅r(shí)候直接導(dǎo)入#import "ZDTextView.h"
ZDTextView *sugesttextview=[[ZDTextView alloc]initWithFrame:CGRectMake(ScaleX(15), suglable.frame.origin.y+suglable.frame.size.height, SIZE_WIDTH-ScaleX(30), ScaleX(140))];//適配ZDTextView,這里你可以使用自己的方式
sugesttextview.backgroundColor=[UIColor colorWithHexString:@"#efefef"];//改變背景顏色
sugesttextview.myPlaceholder=@"請(qǐng)輸入您的建議或意見";
sugesttextview.editable=YES;
sugesttextview.myPlaceholderColor=[UIColor colorWithHexString:@"#999999"];//改變字體顏色
sugesttextview.placeholderLabel.font=[UIFont systemFontOfSize:mytextfont];//設(shè)置字體大小
[self addSubview:sugesttextview];