怎样扩展UITextView以追加placeholder功能呢?

我们的需求是:追加placeholder功能

方案讨论:

  • 通过继承UITextView的方式
  • 通过扩展UITextView的方式

分析:方案1使用继承方式实现起来更简单,可是使用起来就没有那么方便;方案2 使用扩展的方式,实现起来稍比前者复杂。可是外部使用起来更简单

方案定位:採用扩展的方式,以极简的风格作为參考依据。

Tip:所谓极简。即对外接口最简,对内部能够非常复杂

扩展头文件

#import <UIKit/UIKit.h>

/**
* @author huangyibiao
*
* 给UITextView加入placeholder
*
* @note 注意。此扩展有点小问题。假设在加入placeholder后,又直接赋值Text属性。则不会自己主动消失
* 解决的方法是:假设有初始值,先给text,再设置holder
*/
@interface UITextView (HDFTextView) /**
* 占位提示语
*/
@property (nonatomic, copy) NSString *hdf_placeholder; /**
* 占位提示语的字体颜色
*/
@property (nonatomic, strong) UIColor *hdf_placeholderColor; /**
* 占位提示语的字体
*/
@property (nonatomic, strong) UIFont *hdf_placeholderFont; /**
* 占位提示语标签
*/
@property (nonatomic, strong, readonly) UILabel *hdf_placeholderLabel; @end
##说明:头文件里事实上仅仅须要公开hdf_placeholderLabel属性就能够实现了,可是依旧公开了对hdf_placeholderLabel直接属性,能够依据个人习惯使用。扩展是不能扩展属性的。可是这里写成了属性的形式。事实上仅仅是利用了getter/setter方式。重写其API。

内部会使用动态执行时机制来完毕扩展伪属性的功能(称为伪属性是由于本质上是扩展不了属性的)


实现文件

#import "UITextView+HDFTextView.h"

