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]}]
.