iOS DeveloperiOS 开发

UITextField 重写方法 编辑文本的位置 显示文本的位置

2016-04-01  本文已影响499人  这样的我321

#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

上一篇下一篇

猜你喜欢

热点阅读