UITextField placeholder text color


iOS6 and Later 改变UITextField 中占位符 提示文本的文字颜色

在新版本中(iOS6以后)iOS提供一种 Key = value 属性的方式,来改变UI的属性内容。
以UITextField为例

@property(nonatomic,copy) NSAttributedString *attributedText NS_AVAILABLE_IOS(6_0); // default is nil

  

attributedText 为UITextField 的 public 属性 可以通过键值对的方式来改变 UITextField的内容

例如:

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之前可以重写

-(void)drawPlaceholderInRect:(CGRect)rect;

  



例如:

-(void) drawPlaceholderInRect:(CGRect)rect 
{ 
[[UIColor blueColor] setFill]; 
[[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]];
}

  


还有一些其他的方法可以重写控件


当然 你也可以继承UITextField 自定义自己的控件 实现的自定义方法
一般就是 以下几种:

 

- (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;

  

posted @ 2014-05-15 16:02  無出吾右  阅读(1148)  评论(0编辑  收藏  举报