博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
新浪微博客户端(36)-自定义带placeholder的TextView
阅读量:7057 次
发布时间:2019-06-28

本文共 2448 字,大约阅读时间需要 8 分钟。

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

DJTextView.h

#import 
@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

最终效果:

 

转载地址:http://gngol.baihongyu.com/

你可能感兴趣的文章
C++ 重载、覆盖和隐藏
查看>>
Silverlight浮动窗体 floatablewindow 非模态对话框
查看>>
C#解析json文件的方法
查看>>
WPF如何不显示最大化,最小化的按钮
查看>>
交叉表组件
查看>>
探索式测试实践之路(国际大师James Bach题词推荐之探索式测试唯一本土著作)
查看>>
每日英语:Generation Exhausted
查看>>
初识EseNt
查看>>
POJ 1787 Charlie's Change (完全背包,记录路径)
查看>>
Java中IO操作的基本规律
查看>>
第四章 数据抽象 ----《C++编程思想》
查看>>
iBatis简单入门教程
查看>>
ATL 对象映射表
查看>>
JavaScript 引擎——Chrome V8 引擎入门
查看>>
JASocket 0.6.0 发布
查看>>
每天一个linux命令(33):df 命令
查看>>
windows下PHP环境搭建
查看>>
【R作图】如何避免图例显示不完全
查看>>
优秀网页设计:使用纹理的20个华丽示例
查看>>
线程同步(VC_Win32)
查看>>