这里只介绍一种,这种方便扩展,可以占位文字颜色.

我们继承一个UITextView:

#import <UIKit/UIKit.h>

@interface MyTextView : UITextView

/** 占位文字 */
@property (nonatomic, copy) NSString *placeholder;
/** 占位文字颜色 */
@property (nonatomic, strong) UIColor *placeholderColor; @end
#import "MyTextView.h"

@implementation MyTextView

- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
// 设置默认字体
self.font = [UIFont systemFontOfSize:]; // 设置默认颜色
self.placeholderColor = [UIColor grayColor]; // 使用通知监听文字改变
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextViewTextDidChangeNotification object:self];
}
return self;
} - (void)textDidChange:(NSNotification *)note
{
// 会重新调用drawRect:方法
[self setNeedsDisplay];
} - (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
} /**
* 每次调用drawRect:方法,都会将以前画的东西清除掉
*/
- (void)drawRect:(CGRect)rect
{
// 如果有文字,就直接返回,不需要画占位文字
if (self.hasText) return; // 属性
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSFontAttributeName] = self.font;
attrs[NSForegroundColorAttributeName] = self.placeholderColor; // 画文字
rect.origin.x = ;
rect.origin.y = ;
rect.size.width -= * rect.origin.x;
[self.placeholder drawInRect:rect withAttributes:attrs];
} - (void)layoutSubviews
{
[super layoutSubviews]; [self setNeedsDisplay];
} #pragma mark - setter
- (void)setPlaceholder:(NSString *)placeholder
{
_placeholder = [placeholder copy]; [self setNeedsDisplay];
} - (void)setPlaceholderColor:(UIColor *)placeholderColor
{
_placeholderColor = placeholderColor; [self setNeedsDisplay];
} - (void)setFont:(UIFont *)font
{
[super setFont:font]; [self setNeedsDisplay];
} - (void)setText:(NSString *)text
{
[super setText:text]; [self setNeedsDisplay];
} - (void)setAttributedText:(NSAttributedString *)attributedText
{
[super setAttributedText:attributedText]; [self setNeedsDisplay];
}

调用:

    MyTextView *my = [[MyTextView alloc] initWithFrame:CGRectMake(, , [UIScreen mainScreen].bounds.size.width-, )];
my.placeholder = @"请输入文字";
//不设置颜色,默认是灰色.
my.placeholderColor = [UIColor orangeColor];
//添加边框
my.layer.borderColor = [UIColor grayColor].CGColor; my.layer.borderWidth =1.0; my.layer.cornerRadius =5.0; [self.view addSubview:my];

UITextView设置占位文字的更多相关文章

  1. iOS UITextView placeHolder占位文字的N种方法实现方法

    方法一 1.把UITextView的text属性当成“placeholder”使用. 2.在开始编辑的代理方法里清除“placeholder”. 3.在结束编辑的代理方法里根据条件设置“placeho ...

  2. iOS中用UILabel实现UITextView的占位文字

    @interface BSPublishTextView : UITextView /** 对外属性占位字符 placeholder */ @property (nonatomic, copy) NS ...

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

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

  4. 简易封装一个带有占位文字的TextView

    在实际iOS应用开发中我们经常会用到类似于下图所示的界面,即带有占位文字的文本框:

  5. AJ学IOS 之微博项目实战(11)发送微博自定义TextView实现带占位文字

    AJ分享,必须精品 一:效果 二:代码: 由于系统自带的UITextField:和UITextView:不能满足我们的需求,所以我们需要自己设计一个. UITextField: 1.文字永远是一行,不 ...

  6. UITextField-修改占位文字和光标的颜色,大小

    一.设置占位文字的颜色 方法一:利用富文本 /** 手机号输入框 */ @property (weak, nonatomic) IBOutlet UITextField *phoneTextField ...

  7. 修改UITextField的占位文字颜色的三种层次

    层次一:利用富文本 // 描述占位文字属性 NSMutableDictionary *dict = [NSMutableDictionary dictionary] ; dict[NSForegrou ...

  8. ios_UITextField-修改占位文字和光标的颜色,大小

    一.设置占位文字的颜色 方法一:利用富文本 /** 手机号输入框 */ @property (weak, nonatomic) IBOutlet UITextField *phoneTextField ...

  9. iOS开发中设置UITextField的占位文字的颜色,和光标的颜色

    在iOS开发中,对于很多初学者而言,很有可能碰到需要修改UITextField的占位文字的颜色,以及当UITextField成为第一响应者后光标的颜色,那么下面小编就介绍一下修改占位文字和光标的颜色. ...

随机推荐

  1. openresty 使用 log_by_lua 发送日志到 syslog-ng

    1. 安装   opm get p0pr0ck5/lua-resty-logger-socket   2. 使用    location   lua_by_lua_block    log_by_lu ...

  2. Docker阿里云镜像加速器 for CentOS 7

    CentOS 7 CentOS使用配置方式略微复杂,需要先将默认的配置文件复制出来 /lib/systemd/system/docker.service -> /etc/systemd/syst ...

  3. Oracle 之 Cloning $oracle_home (克隆安装oracle软件)

    用途:Cloning an Oracle Home , 可以免去多台机器重复安装oracle软件 1.停止相关进程[root@node1 bin]# ./crsctl stop cluster -al ...

  4. 在vue中无论使用router-link 还是 @click事件,发现都没法从列表页点击跳转到内容页去

    在vue中如论使用router-link 还是 @click事件,发现都没法从列表页点击跳转到内容页去,以前都是可以的,想着唯一不同的场景就是因为运用了scroll组件(https://ustbhua ...

  5. 老齐python-基础3(列表)

    1.定义一个列表 >>> a = [] #创建一个空列表 >>> type(a) #查看数据类型 <class 'list'> >>> ...

  6. EMguCV搭建第一个程序

    这篇博客旨在教学Emgucv3.0的安装与配置. 环境:vs2013+Emgucv3.0 Emgu Cv简介: Emgu CV 是.NET平台下对OpenCV图像处理库的封装.也就是opencv的.N ...

  7. xshell 提示 继续使用此程序必须应用到最新的更新或使用新版本 的解决方案

    当打开正在使用的xshell后,提示“继续使用此程序必须应用到最新的更新或使用新版本 ”  是因为我们正在使用的是xshell5 版本,需要我们再安装一个xshell6 版本 我个人使用的是家庭/教育 ...

  8. python中包和模块的使用说明

    python中,每个py文件被称之为模块,每个具有__init__.py文件的目录被称为包.只要模块或者包所在的目录在sys.path中,就可以使用import 模块或import 包来使用. 如果想 ...

  9. POJ-2282题解&数位DP总结

    一.题意 给定一个区间[a, b](注意输入的时候可能a > b,所以,在数据输入后,要先比较a和b,如果a > b,交换a和b的值),统计这个区间里面,数位上有多少个0.多少个1.--. ...

  10. 【UVa】1374 Power Calculus(IDA*)

    题目 题目     分析 IDA*大法好,抄了lrj代码.     代码 #include <cstdio> #include <cstring> #include <a ...