static const void *s_hdfTextViewPlaceholderLabelKey = "s_hdfTextViewPlaceholderLabelKey";
static const void *s_hdfTextViewPlaceholderTextKey = "s_hdfTextViewPlaceholderTextKey"; @interface UIApplication (HDFTextViewHolder) @end @implementation UIApplication (HDFTextViewHolder) - (void)hdf_placehoderTextChange:(NSNotification *)nofitication {
if (kIsIOS7OrLater) {
return;
}
UITextView *textView = nofitication.object;
if ([textView isKindOfClass:[UITextView class]]) {
if (!kIsEmptyString(textView.text)) {
textView.hdf_placeholderLabel.text = @"";
} else {
textView.hdf_placeholderLabel.text = textView.hdf_placeholder;
}
}
} @end @interface UITextView (HDFPlaceholderTextView) @property (nonatomic, strong) UILabel *placeholderLabel; @end @implementation UITextView (HDFPlaceholderTextView) - (void)setPlaceholderLabel:(UILabel *)placeholderLabel {
objc_setAssociatedObject(self,
s_hdfTextViewPlaceholderLabelKey,
placeholderLabel,
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
} - (UILabel *)placeholderLabel {
UILabel *label = objc_getAssociatedObject(self, s_hdfTextViewPlaceholderLabelKey); if (label == nil || ![label isKindOfClass:[UILabel class]]) {
label = [[UILabel alloc] init];
label.textAlignment = NSTextAlignmentLeft;
label.font = self.font;
label.backgroundColor = [UIColor clearColor];
label.textColor = kHolderTipColor;
[self addSubview:label]; kWeakObject(self);
self.placeholderLabel = label;
CGFloat left = kIsIOS7OrLater ? 5 : 7;
[label mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(weakObject).insets(UIEdgeInsetsMake(7.5, left, 0, 0));
}];
label.enabled = NO; [kNotificationCenter addObserver:kIsIOS7OrLater ? self : [UIApplication sharedApplication]
selector:@selector(hdf_placehoderTextChange:)
name:UITextViewTextDidChangeNotification
object:nil];
} return label;
} @end @implementation UITextView (HDFTextView) - (void)hdf_placehoderTextChange:(NSNotification *)notification {
if (kIsIOS7OrLater) {
if (!kIsEmptyString(self.text)) {
self.placeholderLabel.text = @"";
} else {
self.placeholderLabel.text = self.hdf_placeholder;
}
}
} - (UILabel *)hdf_placeholderLabel {
return self.placeholderLabel;
} - (void)setHdf_placeholder:(NSString *)hdf_placeholder {
if (kIsEmptyString(hdf_placeholder)) {
objc_setAssociatedObject(self, s_hdfTextViewPlaceholderLabelKey, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[self.placeholderLabel removeFromSuperview];
return;
} objc_setAssociatedObject(self,
s_hdfTextViewPlaceholderTextKey,
hdf_placeholder,
OBJC_ASSOCIATION_COPY_NONATOMIC); if (!kIsEmptyString(self.text)) {
self.placeholderLabel.text = @"";
} else {
self.placeholderLabel.text = hdf_placeholder;
}
} - (NSString *)hdf_placeholder {
return objc_getAssociatedObject(self, s_hdfTextViewPlaceholderTextKey);
} - (void)setHdf_placeholderColor:(UIColor *)hdf_placeholderColor {
self.placeholderLabel.textColor = hdf_placeholderColor;
} - (UIColor *)hdf_placeholderColor {
return self.placeholderLabel.textColor;
} - (void)setHdf_placeholderFont:(UIFont *)hdf_placeholderFont {
self.placeholderLabel.font = hdf_placeholderFont;
} - (UIFont *)hdf_placeholderFont {
return self.placeholderLabel.font;
} @end
说明:这里的实现文件里使用了执行时机制(runtime)来实现,这里对文本改变的监听,交给了自己和UIApplication。是为了兼容到IOS6.0,在6.0下。交给自己是不可行的,会崩溃,因此移交给UIApplication单例对象来管理。

############提示:这里使用了Masonary这个自己主动布局的三方库。让这个placeholderlabel自己主动依据uitextview的大小变化而变化。代码中使用了推断IOS系统。推断是否为空串的代码,这里不写出来了,自己替掉就可以。

以下就是使用了,使用起来就非常easy了

  self.remarkView.text = @"哈哈,非常easy吧";
self.remarkView.hdf_placeholder = @"写点什么...";

备注:当需求中须要给textview加入placeholder时,网上的都是继承的方式。还须要改动非常多地方,这是不可行的。所以我才提出这个问题来,然后做出了自己的解决方式。假设对大家实用,请放心地拿去用吧。

兼容IOS6.0及其以上版本号

教大家怎样给UITextView加入placeholder扩展的更多相关文章

  1. UITextView实现placeHolder方法汇总

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

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

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

  3. UITextView 实现placeholder的方法

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

  4. UITextView实现PlaceHolder的方式

    实现UITextView实现PlaceHolder的方式的方式有两种,这两种方法的核心就是通过通知来添加和去除PlaceHolder:下面来介绍两种方法:个人比较喜欢第一种,看起来更加合理. 方法1: ...

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

    之前开发遇到过UITextField中加入一个PlaceHolder的问题,直接设置一下即可,不过这次是需要在UITextView中实现一个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. UITextView 实现placeholder

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

  9. iOS 设置UITextView的Placeholder

    代码如下: - (void)setupTextView { UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, ...

随机推荐

  1. 定制滚动条样式 webkit

    ::-webkit-scrollbar              { /* 1 */ } ::-webkit-scrollbar-button       { /* 2 */ } ::-webkit- ...

  2. Attribute在.NET编程中的应用(三)

    用于参数的Attribute 在编写多层应用程序的时候,你是否为每次要写大量类似的数据访问代码而感到枯燥无味?比如我们需要编写调用存储过程的代码,或者编写T_SQL代码,这些代码往往需要传递各种参数, ...

  3. linux系统下手动安装Angular-cli

    安装Angular-cli 背景 由于公司linux服务器没有外网,无法通过npm包管理器直接安装,只能手动安装一个Angular-cli平台环境! 安装步骤 1. 先再linux系统下安装好node ...

  4. JavaScript数组对象方法

    数组的方法:首先,数组的方法有数组原型方法,也有从object对象继承来的方法.这里只详细说明一下数组的原型方法. (1)join:把数组中的所有元素放入一个字符串.元素通过指定的分隔符进行分隔. 例 ...

  5. [最短路]信使(msner)

    [题目描述] 战争时期,前线有n个哨所,每个哨所可能会与其他若干个哨所之间有通信联系.信使负责在哨所之间传递信息,当然,这是要花费一定时间的(以天为单位).指挥部设在第一个哨所.当指挥部下达一个命令后 ...

  6. JS组件系列——Gojs组件,前端图形化插件之利器

    前言:之前分享过两篇关于流程画图的前端组件,使用的jsPlumb.这个组件本身还不错,使用方便.入门简单.轻量级,但是使用一段时间下来,发现一些弊病,比如组件不太稳定,初始进入页面的时候连线的样式有时 ...

  7. react native android6+拍照闪退或重启的解决方案

    前言 android 6+权限使用的时候需要动态申请,那么在使用rn的时候要怎么处理拍照权限问题呢?本文提供的是一揽子rn操作相册.拍照的解决方案,请看正文的提高班部分. 解决步骤 1.Android ...

  8. ASP.NET中HttpContext.Cache的使用

    -------------------------------键 --值-----依赖-----过期时间-------------------------------绝对过期------------- ...

  9. Python随笔------初探

    今年的双十一刚刚才过去,大多数人主要就是抢购商品,可能他们现在已经收到了他们夜以继日抢购的商品.然而对于我们做技术的,特别是做互联网技术的,我相信肯定都被双十一那天的许多技术震撼到了吧.云计算.分压式 ...

  10. EntityFramework For Mysql 动态切换数据源

    1.简介 在工作中遇到一个问题.项目有三个数据库(三个数据库表结构一样),用户可以选择使用哪个数据库.其实就是动态切换数据库连接. 2.EntityFramework For Mysql 先来简单的介 ...