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

#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. Java Web项目中连接Access数据库的配置方法

    本文是对前几天的"JDBC连接Access数据库的几种方式"这篇的升级.因为在做一些小项目的时候遇到的问题,因此才决定写这篇博客的.昨天已经将博客公布了.可是后来经过一些验证有点问 ...

  2. 【Math】根据置信度、样本数相关推导过程

    时间长了会忘,备忘下. http://blog.csdn.net/liangzuojiayi/article/details/78044780 http://wiki.mbalib.com/wiki/ ...

  3. 仿迅雷播放器教程 -- C++ 100款开源界面库 (10)

      (声明:Alberl以后说到开源库,一般都是指著名的.或者不著名但维护至少3年以上的.那些把代码一扔就没下文的,Alberl不称之为开源库,只称为开源代码.这里并不是贬低,像Alberl前面那个系 ...

  4. android SQLite数据库的基本操作

    SQLite是Android使用的轻量级的数据库,开发Android应用是对数据库的操作自然是必不可少. Android提供了一个SQLiteOpenHelper类来可以很方便的操作数据库, 继承和扩 ...

  5. [DQN] What is Deep Reinforcement Learning

    已经成为DL中专门的一派,高大上的样子 Intro: MIT 6.S191 Lecture 6: Deep Reinforcement Learning Course: CS 294: Deep Re ...

  6. UML中类结构图示例

  7. fclose函数无响应

    现象:win32程序在退出时无响应,当一步步跟踪代码时走到fclose. 原因:打开文件在一个线程中,写文件时在另一个线程,在open和write文件时均正常,只有在fclose时出现无响应. 解决: ...

  8. 设计模式学习--Abstarct Factory

    What Abstarct Factory:提供一个创建一系列相关或相互依赖的接口,而无需指定他们具体类. Why Abstarct Factory是创建型设计模式的一种,主要在创建对象时解耦,避免对 ...

  9. GDB常用调试命令以及多进程多线程调试

    http://blog.csdn.net/freeelinux/article/details/53700266 一:普通命令   1.list命令 list  linenum      显示程序第l ...

  10. 关于SQL SERVER中的FLOAT转换为VARCHAR

    关于SQL SERVER中的FLOAT转换为VARCHAR 一个FLOAT型的字段,要转换为VARCHAR,可是小数点后面的都自动被删去了...后查得可以通过如下转换获得: SELECT CAST(C ...