您的位置:首页 > 产品设计 > UI/UE

14.UITextField内容缩进/placeholder改变颜色

2016-02-19 17:13 531 查看
UITextField 里面的字体内容如何缩进?

如何修改 UITextField 的placeholder 的颜色呢?

1.也许你会想到在 textField的底层加一层 UIView, 其实有一个更简单的技巧,就是通过其自身属性leftView去控制内容缩进。

UITextField *putInTF = [[UITextField alloc]initWithFrame:CGRectMake(10, 50, 200, 35)];
putInTF.textAlignment = NSTextAlignmentLeft;
putInTF.backgroundColor = [UIColor greenColor];
putInTF.placeholder = @"请输入";
[self.view addSubview:putInTF];

UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 0)];
leftView.backgroundColor = [UIColor whiteColor];
putInTF.leftView = leftView;
putInTF.leftViewMode = UITextFieldViewModeAlways;
.


leftViewMode

UITextFieldViewModeNever, 从不存在

UITextFieldViewModeWhileEditing, 编辑的时候存在

UITextFieldViewModeUnlessEditing, 非编辑的时候存在

UITextFieldViewModeAlways 一直存在

leftView添加前:



leftView添加后:



2.改变UITextField 的placeholder 颜色

其实通过NSAttributedString就可以来控制 placeholder 的颜色。

UITextField *putInTF = [[UITextField alloc]initWithFrame:CGRectMake(10, 50, 200, 35)];
putInTF.textAlignment = NSTextAlignmentLeft;
putInTF.backgroundColor = [UIColor greenColor];
[self.view addSubview:putInTF];

putInTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入" attributes:@{NSForegroundColorAttributeName:[UIColor redColor]}];
.


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: