欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

UITextView 添加占位提示文字

程序员文章站 2022-06-01 14:01:52
...

UITextView添加占位文字简单好用的方法。主要运用drawRect:方法对占位文字进行绘制。
创建CustomPlaceholderTextView文件继承于UITextView,在CustomPlaceholderTextView.h 文件中有如下属性:

//占位文字
@property (nonatomic, copy) NSString *placeholder;
//占位文字颜色
@property (nonatomic, strong) UIColor *placeholderColor;
//占位文字大小
@property (nonatomic, assign) CGFloat placeholderFontSize;
//文字大小
@property (nonatomic, assign) CGFloat fontSize;
//提示文字距左距离
@property (nonatomic, assign) CGFloat text_x;
//提示文字距上距离
@property (nonatomic, assign) CGFloat text_y;
//圆角弧度
@property (nonatomic, assign) CGFloat cornerRadius;
//描边宽
@property (nonatomic, assign) CGFloat borderWidth;
//描边颜色
@property (nonatomic, strong) UIColor *borderColor;

在CustomPlaceholderTextView.m文件中实现占位文字的绘制。
初始化 textView 并进行一些默认设置:

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        //设置占位文字默认大小
        self.placeholderFontSize = 15.0;
        //设置占位文字默认颜色
        self.placeholderColor = [UIColor grayColor];
        //设置显示文字默认大小
        self.font = [UIFont systemFontOfSize:17.0];
        // 使用通知监听文字改变
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextViewTextDidChangeNotification object:self];
    }
    return self;
}

当文字发生改变的时候调用 textDidChange: 方法:

- (void)textDidChange:(NSNotification *)note
{
    // 会重新调用drawRect:方法
    [self setNeedsDisplay];
}

调用drawRect:

/**
 * 每次调用drawRect:方法,都会将以前画的东西清除掉
 */
- (void)drawRect:(CGRect)rect
{
    // 如果有文字,就直接返回,不需要画占位文字
    if (self.hasText) return;

    // 属性
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];

    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:self.placeholderFontSize];
    attrs[NSForegroundColorAttributeName] = self.placeholderColor;

    // 画文字
    rect.origin.x = self.text_x;
    rect.origin.y = self.text_y;
    rect.size.width -= 2 * rect.origin.x;
    [self.placeholder drawInRect:rect withAttributes:attrs];
}

一些属性的 setter 方法:

//占位文字
- (void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = [placeholder copy];
    [self setNeedsDisplay];
}

//占位文字颜色
- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
    _placeholderColor = placeholderColor;
    [self setNeedsDisplay];
}

//占位文字大小
- (void)setPlaceholderFontSize:(CGFloat)placeholderFontSize
{
    _placeholderFontSize = placeholderFontSize;
    [self setNeedsDisplay];
}

//textView 文字大小
- (void)setFontSize:(CGFloat)fontSize
{
    _fontSize = fontSize;
    self.font = [UIFont systemFontOfSize:fontSize];
}

//边框圆角弧度
- (void)setCornerRadius:(CGFloat)cornerRadius
{
    _cornerRadius = cornerRadius;
    self.layer.cornerRadius = cornerRadius;
}

//边框宽度
- (void)setBorderWidth:(CGFloat)borderWidth
{
    _borderWidth = borderWidth;
    self.layer.borderWidth = borderWidth;
}

//边框颜色
- (void)setBorderColor:(UIColor *)borderColor
{
    _borderColor = borderColor;
    self.layer.borderColor = borderColor.CGColor;
}

- (void)setText:(NSString *)text
{
    [super setText:text];
    [self setNeedsDisplay];
}

- (void)setAttributedText:(NSAttributedString *)attributedText
{
    [super setAttributedText:attributedText];
    [self setNeedsDisplay];
}

调用 layoutSubviews 方法:

- (void)layoutSubviews
{
    [super layoutSubviews];
    [self setNeedsDisplay];
}

调用 dealloc 方法,移除监听:

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

在 ViewController 中使用:

引入头文件#import “CustomPlaceholderTextView.h”
创建CustomPlaceholderTextView类实例:

@property (nonatomic, strong) CustomPlaceholderTextView *textView;

使用自定义的 textView:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.textView = [[CustomPlaceholderTextView alloc] initWithFrame:CGRectMake(30, 100, 350, 100)];
    self.textView.placeholder = @"textView placeholder";
    self.textView.placeholderColor = [UIColor redColor];
    self.textView.fontSize = 15.0;
    self.textView.placeholderFontSize = 15.0;
    self.textView.text_x = 7;
    self.textView.text_y = 8;
    self.textView.borderWidth = 0.5;
    self.textView.borderColor = [UIColor grayColor];
    self.textView.cornerRadius = 5;
    [self.view addSubview:self.textView];
}

运行结果:
UITextView 添加占位提示文字

相关标签: UITextView