iOS之UITextView实现placeHolder占位文字的最佳方法
来源:互联网 发布:js插件的写法 编辑:程序博客网 时间:2024/06/05 14:07
在iOS开发中,UITextField和UITextView是最常用的文本输入类和文本展示类的控件。不同的是,UITextField中有一个placeholder属性,可以设置UITextField的占位文字,可是,Apple没有给UITextView公开提供一个类似于placeholder这样的属性来供开发者使用。但是,通过Runtime,我们发现,UITextView内部有个“_placeHolderLabel”的私有成员变量。这种方法有一定的风险,而且可移植性不强。
笔者参考了各种方法,推荐下面自定义的UITextView,不仅可移植性强、而且拓展性好,还可以随时修改自定义属性的值,我们只需要自定义一个UITextView,就可以一劳永逸了。
.h文件
@interface UITextViewPlaceholder : UITextView
/* 占位文字 /
@property (copy, nonatomic) NSString *placeholder;
/* 占位文字颜色 /
@property (strong, nonatomic) UIColor *placeholderColor;
@end
.m文件
@implementation UITextViewPlaceholder
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// 设置默认字体
self.font = [UIFont systemFontOfSize:14];
// 设置默认颜色
self.placeholderColor = [UIColor lightGrayColor];
// 使用通知监听文字改变
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextViewTextDidChangeNotification object:self];
}
return self;
}
- (void)textDidChange:(NSNotification *)note {
// 会重新调用drawRect:方法
[self setNeedsDisplay];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
// 每次调用drawRect:方法,都会将以前画的东西清除掉
- (void)drawRect:(CGRect)rect {
// 如果有文字,就直接返回,不需要画占位文字
if (self.hasText) return;
// 属性
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = self.font;
attrs[NSForegroundColorAttributeName] = self.placeholderColor;
// 画文字
rect.origin.x = 5;
rect.origin.y = 8;
rect.size.width -= 2 * rect.origin.x;
[self.placeholder drawInRect:rect withAttributes:attrs];
}
- (void)layoutSubviews {
[super layoutSubviews];
[self setNeedsDisplay];
}
//#pragma mark - setter
- (void)setPlaceholder:(NSString *)placeholder {
_placeholder = [placeholder copy];
[self setNeedsDisplay];
}
- (void)setPlaceholderColor:(UIColor *)placeholderColor {
_placeholderColor = placeholderColor;
[self setNeedsDisplay];
}
- (void)setFont:(UIFont *)font {
[super setFont:font];
[self setNeedsDisplay];
}
- (void)setText:(NSString *)text {
[super setText:text];
[self setNeedsDisplay];
}
- (void)setAttributedText:(NSAttributedString *)attributedText {
[super setAttributedText:attributedText];
[self setNeedsDisplay];
}
@end
- iOS之UITextView实现placeHolder占位文字的最佳方法
- iOS 实现 UITextView placeHolder的占位文字
- 史上最全的iOS之UITextView实现placeHolder占位文字的N种方法
- 史上最全的iOS之UITextView实现placeHolder占位文字的N种方法
- iOS UITextView的PlaceHolder 占位文字的实现
- iOS 史上最全的 UITextView 实现 placeHolder 占位文字 的N种方法
- iOS开发:UITextView实现placeHolder占位文字的N种方法
- iOS-UITextView占位文字placeholder
- UITextView的Placeholder占位文字
- iOS之UITextView 实现占位符占位文字的Ñ种方法
- ios中实现带占位符placeholder的UITextview
- iOS UITextView 实现placeholder的方法
- iOS 实现UITextView的placeholder
- UITextView 实现placeholder的方法
- UITextView 实现placeholder的方法
- UITextView 实现placeholder的方法
- UITextView 实现placeholder的方法
- UITextView 实现placeholder的方法
- Win10 如何卸载重装MySQL
- c++:分析智能指针与发展历史
- c++ socket调用gethostbyname()失败
- mysql inndb MyISAM分析
- 反转单链表的方法
- iOS之UITextView实现placeHolder占位文字的最佳方法
- 回顾一下web
- 工欲善其事,必先利其器-java高级测试进阶之接口自动化测试工具类(一)处理http请求。
- Mysql cannot allocate memory for the buffer pool 解决方法
- 区块链和物联网结合的挑战和前景
- 《Spring实战 第4版》的阅读笔记 -----且行且珍惜
- android gradle + junit + jacoco 集成jenkins,sonar系统
- 轻松理解 Android Binder,只需要读这一篇
- 完美解决listView滚动条隐藏的问题!