UITextField内容缩进/placeholder改变颜色

来源:互联网 发布:mysql 参数化 编辑:程序博客网 时间:2024/04/29 00:49

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;.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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]}];.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

这里写图片描述

0 0