下面是我的代码,可以直接拿来用

#import <UIKit/UIKit.h>

@interface CustomTextView : UITextView
@property(nonatomic,strong)NSString *placeholder;
@end
#import "CustomTextView.h"

@interface CustomTextView ()
@property(nonatomic,strong)UILabel *placeLab;
@end @implementation CustomTextView -(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if(self){ self.placeLab = [UILabel new];
self.placeLab.textColor = [ImageService colorWithHexString:GRAY_COLOR];
[self addSubview:self.placeLab];
self.placeLab.textAlignment = NSTextAlignmentRight;
[self.placeLab mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.offset(5);
make.width.equalTo(self.mas_width).offset(-10);
}];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(beginChange) name:UITextViewTextDidBeginEditingNotification object:self];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didChange) name:UITextViewTextDidChangeNotification object:self]; }
return self;
}
-(void)setFont:(UIFont *)font {
[super setFont:font];
self.placeLab.font = font;
}
-(void)setTextAlignment:(NSTextAlignment)textAlignment{
[super setTextAlignment:textAlignment];
self.placeLab.textAlignment = textAlignment;
} -(void)setPlaceholder:(NSString *)placeholder{
_placeholder = placeholder;
self.placeLab.text = placeholder;
} -(void)beginChange{
if([self.text isEqualToString:@""]){
self.placeLab.hidden = NO;
}else{
self.placeLab.hidden = YES;
}
}
-(void)didChange{
if([self.text isEqualToString:@""]){
self.placeLab.hidden = NO;
}else{
self.placeLab.hidden = YES;
}
}
-(void)setText:(NSString *)text{
[super setText:text];
[self beginChange];
} @end
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidBeginEditingNotification object:self];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:self];
}

UITextView设置placeholder的更多相关文章

  1. UITextView实现placeHolder方法汇总

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

  2. iOS TextView输入长度限制 设置placeholder

    textView在使用中通常会有2个功能是最常用的 设置placeholder 限制输入长度 TYLimitedTextView刚好是为了解决这个2个问题而诞生的,下面讲解TYLimitedTextV ...

  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的方式

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

  7. 【代码笔记】iOS-UITextField设置placeholder颜色

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...

  8. 设置placeholder字体颜色

    /*设置placeholder字体颜色*/::-webkit-input-placeholder{ color: #FFF;}:-ms-input-placeholder{ color: #FFF;} ...

  9. UITextfield设置Placeholder颜色 控件 内边距、自适应高度

    //创建UITextField对象 UITextField * tf=[[UITextField alloc]init];    //设置Placeholder颜色 [text setAttribut ...

随机推荐

  1. mongodb查询数据库中某个字段中的值包含某个字符串的方法

    正则表达式最能解决: 例如: db.getCollection(.*$/}) 这里主要是注意正则表达式要写对,该转义的注意转义,否则报错.

  2. js函数作用域

    函数 1.函数没有用return返回函数时,返回默认参数undefined 结果 return返回得话 就是里面得数值 结果 JS执行过程是上到下,下面的a元素覆盖了上面的a元素 function d ...

  3. C# Timer 定时器

    Timer简单介绍 构造函数 通过ILSpy反编译可以看到Timer的内部代码. Timer 的默认构造函数里赋了初值,所以当我们初始化Timer的时候,已经有了默认值. Timer 的带参数的构造函 ...

  4. 7.11js常用对象

    <!DOCTYPE html> <html> <head> <title>js常用对象</title> <script type=&q ...

  5. H - Being a Good Boy in Spring Festival

    一年在外 父母时刻牵挂 春节回家 你能做几天好孩子吗 寒假里尝试做做下面的事情吧 陪妈妈逛一次菜场 悄悄给爸爸买个小礼物 主动地 强烈地 要求洗一次碗 某一天早起 给爸妈用心地做回早餐 如果愿意 你还 ...

  6. 在浏览器中输入 www.baidu.com 后执行的全部过程

    现在假设如果我们在客户端(客户端)浏览器中输入http://www.baidu.com,而baidu.com为要访问的服务器(服务器),下面详细分析客户端为了访问服务器而执行的一系列关于协议的操作: ...

  7. 关于ICO的一些理解

    第一次看到ICO,估计很多人都处于懵逼的状态,感觉很抽象. 提到IOC可能想到的下一个词语就是DI IOC:控制反转 DI:依赖注入 那么什么是控制反转呢? 我以前对这个概念也很模糊,最近在知乎上看到 ...

  8. python如何去掉字符串‘\xa0’

    发现问题: 最近在用爬虫爬取网页信息时,在网页源码中遇到了“&nbsp"字符串,经查阅,发现该字符是不间断空格符.我们通常所用的空格是 \x20 ,是在标准ASCII可见字符 0x2 ...

  9. 操作系统中的IPC机制(inter-process Communication)

    http://www.cnblogs.com/tsiangleo/p/4902695.html

  10. .NET Core开发日志——HttpContext

    之前的文章记述了从ASP.NET Core Module到KestrelServer的请求处理过程.现在该聊聊如何生成ASP.NET中我们所熟悉的HttpContext. 当KestrelServer ...