UITextView添加占位文字简单好用的方法。主要运用drawRect:方法对占位文字进行绘制。
创建CustomPlaceholderTextView文件继承于UITextView,在CustomPlaceholderTextView.h 文件中有如下属性:
//占位文字
@property (nonatomic, copy) NSString *placeholder;
//占位文字颜色
@property (nonatomic, strong) UIColor *placeholderColor;
//占位文字大小
@property (nonatomic, assign) CGFloat placeholderFontSize;
//文字大小
@property (nonatomic, assign) CGFloat fontSize;
//提示文字距左距离
@property (nonatomic, assign) CGFloat text_x;
//提示文字距上距离
@property (nonatomic, assign) CGFloat text_y;
//圆角弧度
@property (nonatomic, assign) CGFloat cornerRadius;
//描边宽
@property (nonatomic, assign) CGFloat borderWidth;
//描边颜色
@property (nonatomic, strong) UIColor *borderColor;
在CustomPlaceholderTextView.m文件中实现占位文字的绘制。
初始化 textView 并进行一些默认设置:
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
//设置占位文字默认大小
self.placeholderFontSize = 15.0;
//设置占位文字默认颜色
self.placeholderColor = [UIColor grayColor];
//设置显示文字默认大小
self.font = [UIFont systemFontOfSize:17.0];
// 使用通知监听文字改变
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextViewTextDidChangeNotification object:self];
}
return self;
}
当文字发生改变的时候调用 textDidChange: 方法
- (void)textDidChange:(NSNotification *)note
{
// 会重新调用drawRect:方法
[self setNeedsDisplay];
}
调用drawRect:
/**
* 每次调用drawRect:方法,都会将以前画的东西清除掉
*/
- (void)drawRect:(CGRect)rect
{
// 如果有文字,就直接返回,不需要画占位文字
if (self.hasText) return;
// 属性
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = [UIFont systemFontOfSize:self.placeholderFontSize];
attrs[NSForegroundColorAttributeName] = self.placeholderColor;
// 画文字
rect.origin.x = self.text_x;
rect.origin.y = self.text_y;
rect.size.width -= 2 * rect.origin.x;
[self.placeholder drawInRect:rect withAttributes:attrs];
}
一些属性的 setter 方法:
//占位文字
- (void)setPlaceholder:(NSString *)placeholder
{
_placeholder = [placeholder copy];
[self setNeedsDisplay];
}
//占位文字颜色
- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
_placeholderColor = placeholderColor;
[self setNeedsDisplay];
}
//占位文字大小
- (void)setPlaceholderFontSize:(CGFloat)placeholderFontSize
{
_placeholderFontSize = placeholderFontSize;
[self setNeedsDisplay];
}
//textView 文字大小
- (void)setFontSize:(CGFloat)fontSize
{
_fontSize = fontSize;
self.font = [UIFont systemFontOfSize:fontSize];
}
//边框圆角弧度
- (void)setCornerRadius:(CGFloat)cornerRadius
{
_cornerRadius = cornerRadius;
self.layer.cornerRadius = cornerRadius;
}
//边框宽度
- (void)setBorderWidth:(CGFloat)borderWidth
{
_borderWidth = borderWidth;
self.layer.borderWidth = borderWidth;
}
//边框颜色
- (void)setBorderColor:(UIColor *)borderColor
{
_borderColor = borderColor;
self.layer.borderColor = borderColor.CGColor;
}
- (void)setText:(NSString *)text
{
[super setText:text];
[self setNeedsDisplay];
}
- (void)setAttributedText:(NSAttributedString *)attributedText
{
[super setAttributedText:attributedText];
[self setNeedsDisplay];
}
调用 layoutSubviews 方法:
- (void)layoutSubviews
{
[super layoutSubviews];
[self setNeedsDisplay];
}
调用 dealloc 方法,移除监听:
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
网友评论