转自:http://blog.csdn.net/mazy_ma/article/details/51775670

有时,UITextField自带的Placeholder的颜色太浅或者不满足需求,所以需要修改,而UITextField没有直接的属性去修改Placeholder的颜色,所以只能通过其他间接方式去修改。

例如:系统默认的Placeholder颜色太浅

需要加深颜色,或者改变颜色

方法一:通过attributedPlaceholder属性修改Placeholder颜色

    CGFloat viewWidth  = self.view.bounds.size.width;
CGFloat textFieldX = 50;
CGFloat textFieldH = 30;
CGFloat padding = 30; UITextField *textField = [[UITextField alloc] init];
textField.frame = CGRectMake(textFieldX, 100, viewWidth - 2 * textFieldX, textFieldH);
textField.borderStyle = UITextBorderStyleRoundedRect; // 边框类型
textField.font = [UIFont systemFontOfSize:14];
// 就下面这两行是重点
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"请输入占位文字" attributes:
@{NSForegroundColorAttributeName:[UIColor redColor],
NSFontAttributeName:textField.font
}];
textField.attributedPlaceholder = attrString;
[self.view addSubview:textField];

方法二:通过KVC修改Placeholder颜色

    UITextField *textField1 = [[UITextField alloc] init];
textField1.frame = CGRectMake(textFieldX, CGRectGetMaxY(textField.frame) + padding, viewWidth - 2 * textFieldX, textFieldH);
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.placeholder = @"请输入占位文字";
textField1.font = [UIFont systemFontOfSize:14];
// "通过KVC修改占位文字的颜色"
[textField1 setValue:[UIColor greenColor] forKeyPath:@"_placeholderLabel.textColor"];
[self.view addSubview:textField1];

方法三:通过重写UITextField的drawPlaceholderInRect:方法修改Placeholder颜色

1、自定义一个TextField继承自UITextField

2、重写drawPlaceholderInRect:方法

3、在drawPlaceholderInRect方法中设置placeholder的属性

// 重写此方法
-(void)drawPlaceholderInRect:(CGRect)rect {
// 计算占位文字的 Size
CGSize placeholderSize = [self.placeholder sizeWithAttributes:
@{NSFontAttributeName : self.font}]; [self.placeholder drawInRect:CGRectMake(0, (rect.size.height - placeholderSize.height)/2, rect.size.width, rect.size.height) withAttributes:
@{NSForegroundColorAttributeName : [UIColor blueColor],
NSFontAttributeName : self.font}];
}

总结:

1、当我们使用纯代码创建UITextField时,用第二种方法(KVC)修改占位文字颜色是最便捷的 。

2、当我们使用XIB或者Storyboard创建UITextField时,通过自定义UITextField,修改占位文字颜色是最适合的。

3、我们也可以在第三种重写方法中,通过结合第二种方法中的KVC修改属性来实现。

iOS-改变UITextField的Placeholder颜色的三种方式的更多相关文章

  1. Latex中如何设置字体颜色(三种方式)

    1.直接使用定义好的颜色 \usepackage{color} \textcolor{red/blue/green/black/white/cyan/magenta/yellow}{text} 其中t ...

  2. 改变UITextField的Placeholder颜色

    通过 attributedPlaceholder 属性来改变 if([textField respondsToSelector:@selector(setAttributedPlaceholder:) ...

  3. iOS 改变UITextField中光标颜色

    第一种: [[UITextField appearance] setTintColor:[UIColor blackColor]]; 这个方法会影响整个app的所有UITextFiled... 第二种 ...

  4. placeholder 设置换行三种方式

    在 html 中编写代码时保留代码换行 <textarea name="" id="" cols="30" rows="10 ...

  5. Latex中如何设置字体颜色(3种方式)

    Latex中如何设置字体颜色(三种方式)   1.直接使用定义好的颜色 \usepackage{color} \textcolor{red/blue/green/black/white/cyan/ma ...

  6. iOS UITextField设置placeholder颜色

    设置UITextField的placeholder颜色 UIColor *color = [UIColor blackColor]; textField.attributedPlaceholder = ...

  7. iOS字体加载三种方式

    静态加载 动态加载 动态下载苹果提供的多种字体 其他 打印出当前所有可用的字体 检查某字体是否已经下载 这是一篇很简短的文章,介绍了 iOS 自定义字体加载的三种方式. 静态加载 这个可以说是最简单最 ...

  8. iOS --- UIWebView的加载本地数据的三种方式

    UIWebView是IOS内置的浏览器,可以浏览网页,打开文档  html/htm  pdf   docx  txt等格式的文件.  safari浏览器就是通过UIWebView做的. 服务器将MIM ...

  9. iOS拨打电话的三种方式

    iOS拨打电话的三种方式 1,这种方法,拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示 1 2 var string = "tel:" + "1 ...

随机推荐

  1. 1018关于MySQL复制搭建[异步复制和半同步复制]

    转自:http://www.cnblogs.com/ivictor/p/5735580.html 搭建MySQL数据库的主从架构,还是蛮简单的.重要的几个命令整理一下. 主从服务器上: SHOW VA ...

  2. eclipse下maven插件搭建springmvc之helloworld

    这几天学习了怎样使用maven,最终还是要回归web项目嘛,所以主要还是使用eclipse插件. 1 下载eclipse maven插件. 其实新版的eclipse已经集成了maven:lunar.m ...

  3. SpringIOC学习二

    Spring的IOC容器通过依赖注入DI(dependency injection)来实现程序之间的依赖关系,达到解耦的方式依赖的方式:a.基于xml文件配置的注入    * 构造函数注入    * ...

  4. [Apio2009]Atm

    题目描述 输入 第一行包含两个整数N.M.N表示路口的个数,M表示道路条数.接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i条道路的起点和终点的路口编号.接下来N行,每 ...

  5. [Codeforces]853E - Lada Malina

    题目大意:给出平面上$n$个带权点$f_{i}$,再给出$k$个向量$v_{i}$,每次询问给出一个点$p$和一个值$t$,求能满足$f_{i}+\sum w_{j}v_{j}=p(-t<=w_ ...

  6. ●BZOJ 3238 [Ahoi2013]差异

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3238 题解: 后缀数组套路深. 问题转化为求出任意两个后缀的LCP之和 在计算贡献时,各种不 ...

  7. Codeforces 2B. The least round way

    There is a square matrix n × n, consisting of non-negative integer numbers. You should find such a w ...

  8. 例10-7 uva10820(欧拉)

    题意:输入n,要求满足1≤x,y≤n,且x,y互素的个数. 若输入2,则答案3为(1,1),(1,2),(2,1);所以欧拉函数求出所有数的phi值,除了1之外都加上phi值的2倍即可 通过推导: p ...

  9. hdu 4777 树状数组+合数分解

    Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  10. Python中文件的操作

    文件的操作介绍 文件打开的方法 主要有两种: no with 格式:open(file, mode='r', buffering=-1, encoding=None, errors=None, new ...