新浪微博客户端(36)-自定义带placeholder的TextView

iOS 上自带的UITextView竟然不能设置placeholder,但是UITextView却可以,我也真是醉了。没办法了,自己写一个

DJTextView.h

复制代码
#import <UIKit/UIKit.h>

@interface DJTextView : UITextView


@property (nonatomic,copy) NSString *placeholder;
@property (nonatomic,strong) UIColor *placeholderColor;


@end
复制代码

 

DJTextView.m

复制代码
#import "DJTextView.h"

@implementation DJTextView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 在通知中心为TextView注册一个当文本改变的通知,当文本发生变化时,TextView会发一个通知
        // 类似于android里面的BroadcastReceiver
        // iOS 注册接收通知的方式为:addObserver name:
        // android 注册接收通知的方式为:intent.addAction();
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textHasChange) name:UITextViewTextDidChangeNotification object:self];
    }
    return self;
}



// 当文字改变时会调用此方法
- (void)textHasChange {

    // setNeedsDisplay 类似于android里面的postInvalidate()方法,都是向操作系统发出请求重绘的消息
    // 操作系统会在未来的时间内调用drawRect(iOS),onDraw(android);
    // 注意:系统不允许我们自己调用drawRect或onDraw方法
    [self setNeedsDisplay];

}



- (void)drawRect:(CGRect)rect {

    
    if ([self hasText]) return; // 如果检测到当前TextView中有文本,就不再绘制
    
    CGFloat placeholderRectX = 5;
    CGFloat placeholderRectY = 8;
    CGFloat placeholderRectW = rect.size.width - 2 * placeholderRectX;
    CGFloat placeholderRectH = rect.size.height - 2 * placeholderRectY;
    
    
    // 将文本绘制在一个指定的矩形框内
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = self.font;
     // 若用户没有设置placeholder的颜色,则给placeholder设置一个默认颜色
    attrs[NSForegroundColorAttributeName] = self.placeholderColor ? self.placeholderColor : [UIColor grayColor];
    [self.placeholder drawInRect:CGRectMake(placeholderRectX, placeholderRectY, placeholderRectW, placeholderRectH) withAttributes:attrs];
    

}



#pragma mark - 当用户手动更新当前字体属性时,就自动触发重绘
- (void)setText:(NSString *)text{

    [super setText:text];
    [self setNeedsDisplay];

}


- (void)setFont:(UIFont *)font {

    [super setFont:font];
    [self setNeedsDisplay];

}

- (void)setPlaceholder:(NSString *)placeholder {

    _placeholder = placeholder;
    [self setNeedsDisplay];

}


- (void)setPlaceholderColor:(UIColor *)placeholderColor {

    _placeholderColor = placeholderColor;
    [self setNeedsDisplay];

}



- (void)dealloc {

        // 类似于android,通知中心在使用完毕后需要销毁
        // iOS: [NSNotificationCenter defaultCenter] removeObserver:self]
        // android: unRegister(mBroadcastReceiver);
       [[NSNotificationCenter defaultCenter] removeObserver:self];

}



@end
复制代码

最终效果:

 



如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
posted @   夜行过客  阅读(409)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示