我們有時(shí)需要定制化UITextField對(duì)象的風(fēng)格饵溅,可以添加許多不同的重寫方法,來改變文本字段的顯示行為妇萄。這些方法都會(huì)返回一個(gè)CGRect結(jié)構(gòu)蜕企,制定了文本字段每個(gè)部件的邊界范圍,甚至修改placeHolder顏色冠句,字體,有些方法對(duì)UITextView同樣適用轻掩。
- (CGRect)textRectForBounds:(CGRect)bounds //重寫來重置文字區(qū)域
- (void)drawTextInRect:(CGRect)rect//改變繪文字屬性.重寫時(shí)調(diào)用super可以按默認(rèn)圖形屬性繪制,若自己完全重寫繪制函數(shù),就不用調(diào)用super了.
- (CGRect)placeholderRectForBounds:(CGRect)bounds //重寫來重置占位符區(qū)域
- (void)drawPlaceholderInRect:(CGRect)rect //重寫改變繪制占位符屬性.重寫時(shí)調(diào)用super可以按默認(rèn)圖形屬性繪制,若自己完全重寫繪制函數(shù)懦底,就不用調(diào)用super了
- (CGRect)borderRectForBounds:(CGRect)bounds //重寫來重置邊緣區(qū)域
- (CGRect)editingRectForBounds:(CGRect)bounds //重寫來重置編輯區(qū)域
- (CGRect)clearButtonRectForBounds:(CGRect)bounds //重寫來重置clearButton位置,改變size可能導(dǎo)致button的圖片失真
- (CGRect)leftViewRectForBounds:(CGRect)bounds
- (CGRect)rightViewRectForBounds:(CGRect)bounds
通過- (void)drawPlaceholderInRect:(CGRect)rect 方法可改變placeHolder顏色唇牧、字體,請(qǐng)看代碼:
自定義一個(gè)類CustomTextField讓它繼承UITextField實(shí)現(xiàn)以下方法即可:
//控制清除按鈕的位置
-(CGRect)clearButtonRectForBounds:(CGRect)bounds
{
return CGRectMake(bounds.origin.x + bounds.size.width - 50, bounds.origin.y + bounds.size.height -20, 16, 16);
}
//控制placeHolder的位置聚唐,左右縮20
-(CGRect)placeholderRectForBounds:(CGRect)bounds
{
//return CGRectInset(bounds, 20, 0);
CGRect inset = CGRectMake(bounds.origin.x+20, bounds.origin.y, bounds.size.width, bounds.size.height);//更好理解些
return inset;
}
//控制顯示文本的位置
-(CGRect)textRectForBounds:(CGRect)bounds
{
//return CGRectInset(bounds, 50, 0);
CGRect inset = CGRectMake(bounds.origin.x+190, bounds.origin.y, bounds.size.width -10, bounds.size.height);//更好理解些
return inset;
}
//控制編輯文本的位置
-(CGRect)editingRectForBounds:(CGRect)bounds
{
//return CGRectInset( bounds, 10 , 0 );
CGRect inset = CGRectMake(bounds.origin.x +10, bounds.origin.y, bounds.size.width -10, bounds.size.height);
return inset;
}
//控制左視圖位置
- (CGRect)leftViewRectForBounds:(CGRect)bounds
{
CGRect inset = CGRectMake(bounds.origin.x +10, bounds.origin.y, bounds.size.width-250, bounds.size.height);
return inset;
//return CGRectInset(bounds,50,0);
}
//控制placeHolder的顏色丐重、字體
- (void)drawPlaceholderInRect:(CGRect)rect
{
//CGContextRef context = UIGraphicsGetCurrentContext();
//CGContextSetFillColorWithColor(context, [UIColor yellowColor].CGColor);
[[UIColororangeColor] setFill];
[[selfplaceholder] drawInRect:rectwithFont:[UIFontsystemFontOfSize:20]];
}
下面是使用CustomTextField的代碼,可放在viewDidLoad等方法中
_textField = [[CustomTextField alloc] initWithFrame:CGRectMake(20, 150, 280, 30)];
_textField.placeholder = @"手機(jī)號(hào)";
_textField.borderStyle = UITextBorderStyleRoundedRect;
_textField.textAlignment = UITextAlignmentLeft;
_textField.delegate = self;
_textField.clearButtonMode = UITextFieldViewModeWhileEditing;
//_textField.text = @"隨便";
UIImageView *imgv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"anyImage.png"]];
_textField.leftView = imgv;
_textField.leftViewMode = UITextFieldViewModeAlways;
[self.view addSubview:_textField];
還可以將placeholder的字體設(shè)置為傾斜杆查,正常的情況下直接設(shè)置使用italicSystemFontOfSize:是只能顯示英文的斜體扮惦,中文是不可以的,使用如下方法可以使中文傾斜
- (void)drawPlaceholderInRect:(CGRect)rect
{
CGAffineTransform matrix = CGAffineTransformMake(1, 0, tanf(15 * (CGFloat)M_PI / 180), 1, 0, 0);
CGFloat size=self.font.pointSize;
UIFontDescriptor *desc = [ UIFontDescriptor fontDescriptorWithName :self.font.fontName matrix :matrix];
UIFont *font = [ UIFont fontWithDescriptor :desc size :size];
[[UIColor lightGrayColor] set]; //顏色會(huì)自動(dòng)變成黑色,所以需要手動(dòng)設(shè)置為灰色
[self.placeholder drawInRect:rect withFont:font lineBreakMode:NSLineBreakByTruncatingTail];
}