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

#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. Android图片处理(Matrix,ColorMatrix)

    转发说明:原文链接http://www.cnblogs.com/leon19870907/articles/1978065.html 在编程中有时候需要对图片做特殊的处理,比如将图片做出黑白的,或者老 ...

  2. Python 中filter函数用法

    filter()和map一样,接收一个函数和一个序列.和map不同的是,filter()把传入的函数依次作用于每个元素,然后根据返回值是True还是False决定保留还是丢弃该元素 过滤出奇数: de ...

  3. [Node.js] 08 - Web Server and REST API

    有了 [Node.js] 07 - Html and Http 作为基础,再继续下面的内容. Node.js 路由 Node.js GET/POST请求 Node.js Web 模块 Node.js ...

  4. [Android] 基于 Linux 命令行构建 Android 应用(二):命令行管理项目

    创建 Android 项目 在命令行创建 Android 项目需要用到 android 工具(该工具由 Android SDK 提供,位于 <sdk>/tools/ 目录下.).它能自动生 ...

  5. PHP利用ImageMagick把PDF转成PNG

    http://blog.csdn.net/longaction2012/article/details/12257867

  6. php面试题之一——HTML+CSS(基础部分)

    一.HTML + CSS部分 1. 请说明 HTML 文档中 DTD 的意义和作用(酷讯) DTD,文档类型定义,是一种保证 html 文档格式正确的有效方法,在解析网页时,浏览器将使用 DTD 来检 ...

  7. 10.21CRM项目(01)

    2018-10-21 13:35:19 crm第一天!放上初始源码! 后面等做完最后一天的手放上所有源码! 越努力越幸运!永远不要高估自己! 注意 多层for循环的时候,切记,不要名字重复!啦!!!! ...

  8. day_6.5 py

    Wireshark的使用  2018-6-5 20:16:05 明天学 03

  9. node.js 简单的获取命令参数

    class Argvs { constructor() { this.argvsAll = this.argvsAll(); } argvsAll() { return process.argv.sl ...

  10. 在Ubuntu上安装Chrome Driver和Firefox Driver

    在Ubuntu上安装Chrome Driver和Firefox Driver 此文章只介绍Chrome Driver(Firefox Driver和该步骤相同) 下载链接:http://chromed ...