iOS开发小技巧--自定义带有占位文字的TextView(两种方式)
自定义控件注意或框架注意:自己暴露在外面的属性,一定要重写setter,保证外界与内部的交互性
一.方案一:通过drawRect:方法将文字画到textView中,监听文字改变用的是通知中心(代理也可以监听文字改变,但是这种方式就成了自己作为自己的代理了,不推荐这种方法)发出的消息
UITextViewTextDidChangeNotification.自己监听通知.
- 对外界提供两个属性

- 内部实现
#import "ChaosPlaceholdTextView.h" @implementation ChaosPlaceholdTextView - (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:nil]; // 外界文字颜色不设置,默认为灰色
self.placeholdColor = [UIColor grayColor];
}
return self;
} - (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
} - (void)textChange
{
[self setNeedsDisplay];
} // 调用drawRect时,会将之前的图像擦除掉,重新绘制
- (void)drawRect:(CGRect)rect { if ([self hasText]) return; rect.origin.y += ( + );
rect.origin.x += ;
rect.size.width -= * rect.origin.x; NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSForegroundColorAttributeName] = self.placeholdColor;
attrs[NSFontAttributeName] = [UIFont systemFontOfSize:];
[self.placehold drawInRect:rect withAttributes:attrs];
} // 设计框架需注意,给外界提供了属性后,一定重写出行的setter,这样既可以时时监听使用者对属性的更改,还可以跟好的与外界代码进行交互
- (void)setPlacehold:(NSString *)placehold
{
_placehold = placehold;
// 设置了站位文字后,需要重绘一遍
[self setNeedsDisplay];
} - (void)setPlaceholdColor:(UIColor *)placeholdColor
{
_placeholdColor = placeholdColor;
[self setNeedsDisplay];
} // 同时,也要考虑到
- (void)setFont:(UIFont *)font
{
[super setFont:font]; [self setNeedsDisplay];
} - (void)setText:(NSString *)text
{
[super setText:text]; [self setNeedsDisplay];
} - (void)setAttributedText:(NSAttributedString *)attributedText
{
[super setAttributedText:attributedText]; [self setNeedsDisplay];
} @end
- 缺点:由于是画上去的,当设置了textView的alwaysBounceHorizontal属性后,不能跟随着scrollView的滑动而滑动,如下图

