UITextField 重写方法 编辑文本的位置 显示文本的位置 placeHolder的位置 字体 颜色

#import@interface SJCTextField : UITextField

-(id)initWithFrame:(CGRect)frame drawingLeft:(UIImageView*)icon;

@end

#import "SJCTextField.h"

@implementation SJCTextField

-(id)initWithFrame:(CGRect)frame drawingLeft:(UIImageView *)icon{

self = [super initWithFrame:frame];

if (self) {

self.leftView = icon;

self.leftViewMode = UITextFieldViewModeAlways;

}

return self;

}

-(CGRect)leftViewRectForBounds:(CGRect)bounds{

CGRect iconRect = [super leftViewRectForBounds:bounds];

iconRect.origin.x += 20;//

return iconRect;

}

//控制placeHolder的颜色、字体

- (void)drawPlaceholderInRect:(CGRect)rect

{

[[self placeholder] drawInRect:rect withAttributes:@{NSFontAttributeName:FONT_ROW_LABEL, NSForegroundColorAttributeName:COLOR_PLACEHOLDER}];

}//控制编辑文本的位置

-(CGRect)editingRectForBounds:(CGRect)bounds

{

CGRect inset = CGRectMake(bounds.origin.x+60, bounds.origin.y, bounds.size.width -10, bounds.size.height);

return inset;

}

//控制显示文本的位置

-(CGRect)textRectForBounds:(CGRect)bounds

{

CGRect inset = CGRectMake(bounds.origin.x+60, bounds.origin.y, bounds.size.width -10, bounds.size.height);

return inset;

}

//控制placeHolder的位置,

-(CGRect)placeholderRectForBounds:(CGRect)bounds

{

CGRect inset = CGRectMake(bounds.origin.x+60, bounds.origin.y+15, bounds.size.width-10, bounds.size.height);

return inset;

}

@end

推荐阅读更多精彩内容