如何实现在TextView使用占位符(placeholder)
来源:互联网 发布:项目管理软件.知乎 编辑:程序博客网 时间:2024/06/03 19:46
近期在开发中遇到了一个需求,就是要在textView中添加一个占位符,类似于textField的placeholder,但是经过查询,发现textView并没有placeholder属性。 随意只能自己重写了。其实原理很简单,分为两步
- 1、创建一个label加入textView中。
- 2、判断状态,如果在输入,就隐藏label。
其实主要就是这两步,我们接下来就开始封装一个类,来实现这个功能。
创建一个类集成UITextView
定义两个属性
//这里接收占位符的字符串@property (nonatomic, copy) NSString *placeHolder;//用来判断是否隐藏占位符的label@property (nonatomic, assign) BOOL hidenPlaceHolder;
首先自然是要先懒加载一个UILabel
Q:为什么这个UIlabel要懒加载
A:如果不懒加载,否则,没设置一次就会加载一次,造成内存浪费
//在分类中添加一个uiLabel属性@interface XXTextView ()@property (nonatomic, weak) UILabel *placeHolderLabel;@end
然后重写这个Label的get属性,即懒加载:
//懒加载这个Label-(UILabel *)placeHolderLabel{ if (_placeHolderLabel == nil) { //创建label UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 7, 30, 20)]; label.textColor = [UIColor lightGrayColor]; //给label赋值 _placeHolderLabel = label; //将label显示在试图上 [self addSubview:label]; } return _placeHolderLabel;}
接下来在在实现文件中重写placeholder属性的set方法:
//重写placeholder的set方法-(void)setPlaceHolder:(NSString *)placeHolder{ _placeHolder = placeHolder; self.placeHolderLabel.text = placeHolder; //边界根据内容自适应 [self.placeHolderLabel sizeToFit];}
但是运行时发现,占位符的字号和即将输入的字号不同,这个时候我们就要调整Label的字条,使UILabel的字号和占位符的字号一致。
为了不造成资源浪,我们重写intwithFrame方法,在其中设置光标的字号,代码如下:
//默认调整字号为16-(instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { self.font = [UIFont systemFontOfSize:16]; self.placeHolderLabel.font = self.font; } return self;}
这样,是不是就重合了呢,第一步就完成了,接下来就要实现隐藏功能了。
重写hidenplaceholder的set方法:
//重写hidenplaceholder的set方法- (void)setHidenPlaceHolder:(BOOL)hidenPlaceHolder{ _hidenPlaceHolder = hidenPlaceHolder; self.placeHolderLabel.hidden = hidenPlaceHolder;}
然后在你创建textView的时候进行判断。如果监听到输入,就隐藏Label,这个时候就要用到通知了。
自然,第一步,订阅通知:
//订阅通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:nil];
然后我们实现这个textChange方法就可以了
- (void)textChange{ [self becomeFirstResponder]; if (_textView.text.length) { //有内容 _textView.hidenPlaceHolder = YES; }else{ _textView.hidenPlaceHolder = NO; }}
订阅了通知,记得退订通知
-(void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self];}
这样。封装一个TextView类的占位符功能就完成了。下次如果还要使用,直接导入.h和.m文件就可以使用了。
可能描述的不是很清楚。详细代码如下
XXTextView.h
#import <UIKit/UIKit.h>@interface middleViewController : UIViewController@end
XXTextView.m
#import "XXTextView.h"@interface XXTextView ()@property (nonatomic, weak) UILabel *placeHolderLabel;@end@implementation XXTextView-(instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { self.font = [UIFont systemFontOfSize:16]; _placeHolderLabel.font = self.font; } return self;}-(UILabel *)placeHolderLabel{ if (_placeHolderLabel == nil) { //创建label UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 7, 30, 20)]; label.textColor = [UIColor lightGrayColor]; //给label赋值 _placeHolderLabel = label; //将label显示在试图上 [self addSubview:label]; } return _placeHolderLabel;}- (void)setHidenPlaceHolder:(BOOL)hidenPlaceHolder{ _hidenPlaceHolder = hidenPlaceHolder; self.placeHolderLabel.hidden = hidenPlaceHolder;}-(void)setPlaceHolder:(NSString *)placeHolder{ _placeHolder = placeHolder; self.placeHolderLabel.text = placeHolder; //边界根据内容自适应 [self.placeHolderLabel sizeToFit];}- (void)setFont:(UIFont *)font{ [super setFont:font]; _placeHolderLabel.font = font; [_placeHolderLabel sizeToFit];}@end
接下来在ViewController中创建一个UITextView
XXTextView *textView = [[XXTextView alloc] initWithFrame:[UIScreen mainScreen].bounds]; _textView = textView; //设置占位符 textView.placeHolder = @"快记录今天的心情吧"; [self.view addSubview:textView]; //订阅通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:nil];
如果写得不好,或者有问题,请联系作者修正
- 如何实现在TextView使用占位符(placeholder)
- 自定义带占位符Placeholder的textView
- textView 如何设置占位符
- tensorflow-placeholder-占位符的使用
- 关于表单用js如何实现占位符placeholder的效果
- XZ_iOS之使用textView实现占位文字
- TextView 实现 placeholder 备忘
- textView实现placeholder效果
- TextView 实现 placeholder
- textView 实现placeholder
- ios中实现带占位符placeholder的UITextview
- AI学习之路(20)占位符(placeholder)的使用
- spring 配置文件中的占位符 使用 context:property-placeholder
- HTML5 - 使用占位符文本placeholder添加文本框提示
- spring 配置文件中的占位符 使用 context:property-placeholder
- spring 配置文件中的占位符 使用 context:property-placeholder
- spring 配置文件中的占位符 使用 context:property-placeholder
- 也谈placeHolder占位符
- 从jsp页面导出Excel(IE和其他浏览器下)
- javaweb前端点击小图切换大图
- 安卓App热补丁动态修复技术介绍
- ImageMagick install support Jbig 支持JBIG PNG
- weblogic11g 使用postgresql创建数据源
- 如何实现在TextView使用占位符(placeholder)
- 一篇文章看懂Android学习最佳路线
- 外排序
- RecyclerView的使用(一)
- 从现在开始每天进步一点点
- HDU ACM 1009 FatMouse' Trade
- 类和实例
- Android AIDL的简单例子
- MySQL:MySQL的一些客户端工具