UITextField自定义placeHolder颜色并保持placeHolder居中

思路:

1、自定义UITextField的子类

2、重写drawPlaceholderInRect方法改变placeHolder颜色

3、重写placeholderRectForBounds方法保持placeHolder文字居中

代码如下:

@interface CustomPlaceHolderTextField : UITextField

@end
@implementation CustomPlaceHolderTextField

- (void)drawPlaceholderInRect:(CGRect)rect
{
    [[UIColor colorWithRed:168/255.0 green:168/255.0 blue:168/255.0 alpha:1.0] setFill];
    
    [[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:14]];
}

- (CGRect)placeholderRectForBounds:(CGRect)bounds
{
    CGSize size = [self.placeholder sizeWithFont:[UIFont systemFontOfSize:14]];
    CGRect placeHolderF = CGRectMake((bounds.size.width - size.width) / 2, (bounds.size.height - size.height) / 2, size.width, size.height);
    return placeHolderF;
}

 

posted @ 2014-11-04 09:38  瘦夹鲁尼  阅读(782)  评论(0编辑  收藏  举报