一.设置占位文字的颜色

方法一:利用富文本

/** 手机号输入框 */
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField; - (void)viewDidLoad {
[super viewDidLoad];
// 创建一个富文本对象
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
// 设置富文本对象的颜色
attributes[NSForegroundColorAttributeName] = [UIColor whiteColor];
// 设置UITextField的占位文字
self.phoneTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"手机号" attributes:attributes]; } 修改字体后 可能placeHolder不对其 用下面的方法

方法二:利用Runtime获取私有的属性名称,利用KVC设置属性

// 设置占位文字的颜色为红色(注意下面的'self'代表你要修改占位文字的UITextField控件)
[self setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
  • 注意:_placeholderLabel.textColor是不可乱写的哦,我们是怎么获取到这个属性的呢?请看下文:
// 只调用一次(自定义UITextField)
+ (void)initialize { [self getIvars]; } // 获取私有变量名称
+ (void)getIvars { unsigned int count = 0; Ivar *ivars = class_copyIvarList([UITextField class], &count); for (int i = 0; i < count; i++) {
Ivar ivar = ivars[i]; NSLog(@"%s----%s", ivar_getName(ivar), ivar_getTypeEncoding(ivar));
}
}

查看打印,找出可能的属性名称,试试便知;

  • 完整代码:自定义的UITextField,获取到焦点(编辑状态)的时候是白色,失去焦点(非编辑状态)的时候是灰色:
#import "YCTextField.h"
#import <objc/runtime.h> #define YCplaceholderTextColor @"_placeholderLabel.textColor" @implementation YCTextField + (void)initialize { [self getIvars]; } // 获取私有变量名称
+ (void)getIvars { unsigned int count = 0; Ivar *ivars = class_copyIvarList([UITextField class], &count); for (int i = 0; i < count; i++) {
Ivar ivar = ivars[i]; NSLog(@"%s----%s", ivar_getName(ivar), ivar_getTypeEncoding(ivar));
}
} - (void)awakeFromNib { // 设置光标的颜色
self.tintColor = self.textColor;
} // 获取到焦点
- (BOOL)becomeFirstResponder { // 利用运行时获取key,设置占位文字的颜色
[self setValue:self.textColor forKeyPath:YCplaceholderTextColor]; return [super becomeFirstResponder];
} // 失去焦点
- (BOOL)resignFirstResponder { // 利用运行时获取key,设置占位文字的颜色
[self setValue:[UIColor grayColor] forKeyPath:YCplaceholderTextColor]; return [super resignFirstResponder];
} @end

方法三.将占位文字上去(重写- (void)drawPlaceholderInRect:(CGRect)rect;)

- (void)drawPlaceholderInRect:(CGRect)rect
{ [[UIColor orangeColor] set]; [self.placeholder drawInRect:rect withFont:[UIFont systemFontOfSize:20]];
}

二.设置光标颜色

// 设置光标的颜色
self.tintColor = [UIColor redColor];

三.设置占位文字的偏移

  • 重写-(CGRect)placeholderRectForBounds:(CGRect)bounds;方法
  • 可以用来设置光标与占位的间距

    //控制placeHolder的位置,左右缩20
    -(CGRect)placeholderRectForBounds:(CGRect)bounds
    { //return CGRectInset(bounds, 20, 0);
    CGRect inset = CGRectMake(bounds.origin.x+50, bounds.origin.y, bounds.size.width -10, bounds.size.height);//更好理解些
    return inset;
    }
  • 扩充:系统还提供了很多类似的方法
    • – textRectForBounds:  //重写来重置文字区域
    • – drawTextInRect:    //改变绘文字属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了.
    • – placeholderRectForBounds:  //重写来重置占位符区域
    • – drawPlaceholderInRect:  //重写改变绘制占位符属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了
    • – borderRectForBounds:  //重写来重置边缘区域
    • – editingRectForBounds:  //重写来重置编辑区域
    • – clearButtonRectForBounds:  //重写来重置clearButton位置,改变size可能导致button的图片失真
    • – leftViewRectForBounds:
    • – rightViewRectForBounds:
文/YotrolZ(简书作者)
原文链接:http://www.jianshu.com/p/49b4eb97f41e
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

UITextField-修改占位文字和光标的颜色,大小的更多相关文章

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

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

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

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

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

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

  4. UITextField的placeholder文字的位置,颜色等的自定义设置

    //控制placeHolder的位置,左右缩20 -(CGRect)placeholderRectForBounds:(CGRect)bounds { CGRect inset = CGRectMak ...

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

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

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

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

  7. [BS-19]更改UITextField的placeholder文字颜色的5种方法

    更改UITextField的placeholder文字颜色的5种方法 想要达到的目标是:一个页面上有多个UITextField,当用户聚焦某textField时,该文本框的placeholder的文字 ...

  8. iOS修改TextField占位符颜色大小

    UITextField *addCtrolField = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(rightTitleLa ...

  9. iOS不得姐项目--登录模块的布局,设置文本框占位文字颜色,自定义内部控件竖直排列的按钮

    一.登录模块的布局 将一整部分切割成若干部分来完成,如图分成了三部分来完成 设置顶部状态栏为白色的方法 二.设置文本框占位文字颜色 <1>方法一与方法二实现原理是同一种,都是通过设置pla ...

随机推荐

  1. Unity3D NGUI动态生成模糊背景图

    先上效果. 制作原理:模糊的部分是用UITexture,前面是一个UISprite.用主摄像机渲染出一张纹理,把这张纹理模糊处理,把这张纹理赋值给UITexture. 脚本代码 using Unity ...

  2. ubuntu 安装vsftpd服务器 ftp

    http://www.cnblogs.com/likwo/p/3154868.html http://help.aliyun.com/knowledge_detail.htm?knowledgeId= ...

  3. oracle导入导出数据

    导入数据,cmd   imp 导出数据,cmd   exp

  4. JavaScript高级程序设计学习笔记--事件

    HTML事件处理程序 <input type="button" value="Click Me" onclick"showMessage()&q ...

  5. LeetCode 27 Remove Element

    Problem: Given an array and a value, remove all instances of that value in place and return the new ...

  6. “ifstream” 未声明的标识符

    #include <fstream> 还要加入: using  namespace std;

  7. hadoop 笔记

    我们常说的分布式系统,其实就是分布式软件系统,支持分布式处理的软件系统.他是在通信网络互联的多处理机体系结构上执行任务.   hadoop是分布式软件系统中文件系统层的软件,他实现了分布式文件系统和部 ...

  8. 【chrome插件】web版微信接入图灵机器人API实现自动回复

    小贱鸡自动回复API已经不可以用了,现在改良接入图灵机器人API 360chrome浏览器团队翻译了部分谷歌插件开发文档 地址:http://open.chrome.360.cn/extension_ ...

  9. Xcode修改storyboard大小

    1: 2:

  10. 《DSP using MATLAB》示例Example5.12

    代码: n = 0:10; x = 10*(0.8) .^ n; y = cirshftt(x,6,15); n = 0:14; x = [x, zeros(1,4)]; %% ----------- ...