14.UITextField内容缩进/placeholder改变颜色
2016-02-19 17:13
531 查看
UITextField 里面的字体内容如何缩进?
如何修改 UITextField 的placeholder 的颜色呢?
1.也许你会想到在 textField的底层加一层 UIView, 其实有一个更简单的技巧,就是通过其自身属性leftView去控制内容缩进。
leftViewMode
UITextFieldViewModeNever, 从不存在
UITextFieldViewModeWhileEditing, 编辑的时候存在
UITextFieldViewModeUnlessEditing, 非编辑的时候存在
UITextFieldViewModeAlways 一直存在
leftView添加前:
![](http://img.blog.csdn.net/20160219170537610)
leftView添加后:
![](http://img.blog.csdn.net/20160219170548003)
2.改变UITextField 的placeholder 颜色
其实通过NSAttributedString就可以来控制 placeholder 的颜色。
如何修改 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]}]; .
相关文章推荐
- UIbutton 防止重复点击
- No location, value may have been optimized out
- UITableView回滚到顶部的几种方法
- 深入学习:NSOperationQueue, NSRunLoop和线程安全
- ReferenceQueue
- UI总结
- UIButton.titleLabel.text和[UIButton setTitle:],UIButton.imageView.image和[UIButton setImage]的区别
- UICollectionView
- UISlider增加触动区域
- UICollectionView
- UIScrollView的三个属性
- WinCE Overlay - 示例:mosquito
- 22个值得收藏的android开源代码-UI篇
- 毕业设计(二十)---使用easyUI dialog 对话框做登陆弹出 表单验证
- 13.UILabel 中的一些属性
- Multiple build commands for output file
- 使用BlockingQueue实现包饺子( 生产者做面皮,消费者拿面皮包饺子)简单使用
- 《代码大全》之错误处理技术(Error-Handling Techniques)
- QT笔记:数据库总结(二)之SQL模型类-QSqlQueryModel模型
- UITabbarController左右滑动切换标签页