UITextView 自定义占位文字

@interface BTPlaceholderTextView : UITextView
/**
 *  占位文字
 */
@property (nonatomic,strong) NSString *placeholder;

/**<#属性名#> */
@property(nonatomic,assign) CGFloat placeholderX;
/**<#属性名#> */
@property(nonatomic,assign) CGFloat placeholderY;
@end
#import "BTPlaceholderTextView.h"

@implementation BTPlaceholderTextView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        // 添加监听器,监听自己的文字改变通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:nil];
    }
    
    return self;
}
// 时刻监听文字键盘文字的变化,文字一旦改变便调用setNeedsDisplay方法
- (void)textDidChange
{
   
    // 该方法会调用drawRect:方法,立即重新绘制占位文字
    [self setNeedsDisplay];
}

// 占位文字的setter方法
- (void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = placeholder;
    // 文字一旦改变,立马重写绘制(内部会调drawRect:方法)
    [self setNeedsDisplay];
    
}
// 字体属性setter方法
- (void)setFont:(UIFont *)font
{
    [super setFont:font];
    [self setNeedsDisplay];
}

/**
 *  绘制占位文字
 */
- (void)drawRect:(CGRect)rect {
    
    // 如果一旦有输入文字,直接返回,不再绘制占位文字
    if (self.hasText) return;
    
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = self.font;
    attrs[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
    // 在textView的矩形框中绘制文字
    [self.placeholder drawInRect:CGRectMake(self.placeholderX + 5.0, self.placeholderY, self.frame.size.width, self.frame.size.height) withAttributes:attrs];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end

你可能感兴趣的:(UITextView 自定义占位文字)