silence_0313的博客

闲下来,多整理

UITextView添加占位字符
#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

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/silence_0313/article/details/49924595
个人分类: iOS
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

UITextView添加占位字符

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