TextView增加placeholder

//
//  CustomTextView.m
//  
//
//  Created by Oliver on 2021/5/27.
//

#import "CustomTextView.h"

@implementation CustomTextView

// xib 创建
- (void)awakeFromNib {
    [super awakeFromNib];
    //添加监听,在文字编辑时进行监听
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(customerTextViewDidChange) name:UITextViewTextDidChangeNotification object:nil];
}

// 初始化方法
- (instancetype)init {
    self = [super init];
    if (self) {
        //添加监听,在文字编辑时进行监听
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(customerTextViewDidChange) name:UITextViewTextDidChangeNotification object:nil];
    }
    return self;
}

// 监听文字输入时的变化,发生变化时就进行重绘
- (void)customerTextViewDidChange {
    //该方法会调用执行drawRect:方法对子视图(占位字符)进行重绘
    [self setNeedsDisplay];
}

// 绘制占位符
- (void)drawRect:(CGRect)rect {
    // Drawing code
    //重绘子视图
    if (self.hasText) {
        // 有已经输入的文本内容时,不再重绘占位字符
        return;
    }
    //没有文本时需要对占位字符进行重绘
    NSMutableDictionary *attrsDic = [NSMutableDictionary dictionary];
    attrsDic[NSFontAttributeName] = self.placeholderFont;
    attrsDic[NSForegroundColorAttributeName] = self.defPlaceholderColor;
    
    //在textView的框内绘制占位字符
    CGRect resultRect = [self calculatePlaceholderWidth:self.placeholder];
    
    // 占位符位置根据文本居左居中局右显示
    CGFloat draw_X = 0;
    CGFloat draw_Y = 11;
    if (self.placeholderisCenterY) {
        draw_Y = (self.frame.size.height - resultRect.size.height)/2;
    }
    if (self.textAlignment == NSTextAlignmentLeft) {
        if (self.placeholderisHaveNoLeftMargin) {
            draw_X = 3;
        }else{
           draw_X = 15;
        }
    } else if (self.textAlignment == NSTextAlignmentCenter) {
        draw_X = (self.frame.size.width - resultRect.size.width)/2;
    } else if (self.textAlignment == NSTextAlignmentRight) {
        draw_X = self.frame.size.width - resultRect.size.width;
    } else {
        draw_X = 0;
    }
    CGRect placeRect = CGRectMake(draw_X, draw_Y, resultRect.size.width, resultRect.size.height);
    [self.placeholder drawInRect:placeRect withAttributes:attrsDic];
}

// 计算占位符的宽高
- (CGRect)calculatePlaceholderWidth:(NSString *)placeholder {
    CGFloat labWidth = self.frame.size.width;
    CGFloat labHeight = self.frame.size.height;
    CGSize size = CGSizeMake(labWidth, labHeight);
    CGRect rect = [placeholder boundingRectWithSize:size options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:[NSDictionary dictionaryWithObject:self.placeholderFont forKey:NSFontAttributeName] context:nil];
    return rect;
}

/// 设置占位符
- (void)setPlaceholder:(NSString *)placeholder {
    _placeholder = placeholder;
    // 文本变化时,就进行重绘调用执行drawRect:
    [self setNeedsDisplay];
}

/// 设置字体
- (void)setFont:(UIFont *)font {
    [super setFont:font];
    [self setNeedsDisplay];
}

#pragma mark - LazyLoad/懒加载

- (UIColor *)defPlaceholderColor {
    if (!_defPlaceholderColor) {
        _defPlaceholderColor = [UIColor lightGrayColor];
    }
    return _defPlaceholderColor;
}

- (UIFont *)placeholderFont {
    if (!_placeholderFont) {
        _placeholderFont = self.font;
    }
    return _placeholderFont;
}

- (void)dealloc {
    //移除监听的通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end

初始化方法添加文字编辑时进行监听文字编辑通知重绘 设置占位符 设置字体

调用 [self setNeedsDisplay];

在drawInRect方法 中进行添加placeholder交互。

判断是否有文本内容时,不再重绘占位字符

没有文本时需要对占位符进行重绘

在textview的框内绘制占位符

占位符位置根据文本局中居右显示

最后将占位符字符串绘制到TextView上

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值