标签:style blog class code c tar
iOS6 and Later 改变UITextField 中占位符 提示文本的文字颜色
在新版本中(iOS6以后)iOS提供一种 Key = value 属性的方式,来改变UI的属性内容。
以UITextField为例
|
1 |
@property(nonatomic,copy) NSAttributedString
*attributedText NS_AVAILABLE_IOS(6_0); // default is nil |
attributedText 为UITextField 的 public 属性 可以通过键值对的方式来改变 UITextField的内容
例如:
|
1
2
3
4
5
6
7
8 |
if([textField respondsToSelector:@selector(setAttributedPlaceholder:)]{ UIColor*color =[UIColor blackColor]; textField.attributedPlaceholder =[[NSAttributedString
alloc] initWithString:placeholderText attributes:@{NSForegroundColorAttributeName: color}];}else{ NSLog(@"Cannot set placeholder text‘s color, because deployment target is earlier than iOS 6.0");// TODO: Add fall-back code to set placeholder color.} |
#iOS6 Earlier
iOS6之前可以重写
|
1 |
-(void)drawPlaceholderInRect:(CGRect)rect; |
例如:
|
1
2
3
4
5 |
-(void) drawPlaceholderInRect:(CGRect)rect { [[UIColor blueColor] setFill]; [[self
placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]];} |
还有一些其他的方法可以重写控件
当然 你也可以继承UITextField 自定义自己的控件 实现的自定义方法
一般就是 以下几种:
|
1
2
3
4
5
6
7
8
9
10 |
- (CGRect)borderRectForBounds:(CGRect)bounds;- (CGRect)textRectForBounds:(CGRect)bounds;- (CGRect)placeholderRectForBounds:(CGRect)bounds;- (CGRect)editingRectForBounds:(CGRect)bounds;- (CGRect)clearButtonRectForBounds:(CGRect)bounds;- (CGRect)leftViewRectForBounds:(CGRect)bounds;- (CGRect)rightViewRectForBounds:(CGRect)bounds;- (void)drawTextInRect:(CGRect)rect;- (void)drawPlaceholderInRect:(CGRect)rect; |
UITextField placeholder text color,布布扣,bubuko.com
UITextField placeholder text color
标签:style blog class code c tar
原文地址:http://www.cnblogs.com/kingW/p/3730206.html