方案二:通过给TextView添加一个Label,也是通过监听文字改变的通知,修改label的hidden属性.
#import "ChaosPlaceholdTextView.h" @interface ChaosPlaceholdTextView()
/** 占位文字label */
@property (nonatomic, weak) UILabel *placeholderLabel;
@end @implementation ChaosPlaceholdTextView - (UILabel *)placeholderLabel
{
if (!_placeholderLabel) {
// 添加一个用来显示占位文字的label
UILabel *placeholderLabel = [[UILabel alloc] init];
placeholderLabel.numberOfLines = ;
placeholderLabel.x = ;
placeholderLabel.y = ;
[self addSubview:placeholderLabel];
_placeholderLabel = placeholderLabel;
}
return _placeholderLabel;
} - (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
// 垂直方向上永远有弹簧效果
self.alwaysBounceVertical = YES; // 默认字体
self.font = [UIFont systemFontOfSize:]; // 默认的占位文字颜色
self.placeholderColor = [UIColor grayColor]; // 监听文字改变
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:nil];
}
return self;
} - (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
} /**
* 监听文字改变
*/
- (void)textDidChange
{
// 只要有文字, 就隐藏占位文字label
self.placeholderLabel.hidden = self.hasText;
} /**
* 更新占位文字的尺寸
*/
- (void)updatePlaceholderLabelSize
{
CGSize maxSize = CGSizeMake(ChaosScreenW - * self.placeholderLabel.x, MAXFLOAT);
self.placeholderLabel.size = [self.placeholder boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.font} context:nil].size;
} #pragma mark - 重写setter
- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
_placeholderColor = placeholderColor; self.placeholderLabel.textColor = placeholderColor;
} - (void)setPlaceholder:(NSString *)placeholder
{
_placeholder = [placeholder copy]; self.placeholderLabel.text = placeholder; [self updatePlaceholderLabelSize];
} - (void)setFont:(UIFont *)font
{
[super setFont:font]; self.placeholderLabel.font = font; [self updatePlaceholderLabelSize];
} - (void)setText:(NSString *)text
{
[super setText:text]; [self textDidChange];
} - (void)setAttributedText:(NSAttributedString *)attributedText
{
[super setAttributedText:attributedText]; [self textDidChange];
} @end
iOS开发小技巧--自定义带有占位文字的TextView(两种方式)的更多相关文章
- iOS开发小技巧 - label中的文字添加点击事件
Label中的文字添加点击事件 GitHub地址:https://github.com/lyb5834/YBAttributeTextTapAction 以前老师讲过类似的功能,自己懒得回头看了,找了 ...
- iOS开发小技巧 - UILabel添加中划线
iOS开发小技巧 遇到的问题: 给Label添加中划线,然后并没有效果 NSString *str = [NSString stringWithFormat:@"合计金额 ¥%.2f&quo ...
- iOS开发小技巧 - runtime适配字体
iOS开发小技巧 - runtime适配字体 版权声明:本文为博主原创文章,未经博主允许不得转载,有问题可联系博主Email: liuyongjiesail@icloud.com 一个iOS开发项目无 ...
- iOS开发实用技巧—Objective-C中的各种遍历(迭代)方式
iOS开发实用技巧—Objective-C中的各种遍历(迭代)方式 说明: 1)该文简短介绍在iOS开发中遍历字典.数组和集合的几种常见方式. 2)该文对应的代码可以在下面的地址获得:https:// ...
- iOS开发小技巧
1. 解析详情页(是webView)遇到的3个问题: 1.图片太大,超出屏幕范围 2.怎么在webView上面添加一行文字 3.文字太小 1.解决方法 webView.scalesPageToFit ...
- 简易封装一个带有占位文字的TextView
在实际iOS应用开发中我们经常会用到类似于下图所示的界面,即带有占位文字的文本框:
- 简单说 通过CSS实现 文字渐变色 的两种方式
说明 这次的重点就在于两个属性, background 属性 mask 属性 这两个属性分别是两种实现方式的关键. 解释 方式一 效果图 代码 <!DOCTYPE html> <ht ...
- iOS开发小技巧--TableView中headerView的循环利用,以及自定义的headerView
一.首先要搞清楚,tableView中有两种headerView,一个是tableHeaderView,另一个是headerView.前者就一个;后者根据session决定个数 headerView的 ...
- iOS开发中设置UITextField的占位文字的颜色,和光标的颜色
在iOS开发中,对于很多初学者而言,很有可能碰到需要修改UITextField的占位文字的颜色,以及当UITextField成为第一响应者后光标的颜色,那么下面小编就介绍一下修改占位文字和光标的颜色. ...
随机推荐
- 【CSS】使用边框和背景
1. 应用边框样式 先从控制边框样式的属性开始.简单边框有三个关键属性:border-width.border-style 和 border-color . <!DOCTYPE html> ...
- 创建MyOffice项目
创建查看评分窗体(FrmLOOK),添加定义成员数组,将员工数据绑定到FrmLOOK窗体的ListView控件上 public ListViewItem lv; private void Form1_ ...
- BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]
1085: [SCOI2005]骑士精神 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1800 Solved: 984[Submit][Statu ...
- 第10章 同步设备I/O和异步设备I/O(1)_常见设备及CreateFile函数
10.1 打开和关闭设备 10.1.1 设备的定义——在Windows中可以与之进行通信的任何东西. (1)常见设备及用途 设备 用途 用来打开设备的函数 文件 永久存储任何数据 CreateFile ...
- 使用while循环语句和变量输出九九乘法表
输出的结果如下:
- AC日记——简单密码 openjudge 1.7 10
10:简单密码 总时间限制: 1000ms 内存限制: 65536kB 描述 Julius Caesar曾经使用过一种很简单的密码.对于明文中的每个字符,将它用它字母表中后5位对应的字符来代替,这 ...
- SQL连接查询
连接查询:通过连接运算符可以实现多个表查询.连接是关系数据库模型的主要特点,也是它区别于其它类型数据库管理系统的一个标志. 常用的两个链接运算符: 1.join on 2.union 在关系数据库 ...
- 审核被拒(后台定位,autio,voip,发表朋友圈)
APP上线审核被拒那些事(一) 2.3 - Apps that do not perform as advertised by the developer will be rejected 2.3 D ...
- centos安装docker
一.升级内核 [root@iZ2893wjzgyZ ~]# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org [root@iZ289 ...
- Java核心技术点之动态代理
本篇博文会从代理的概念出发,介绍Java中动态代理技术的使用,并进一步探索它的实现原理.由于个人水平有限,叙述中难免出现不清晰或是不准确的地方,希望大家可以指正,谢谢大家:) 一.概述 1. 什么是代 ...