UITextView实现PlaceHolder的方式
实现UITextView实现PlaceHolder的方式的方式有两种,这两种方法的核心就是通过通知来添加和去除PlaceHolder;下面来介绍两种方法;个人比较喜欢第一种,看起来更加合理。
方法1:原理是通过通知来改变PlaceHolder,把PlaceHolder看成是一个UILabel,设置UILabel的透明度,来让Placeholder显示与不显示。这种方法对UITextView本身影响较小。学习自Fly_Elephant:《UITextView实现PlaceHolder的方式》这篇文章
.h文件
- #import <UIKit/UIKit.h>
- @interface DLTextView : UITextView
- @property (nonatomic, retain) NSString *placeholder;
- @property (nonatomic, retain) UIColor *placeholderColor;
- - (void)textChanged:(NSNotification*)notification;
- @end
.m文件
- #import "DLTextView.h"
- @interface DLTextView ()
- @property (nonatomic, retain) UILabel *placeHolderLabel;
- @end
- @implementation DLTextView
- CGFloat const UI_PLACEHOLDER_TEXT_CHANGED_ANIMATION_DURATION = 0.25;
- - (void)dealloc
- {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- - (void)awakeFromNib
- {
- [super awakeFromNib];
- if (!self.placeholder) {
- [self setPlaceholder:@""];
- }
- if (!self.placeholderColor) {
- [self setPlaceholderColor:[UIColor lightGrayColor]];
- }
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
- }
- - (id)initWithFrame:(CGRect)frame
- {
- if( (self = [super initWithFrame:frame]) )
- {
- [self setPlaceholder:@""];
- [self setPlaceholderColor:[UIColor lightGrayColor]];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
- }
- return self;
- }
- - (void)textChanged:(NSNotification *)notification
- {
- if([[self placeholder] length] == 0)
- {
- return;
- }
- [UIView animateWithDuration:UI_PLACEHOLDER_TEXT_CHANGED_ANIMATION_DURATION animations:^{
- if([[self text] length] == 0)
- {
- [[self viewWithTag:999] setAlpha:1];
- }
- else
- {
- [[self viewWithTag:999] setAlpha:0];
- }
- }];
- }
- - (void)setText:(NSString *)text {
- [super setText:text];
- [self textChanged:nil];
- }
- - (void)drawRect:(CGRect)rect
- {
- if( [[self placeholder] length] > 0 )
- {
- if (_placeHolderLabel == nil ) {
- _placeHolderLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 8, self.bounds.size.width, 10)];
- _placeHolderLabel.lineBreakMode = NSLineBreakByWordWrapping;
- _placeHolderLabel.numberOfLines = 0;
- // _placeHolderLabel.font = self.font;
- _placeHolderLabel.font = [UIFont systemFontOfSize:13.0];
- _placeHolderLabel.backgroundColor = [UIColor clearColor];
- _placeHolderLabel.textColor = self.placeholderColor;
- _placeHolderLabel.alpha = 0;
- _placeHolderLabel.tag = 999;
- [self addSubview:_placeHolderLabel];
- }
- _placeHolderLabel.text = self.placeholder;
- [_placeHolderLabel sizeToFit];
- [self sendSubviewToBack:_placeHolderLabel];
- }
- if( [[self text] length] == 0 && [[self placeholder] length] > 0 )
- {
- [[self viewWithTag:999] setAlpha:1];
- }
- [super drawRect:rect];
- }
- @end
第二种方法:原理是通过Placeholder字符串的长度,当textView输入内容时,Placeholder 字符串的长度为nil,当textView不输入内容时,Placeholder显示。
- #import <UIKit/UIKit.h>
- @interface DLTextView : UITextView
- @property (nonatomic, copy) NSString *placeholder;
- @end
- #import "DLTextView.h"
- @implementation DLTextView
- - (instancetype)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- [self setup];
- }
- return self;
- }
- - (instancetype)initWithCoder:(NSCoder *)decoder
- {
- self = [super initWithCoder:decoder];
- if (self) {
- [self setup];
- }
- return self;
- }
- - (void)awakeFromNib
- {
- // [self setup];
- }
- - (void)setup
- {
- // NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:super.text];
- // NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
- // paragraphStyle.lineSpacing = 10;
- //
- // [attributedText addAttributes:@{NSParagraphStyleAttributeName : paragraphStyle} range:NSMakeRange(0, super.text.length)];
- //
- // super.attributedText = attributedText;
- // 添加通知
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidBeginEditing:) name:UITextViewTextDidBeginEditingNotification object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidEndEditing:) name:UITextViewTextDidEndEditingNotification object:nil];
- }
- - (void)dealloc
- {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- #pragma mark - 开始编辑的消息方法
- - (void)textViewDidBeginEditing:(NSNotification *)sender
- {
- // 开始编辑的时候,父控件的text如果等于placeholder就让什么也不显示
- if ([super.text isEqualToString:self.placeholder]) {
- super.text = @"";
- [super setTextColor:[UIColor blackColor]];
- }
- }
- - (void)textViewDidEndEditing:(NSNotification *)sender
- {
- if (super.text.length == 0) {
- super.text = self.placeholder;
- [super setTextColor:[UIColor grayColor]];
- }
- }
- - (void)setPlaceholder:(NSString *)placeholder
- {
- _placeholder = placeholder;
- // 调用通知的方法,让placeholder显示在UI上面
- [self textViewDidEndEditing:nil];
- }
- #pragma mark - 重写父类的text方法
- - (NSString *)text
- {
- if ([super.text isEqualToString:self.placeholder]) {
- super.text = @"";
- }
- return super.text;
- }
- @end
UITextView实现PlaceHolder的方式的更多相关文章
- iOS开发-UITextView实现PlaceHolder的方式
之前开发遇到过UITextField中加入一个PlaceHolder的问题,直接设置一下即可,不过这次是需要在UITextView中实现一个PlaceHolder,跟之前有点不同.在网上参考了各位前辈 ...
- UITextView实现placeHolder方法汇总
UITextField中有一个placeholder属性,可以设置UITextField的占位文字,起到提示用户的作用.可是UITextView就没那么幸运了,apple没有给UITextView提供 ...
- 教大家怎样给UITextView加入placeholder扩展
怎样扩展UITextView以追加placeholder功能呢? 我们的需求是:追加placeholder功能 方案讨论: 通过继承UITextView的方式 通过扩展UITextView的方式 分析 ...
- iOS - UITextView实现placeHolder占位文字
iOS之UITextView实现placeHolder占位文字的N种方法 前言 iOS开发中,UITextField和UITextView是最常用的文本接受类和文本展示类的控件.UITextFie ...
- UITextView 实现placeholder的方法
本文转载至 http://www.cnblogs.com/easonoutlook/archive/2012/12/28/2837665.html 在UITextField中自带placeholder ...
- 实现UITextView的placeholder
我们知道在iOS开发时,控件UITextField有个placeholder属性,UITextField和UITextView使用方法基本类似,有两个小区别:1.UITextField单行输入,而UI ...
- UITextView设置placeholder
下面是我的代码,可以直接拿来用 #import <UIKit/UIKit.h> @interface CustomTextView : UITextView @property(nonat ...
- spring in action 学习十一:property placeholder Xml方式实现避免注入外部属性硬代码化
这里用到了placeholder特有的一个语言或者将表达形式:${},spring in action 描述如下: In spring wiring ,placeholder values are p ...
- UITextView 实现placeholder
1.在创建textView的时候,赋值其文本属性 即 textView.text = @"内容": 2.在开始编辑的代理方法中进行如下操作 - (void)textViewDidB ...
随机推荐
- textField 基本属性
_textField.frame = CGRectMake(0, 0, 200, 50); _textField.delegate = self; _textField.text = str; [_t ...
- centos 7 内存压测测试--memtester工具
1.下载memteste工具 官方:http://pyropus.ca/software/memtester/ wget http://pyropus.ca/software/memtester/ol ...
- rabbitmq设置队列消息存活时间
public static final int ALIVETIME = 600000; public static final String QUEUE = "hnyz_gs_queue&q ...
- UML-重构
1.重构是什么? 重构是重写或重新构建已有代码的结构化和规律性方法,但不会改变已有代码的外在行为,而是采用一系列少量转换的步骤,并且每一步都结合了重新执行的测试.重构并不是全部推翻原有代码结构. 2. ...
- 程序Dog的大梦想
一.我是程序狗 "怎么又是任宏啊,每天都起这么早,要命啊--" "人家任宏可是要成为学霸的男人,咱们这些凡夫俗子啊,理解不了这么伟大的理想--"----微电影& ...
- Ubuntu的软件安装管理---dpkg与apt-*详解
摘要:软件厂商先在他们的系统上面编译好了我们用户所需要的软件,然后将这个编译好并可执行的软件直接发布给用户安装.不同的 Linux 发行版使用不同的打包系统,一般而言,大多数发行版分别属于两大包管理技 ...
- 正确返回Unicode码点大于0xFFFF的字符串长度
如下: function codePointLength(text){ var result = text.match(/[\s\S]/gu); return result? result.lengt ...
- UML-如何画顺序图?
1.生命线框图和生命线 生命线:可以为虚线(源于UML1),也可以是实线 2.消息 1).创始消息,实心圆开头2).同步消息,实心箭头 3.执行规格条和控制期 控制期:阻塞调用 4.返回值 5.自身消 ...
- 蓝屏(BSOD)转储设置,看本文就够了!
原总结注册表debug调试dump转储文件蓝屏BSODprocess monitor 前言 我们在 内核转储,开抓啦! 这篇文章里介绍了一个关键的系统设置.设置好后可以让系统在蓝屏(Blue Scre ...
- iOS 添加view的分类(更加方便的设置view的位置)
点击创建UIView的分类category,这里命名为 PLExtension(为了和下面对应) view分类.h文件 #import <UIKit/UIKit.h> @interface ...