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

我们继承一个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. linux 磁盘挂载操作

    1. fdisk  -l      查看磁盘   2. fisk  /dev/vdb   进行分区        依次输入  n  p  1   两次回车  wq   3. fdisk -l  查看分 ...

  2. Wordpress主题站

    深度剖析WordPress主题结构 http://down.chinaz.com/try/201106/640_1.htm wordpress工作原理 http://blog.csdn.net/liu ...

  3. 如何在CentOS系统中安装配置SNMP服务

    CentOS(Community Enterprise Operating System,中文意思是:社区企业操作系统)是Linux发行版之一,现在有一大部分服务器在使用此操作系统:SNMP(简单网络 ...

  4. c++深拷贝/浅拷贝

    拷贝构造函数,是一种特殊的构造函数,它由编译器调用来完成一些基于同一类的其他对象的构建及初始化.其唯一的参数(对象的引用)是不可变的(const类型).此函数经常用在函数调用时用户定义类型的值传递及返 ...

  5. 高并发的epoll+线程池,线程池专注实现业务

    我们知道,服务器并发模型通常可分为单线程和多线程模型,这里的线程通常是指“I/O线程”,即负责I/O操作,协调分配任务的“管理线程”,而实际的请求和任务通常交由所谓“工作者线程”处理.通常多线程模型下 ...

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

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

  7. guaua学习,工具专题

    Preconditions 1,http://www.cnblogs.com/peida/p/Guava_Preconditions.html 1 .checkArgument(boolean) : ...

  8. Python——深浅Copy

    1. 赋值 赋值:指向同一块内存地址,所以同时改变 l1 = [1,2,3] l2 = l1 l1.append('a') print(l1,l2) # [1, 2, 3, 'a'] [1, 2, 3 ...

  9. Xbox360游戏收藏

    xbox360游戏下载地址 http://dl.3dmgame.com/SoftList_221.html   XBLA游戏总结.http://tieba.baidu.com/p/3174478602 ...

  10. 以太坊客户端Geth命令用法

    命令用法 geth [选项] 命令 [命令选项] [参数…] 命令: account 管理账户attach 启动交互式JavaScript环境(连接到节点)bug 上报bug Issuesconsol ...