自定义textview

1.首先创建一个继承与UITextView的ZDTextView

2.在#import "ZDTextView.h"文件里面创建

@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]; //给定一个默认字体大小

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self]; //通知:监听文字的改变的状态

}

return self;

}

//实现监听方法

#pragma mark -监听文字改变

- (void)textDidChange {

self.placeholderLabel.hidden = self.hasText;

}


/**

//设置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];

//设置文字

self.placeholderLabel.text= myPlaceholder;

//重新计算子控件frame

[self setNeedsLayout];

}

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

_myPlaceholderColor= myPlaceholderColor;

//设置颜色

self.placeholderLabel.textColor= myPlaceholderColor;

}

//重写这个set方法保持font一致

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

[super setFont:font];

self.placeholderLabel.font= font;

//重新计算子控件frame的大小位置

[self setNeedsLayout];

}

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

[super setText:text];

[self textDidChange]; // UITextViewTextDidChangeNotification 通知的回调方法

}

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

[super setAttributedText:attributedText];

[self textDidChange]; //这里调用的就是UITextViewTextDidChangeNotification 通知的回调

}

//操作完成销毁通知

- (void)dealloc{

[[NSNotificationCenter defaultCenter]removeObserver:UITextViewTextDidChangeNotification];

}



最后我们在使用的时候直接导入#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=@"请输入您的建议或意见";

sugesttextview.editable=YES;

sugesttextview.myPlaceholderColor=[UIColor colorWithHexString:@"#999999"];//改变字体颜色

sugesttextview.placeholderLabel.font=[UIFont systemFontOfSize:mytextfont];//设置字体大小

[self addSubview:sugesttextview];

你可能感兴趣的:(自定义textview)