UITextView设置placeholder
下面是我的代码,可以直接拿来用
#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的更多相关文章
- UITextView实现placeHolder方法汇总
UITextField中有一个placeholder属性,可以设置UITextField的占位文字,起到提示用户的作用.可是UITextView就没那么幸运了,apple没有给UITextView提供 ...
- iOS TextView输入长度限制 设置placeholder
textView在使用中通常会有2个功能是最常用的 设置placeholder 限制输入长度 TYLimitedTextView刚好是为了解决这个2个问题而诞生的,下面讲解TYLimitedTextV ...
- 教大家怎样给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的方式
实现UITextView实现PlaceHolder的方式的方式有两种,这两种方法的核心就是通过通知来添加和去除PlaceHolder:下面来介绍两种方法:个人比较喜欢第一种,看起来更加合理. 方法1: ...
- 【代码笔记】iOS-UITextField设置placeholder颜色
一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...
- 设置placeholder字体颜色
/*设置placeholder字体颜色*/::-webkit-input-placeholder{ color: #FFF;}:-ms-input-placeholder{ color: #FFF;} ...
- UITextfield设置Placeholder颜色 控件 内边距、自适应高度
//创建UITextField对象 UITextField * tf=[[UITextField alloc]init]; //设置Placeholder颜色 [text setAttribut ...
随机推荐
- PowerDesigner 16PDM显示备注
默认显示的列是Name及类型 name和code不支持同时显示,设置方法: Tool->Model Options->Name Convention->右侧display中选择显示n ...
- AngularJS中$timeout和$interval的用法详解
1. 先将$interval,$timeout,作为参数注入到controller中,例如rds.controller('controllerCtrl', ['app', '$scope','$htt ...
- 仿迅雷播放器教程 -- 提取exe资源(12)
既然选择了一个界面库,那么咱们就开始吧! 既然是仿迅雷播放器,那当然要把迅雷播放器的资源提取出来啦,但是很多小伙伴可能不知道怎么提取,所以这里就教大家一些方法: 一.传统的资源提取器 ...
- 分辨率,PPi,DPI,DPR,物理像素,逻辑像素
屏幕尺寸:指的是屏幕对角线的长度 分辨率:是指宽度上和高度上最多能显示的物理像素点个数 点距:像素与像素之间的距离,点距和屏幕尺寸决定了分辨率大小 PPI:屏幕像素密度,即每英寸(1英寸=2.54厘米 ...
- 【代码审计】XYHCMS V3.5代码执行漏洞分析
0x00 环境准备 XYHCMS官网:http://www.xyhcms.com/ 网站源码版本:XYHCMS V3.5(2017-12-04 更新) 程序源码下载:http://www.xyhc ...
- Android开发训练之第五章第六节——Transferring Data Using Sync Adapters
Transferring Data Using Sync Adapters GET STARTED DEPENDENCIES AND PREREQUISITES Android 2.1 (API Le ...
- Redmine发布新闻,自动发送邮件功能失效恢复
问题描述:操作数据库,修改项目公开状体后,创建新闻,自动发送邮件功能失效. 问题检查和恢复测试: 1.检查管理员权限 2.重新手动配置项目公开状态,覆盖数据操作 3.对比其他正常项目数据库状态,进行恢 ...
- F - Communication System
We have received an order from Pizoor Communications Inc. for a special communication system. The sy ...
- Convert PLY to VTK Using PCL 1.6.0 or PCL 1.8.0 使用PCL库将PLY格式转为VTK格式
PLY格式是比较流行的保存点云Point Cloud的格式,可以用MeshLab等软件打开,而VTK是医学图像处理中比较常用的格式,可以使用VTK库和ITK库进行更加复杂的运算处理.我们可以使用Par ...
- Servlet3.0 multipart 文件上传技术
Servlet3.0 javaConfig配置 传统的servlet都是在web.xml中配置,从Servlet 3.0开始提供了ServletContainerInitializer接口,允许使用代 ...