UITextView添加占位字符
来源:互联网 发布:c语言与或运算 编辑:程序博客网 时间:2024/05/18 13:08
#import "YGTextView.h"
#define kPlaceholderDefaultTopMargin 4 // Placeholder上下间距
#define kPlaceholderDefaultLeftMargin 5 // Placeholder左右间距
@implementation YGTextView
#pragma mark - 初始化PlaceholderTextView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.opaque = YES;
// 设置通知, 当TextView文字发生改变时, 向自己发送通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChanged) name:UITextViewTextDidChangeNotification object:self];
}
return self;
}
#pragma mark - 通知事件
- (void)textDidChanged
{
// 重新绘制
[self setNeedsDisplay];
}
#pragma mark - 重写属性方法, 实时绘制
- (void)setPlaceholder:(NSString *)placeholder
{
_placeholder = [placeholder copy];
[self setNeedsDisplay];
}
- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
_placeholderColor = placeholderColor;
[self setNeedsDisplay];
}
- (void)setText:(NSString *)text
{
[super setText:text]; // 系统自带属性
[self setNeedsDisplay];
}
- (void)setAttributedText:(NSAttributedString *)attributedText
{
[super setAttributedText:attributedText];
[self setNeedsDisplay];
}
- (void)setFont:(UIFont *)font
{
[super setFont:font];
// 会在下一个消息循环调用drawRect
[self setNeedsDisplay];
}
#pragma mark - 绘制子控件
- (void)drawRect:(CGRect)rect
{
// 若TextView有文字
if (self.hasText)
{
return;
}
// 文字属性
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = self.font; // 获取当前PlaceholderTextView的文字属性
attrs[NSForegroundColorAttributeName] = self.placeholderColor ? self.placeholderColor : [UIColor lightGrayColor];
// 绘制范围
CGFloat placeholderTopMargin = self.placeholderTopMargin ? self.placeholderTopMargin : kPlaceholderDefaultTopMargin;
CGFloat placeholderLeftMargin = self.placeholderLeftMargin ? self.placeholderLeftMargin : kPlaceholderDefaultTopMargin;
CGFloat placeholderX = placeholderTopMargin;
CGFloat placeholderY = placeholderLeftMargin;
CGFloat placeholderW = rect.size.width - 2 * placeholderLeftMargin;
CGFloat placeholderH = rect.size.height - 2 * placeholderTopMargin;
CGRect placeholderRect = CGRectMake(placeholderX, placeholderY, placeholderW, placeholderH);
[self.placeholder drawInRect:placeholderRect withAttributes:attrs];
}
#pragma mark - 移除通知
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
#define kPlaceholderDefaultTopMargin 4 // Placeholder上下间距
#define kPlaceholderDefaultLeftMargin 5 // Placeholder左右间距
@implementation YGTextView
#pragma mark - 初始化PlaceholderTextView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.opaque = YES;
// 设置通知, 当TextView文字发生改变时, 向自己发送通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChanged) name:UITextViewTextDidChangeNotification object:self];
}
return self;
}
#pragma mark - 通知事件
- (void)textDidChanged
{
// 重新绘制
[self setNeedsDisplay];
}
#pragma mark - 重写属性方法, 实时绘制
- (void)setPlaceholder:(NSString *)placeholder
{
_placeholder = [placeholder copy];
[self setNeedsDisplay];
}
- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
_placeholderColor = placeholderColor;
[self setNeedsDisplay];
}
- (void)setText:(NSString *)text
{
[super setText:text]; // 系统自带属性
[self setNeedsDisplay];
}
- (void)setAttributedText:(NSAttributedString *)attributedText
{
[super setAttributedText:attributedText];
[self setNeedsDisplay];
}
- (void)setFont:(UIFont *)font
{
[super setFont:font];
// 会在下一个消息循环调用drawRect
[self setNeedsDisplay];
}
#pragma mark - 绘制子控件
- (void)drawRect:(CGRect)rect
{
// 若TextView有文字
if (self.hasText)
{
return;
}
// 文字属性
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = self.font; // 获取当前PlaceholderTextView的文字属性
attrs[NSForegroundColorAttributeName] = self.placeholderColor ? self.placeholderColor : [UIColor lightGrayColor];
// 绘制范围
CGFloat placeholderTopMargin = self.placeholderTopMargin ? self.placeholderTopMargin : kPlaceholderDefaultTopMargin;
CGFloat placeholderLeftMargin = self.placeholderLeftMargin ? self.placeholderLeftMargin : kPlaceholderDefaultTopMargin;
CGFloat placeholderX = placeholderTopMargin;
CGFloat placeholderY = placeholderLeftMargin;
CGFloat placeholderW = rect.size.width - 2 * placeholderLeftMargin;
CGFloat placeholderH = rect.size.height - 2 * placeholderTopMargin;
CGRect placeholderRect = CGRectMake(placeholderX, placeholderY, placeholderW, placeholderH);
[self.placeholder drawInRect:placeholderRect withAttributes:attrs];
}
#pragma mark - 移除通知
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
0 0
- UITextView添加占位字符
- UITextView添加占位符
- iOS疯狂详解之UITextView加上占位字符
- iOS 怎么给UITextView添加占位符文字
- [Swift]iOS 开发:给 UITextView 添加占位符的方法
- UITextView实现占位文字
- UITextView的Placeholder占位文字
- iOS-UITextView占位文字placeholder
- 扩展UITextView占位符和清空控能
- iOS 实现 UITextView placeHolder的占位文字
- UITextView添加背景图
- UITextView 添加placeholder
- UITextView 添加 pleaceholder
- UITextView添加Placeholder(swift)
- UITextView添加placeholder属性
- 给UITextView添加PlaceHolder
- UITextView添加背景图片
- UITextView添加planceholder
- 错误
- JSON的parse()和stringfy()方法
- 关于正则表达式 g,m 参数的总结
- 校验xml的合法性
- CentOS配置163的yum源
- UITextView添加占位字符
- Android基础入门教程——10.3 AudioManager(音频管理器)
- Python-安装模块
- MFC滚动条设置的小研究,解决R6010错误和无法显示图片全部等错误
- 前端 json字符串转换成json对象
- 资源国际化
- JS事件监听器
- checkbox android:button="@null" 默认checkbox icon仍占位问题
- SSH:Struts1框架(MVC讲解)