您的位置:首页 > 移动开发 > IOS开发

ios修改textField的placeholder的字体颜色大小

2016-04-27 10:41 716 查看


ios修改textField的placeholder的字体颜色大小


前言

设置UITextField的placeholder的颜色代码片段:

1

2

3

4

5

textField.placeholder
=
@"username is in here!";

[textField
setValue:[UIColor
redColor]
forKeyPath:@"_placeholderLabel.textColor"];

[textField
setValue:[UIFont
boldSystemFontOfSize:16]
forKeyPath:@"_placeholderLabel.font"];

或者直接在iOS6.0之后提供的attributedPlaceholder属性:

1

2

3

4

5

6

7

8

9

10

11

12

13

UITextField
*textField
=
[[UITextField
alloc]
initWithFrame:CGRectMake(0,
0,
200,
200)];

NSString
*holderText
=
@"标哥的技术博客";

NSMutableAttributedString
*placeholder
=
[[NSMutableAttributedString
alloc]
initWithString:holderText];

[placeholder
addAttribute:NSForegroundColorAttributeName

value:[UIColor
redColor]

range:NSMakeRange(0,
holderText.length)];

[placeholder
addAttribute:NSFontAttributeName

value:[UIFont
boldSystemFontOfSize:16]

range:NSMakeRange(0,
holderText.length)];

textField.attributedPlaceholder
=
placeholder;

[cell.contentView
addSubview:textField];

与上面那段代码是一样的效果。


_placeholderLabel说明

1

2

3

4

(lldb)
po
[textField
valueForKey:@"_placeholderLabel"]

<UITextFieldLabel:
0x13fe835f0;
frame
=
(0
0;
0
0);
text
=
'标哥的技术博客';
opaque
=
NO;
userInteractionEnabled
=
NO;
layer
=
<_UILabelLayer:
0x13fe855f0>>

其实_placeholderLabel就是UITextFieldLabel类型,这是在有placeholder的情况下打印出来的,但是为什么知道内部叫\ _placeholderLabel呢?根据苹果的命名规范,猜测出来的,然后测试能否获取到。这不算私有API,这是通过KVC获取的,虽然苹果并不希望我们这么做,但是可以正常上架(笔者在很多个App里使用过)。


说明

iOS6.0之后,有attributedPlaceholder属性,因此可以直接通过它设置。

在iOS6.0之前,可以通过KVC来设置_placeholderLabel的属性值。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: