博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UITextView添加占位字符
阅读量:4079 次
发布时间:2019-05-25

本文共 2279 字,大约阅读时间需要 7 分钟。

#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

转载地址:http://tjsni.baihongyu.com/

你可能感兴趣的文章
程序员最核心的竞争力是什么?
查看>>
linux CPU个数查看
查看>>
消息队列设计精要
查看>>
分布式存储系统设计(1)—— 系统架构
查看>>
MySQL数据库的高可用方案总结
查看>>
常用排序算法总结(一) 比较算法总结
查看>>
SSH原理与运用
查看>>
SIGN UP BEC2
查看>>
出现( linker command failed with exit code 1)错误总结
查看>>
iOS开发中一些常见的并行处理
查看>>
iOS获取手机的Mac地址
查看>>
ios7.1发布企业证书测试包的问题
查看>>
iOS 开发百问
查看>>
Mac环境下svn的使用
查看>>
github简单使用教程
查看>>
如何高效利用GitHub
查看>>
环境分支-git版本管理
查看>>
Spring AOP + Redis + 注解实现redis 分布式锁
查看>>
支付宝生活号服务号 用户信息获取 oauth2 登录对接 springboot java
查看>>
CodeForces #196(Div. 2) 337D Book of Evil (树形dp)
查看>>