mirror of
https://github.com/NohamR/knowledge-kit.git
synced 2026-05-24 20:00:37 +00:00
36 lines
1.2 KiB
Markdown
36 lines
1.2 KiB
Markdown
# 修改 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")
|
||
|
||
|
||
|
||
|