实现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. Python说文解字_Python之多任务_03

    问:线程学完了,现在我们开始学习进程了吧? 答:是的.前面说到线程就是我们的手,我们现在可以学习一下我们的“胳膊”了. 我们有了多线程,为什么还要学习多进程呢?这是因为在Python当中有一把GIL锁 ...

  2. Javascript object.constructor属性与面向对象编程(oop)

    定义和用法 在 JavaScript 中, constructor 属性返回对象的构造函数. 返回值是函数的引用,不是函数名: JavaScript 数组 constructor 属性返回 funct ...

  3. 【每日Scrum】第五天冲刺

    一.计划会议内容 仍然在解决数据库问题 二.任务看板 三.scrum讨论照片 四.产品的状态 无 五.任务燃尽图  

  4. 从认证到调度,K8s 集群上运行的小程序到底经历了什么?

    导读:不知道大家有没有意识到一个现实:大部分时候,我们已经不像以前一样,通过命令行,或者可视窗口来使用一个系统了. 前言 现在我们上微博.或者网购,操作的其实不是眼前这台设备,而是一个又一个集群.通常 ...

  5. idea代理上网

    idea 代理上网 浏览器能够上网,idea无法下载jar 浏览器无法上网则配置浏览器代理 --------- start //------------------------浏览器代理完毕 idea ...

  6. bfs--奇怪的电梯P1135

    计院有一个bug电梯,可能是hyk造的,很多bug,电梯只有两个按钮,“上”和“下”,电梯每层都可以停,每层都有一个数字Ki(0<=Ki<=n),当你在一层楼,你按“上”键会到1+K1层, ...

  7. java 连接mysql 示例

    import java.sql.*; public class Main { // MySQL 8.0 以下版本 - JDBC 驱动名及数据库 URL static final String JDBC ...

  8. 黑马oracle_day02:04.oracle对象&&05.oracle编程(a)

    01.oracle体系结构 02.oracle的基本操作 03.oracle的查询 04.oracle对象&&05.oracle编程(a) 05.oracle编程(b) 04.orac ...

  9. UML-类图-关键字如何使用?

    部分预定义UML关键字: 例如: 加上关键字,明确,清晰.

  10. UINavigationbar跳转黑色

    bug效果:导航栏过渡出现黑色