实现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的方式的更多相关文章

  1. iOS开发-UITextView实现PlaceHolder的方式

    之前开发遇到过UITextField中加入一个PlaceHolder的问题,直接设置一下即可,不过这次是需要在UITextView中实现一个PlaceHolder,跟之前有点不同.在网上参考了各位前辈 ...

  2. UITextView实现placeHolder方法汇总

    UITextField中有一个placeholder属性,可以设置UITextField的占位文字,起到提示用户的作用.可是UITextView就没那么幸运了,apple没有给UITextView提供 ...

  3. 教大家怎样给UITextView加入placeholder扩展

    怎样扩展UITextView以追加placeholder功能呢? 我们的需求是:追加placeholder功能 方案讨论: 通过继承UITextView的方式 通过扩展UITextView的方式 分析 ...

  4. iOS - UITextView实现placeHolder占位文字

      iOS之UITextView实现placeHolder占位文字的N种方法 前言 iOS开发中,UITextField和UITextView是最常用的文本接受类和文本展示类的控件.UITextFie ...

  5. UITextView 实现placeholder的方法

    本文转载至 http://www.cnblogs.com/easonoutlook/archive/2012/12/28/2837665.html 在UITextField中自带placeholder ...

  6. 实现UITextView的placeholder

    我们知道在iOS开发时,控件UITextField有个placeholder属性,UITextField和UITextView使用方法基本类似,有两个小区别:1.UITextField单行输入,而UI ...

  7. UITextView设置placeholder

    下面是我的代码,可以直接拿来用 #import <UIKit/UIKit.h> @interface CustomTextView : UITextView @property(nonat ...

  8. spring in action 学习十一:property placeholder Xml方式实现避免注入外部属性硬代码化

    这里用到了placeholder特有的一个语言或者将表达形式:${},spring in action 描述如下: In spring wiring ,placeholder values are p ...

  9. UITextView 实现placeholder

    1.在创建textView的时候,赋值其文本属性 即 textView.text = @"内容": 2.在开始编辑的代理方法中进行如下操作 - (void)textViewDidB ...

随机推荐

  1. 计算机网络(1): http原理和uuid

    http 的请求报文和响应报文格式 请求报文有哪些方法 一个典型的http报文 状态码有哪几种 以及短语是用来解释状态码的 接口测试中,需要使用到UUID,用来生成唯一ID. 1.什么是UUID UU ...

  2. PAT Advanced 1090 Highest Price in Supply Chain (25) [树的遍历]

    题目 A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)–everyone inv ...

  3. 寒假day13

    今天看了计算机网络的相关面试题

  4. 201771010123汪慧和《面向对象程序设计JAVA》第六周实验总结

    一.理论部分: 1.继承 用已有类来构建新类的一种机制.当定义了一个新类继承了一个类时,这个新类就继承了这个类的方法和域,同时在新类中添加新的方法和域以适应新的情况. 2.类.超类.子类 (1)类继承 ...

  5. Docker部署zookeeper集群和kafka集群,实现互联

    本文介绍在单机上通过docker部署zookeeper集群和kafka集群的可操作方案. 0.准备工作 创建zk目录,在该目录下创建生成zookeeper集群和kafka集群的yml文件,以及用于在该 ...

  6. 17.3.12--uillib模块

    1---uillib是python标准库中最常用的一个python网络应用资源访问的模块,他可以让你像访问文本一样,读取网页的内容 它的作用是访问一些不需要验证的网络资源和cookie等 uillib ...

  7. 吴裕雄--天生自然 PHP开发学习:多维数组

    <pre> <?php // 二维数组: $cars = array ( array("Volvo",100,96), array("BMW" ...

  8. SVN提交时忽略不必提交的文件夹和文件,如node_modules

    空白处右键>选中TortoiseSVN>设置(settings)>常规设置(General)>Subversion>编辑(edit)>在弹出的config文件中找g ...

  9. OracleXE 11g user莫名过期

    参考大大的 环境sqlplus 1.sysdba登陆 SQL>conn sys as sysdba password 2.查看用户状态 SQL>select username,accoun ...

  10. JavaScript详解(一)

    简介: Javascript是一个脚本语言,弱类型的编程语言,简称js,被称为网站开发的行为.它的作用是增加页面特效.前后台交互以及应用于后台开发.它即可写在HTML的script标签里,也可写在外部 ...