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 ...
随机推荐
- Java哪些集合类是线程安全的?
早在jdk的1.1版本中,所有的集合都是线程安全的.但是在1.2以及之后的版本中就出现了一些线程不安全的集合,为什么版本升级会出现一些线程不安全的集合呢?因为线程不安全的集合普遍比线程安全的集合效率高 ...
- Mac Outlook 2016 无法打开会议室日历
问题:Mac Outlook 2016 无法打开会议室日历信息,报错截图如下: 解决方案: Set-MailboxFolderPermission -Identity XXX@xxx.com:\日历 ...
- A4纸网页打印
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- (转载)(DescriptionResource Path Location Type The superclass "javax.servlet.http.HttpServlet" was not foun
eclipse环境下如何配置tomcat 打开Eclipse,单击"Window"菜单,选择下方的"Preferences". 单击"Server&q ...
- PPT |《Kubernetes的兴起》
京东云开发者社区技术沙龙--<Cloud Native时代的应用之路与开源创新> Part1-<Kubernetes的兴起> 欢迎点击"链接"了解更多精彩内 ...
- 超级顽固的流方式读取doc,docx乱码问题
因为工作中需要一个把doc或者docx的office文档内容,需要读取出来,并且也没展示功能.代码中第一考虑可能就是通过读取流方式,结果写了以后,各种乱码,百科的解决方案也是千奇百怪,第一点:可能是文 ...
- 1811 06 pygame 的继续开发
早上看了 高数和python 好像系统没有保存 桑心啊 关于游戏背景的制作 游戏背景就是 背景在移动 而主人物还在原位置的 常常用于跑酷游戏类 背景开始绘制两张图像 一张完全 ...
- vue安装openlayers,jquery,bootstrap,阿里iconfont,
安装 安装openlayers安装指定包安装openlayersVUE中的地图import ol from "openlayers";import "openlayers ...
- JAVA初学者——标识符命名规则及数据类型的转换
Hello!我是浩宇大熊猫~ 直接进入正题吧~ 1)标识符的命名规则. 标识符命名法有小驼峰命名法和大驼峰命名法两种,分别应用于方法.变量和类. 小驼峰命名法应用于方法和变量,主要有两个约定: 1.标 ...
- vscode Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client
vscode 连接 mysql 时出现这个错误 alter user 'root'@'localhost' identified with mysql_native_password by 'pass ...