Files
knowledge-kit/Chapter1 - iOS/1.22.md
2020-02-25 17:46:51 +08:00

36 lines
1.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 修改 UITextField 的 placeholder样式
> 对于 UITextField 的 placeholder 私有属性来说 Apple 不允许我们直接修改但是按照经验我们有2种方式可以实现自定义 placeholder 的样式
### 1、利用 KVC 对 UITextField 的私有属性修改
```
[self.invitecodeTextfield setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
[self.invitecodeTextfield setValue:[UIFont systemFontOfSize:35] forKeyPath:@"_placeholderLabel.font"];
```
### 2、利用 Apple 提供的 API 进行修改
UITextField 有个属性 attributedPlaceholder利用它我们可以修改 placeholder 的样式
```
self.invitecodeTextfield.attributedPlaceholder = [LBPHightedAttributedString setAllText:@"我要Testing" andSpcifiStr:@"Testing" withColor:[UIColor redColor] specifiStrFont:[UIFont systemFontOfSize:17]];
```
其中 **LBPHightedAttributedString** 是我封装的一个关于 NSMutableAttributedString 的工具,可以对一个指定的字符串内部的字符串进行全局查找并高亮设置的小工具,具体可以查看地址
[LBPHightedAttributedString](https://github.com/FantasticLBP/BlogDemos/tree/master/LBPAttributedStringTools/LBPHightedAttributedString "LBPHightedAttributedString")