我們有時需要定制化UITextField對象的風格,可以添加許多不同的重寫方法,來改變文本字段的顯示行為。這些方法都會傳回一個CGRect結構,制定了文本字段每個部件的邊界範圍,甚至修改placeHolder顔色,字型。
– textRectForBounds: //重寫來重置文字區域
– drawTextInRect: //改變繪文字屬性.重寫時調用super可以按預設圖形屬性繪制,若自己完全重寫繪制函數,就不用調用super了.
– placeholderRectForBounds: //重寫來重置占位符區域
– drawPlaceholderInRect: //重寫改變繪制占位符屬性.重寫時調用super可以按預設圖形屬性繪制,若自己完全重寫繪制函數,就不用調用super了
– borderRectForBounds: //重寫來重置邊緣區域
– editingRectForBounds: //重寫來重置編輯區域
– clearButtonRectForBounds: //重寫來重置clearButton位置,改變size可能導緻button的圖檔失真
– leftViewRectForBounds:
– rightViewRectForBounds:
通過– drawPlaceholderInRect:方法可改變placeHolder顔色、字型,請看代碼:
首先定義一個類CustomTextField讓它繼承UITextField實作以下方法即可:
//控制清除按鈕的位置
-(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+100, bounds.origin.y, bounds.size.width -10, 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 = @"請輸入帳号資訊";
_textField.borderStyle = UITextBorderStyleRoundedRect;
_textField.textAlignment = UITextAlignmentLeft;
_textField.delegate = self;
_textField.clearButtonMode = UITextFieldViewModeWhileEditing;
_textField.text = @"aa";
UIImageView *imgv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon-iwant-2.png"]];
_textField.leftView = imgv;
_textField.leftViewMode = UITextFieldViewModeAlways;
[self.view addSubview:_textField];
轉載:http://blog.sina.com.cn/s/blog_671d2e4f0101d90v.html
前一段時間一直尋找如何修改uitextfield中預設字型顔色的方法,網上提供的方法基本都是要派生一個uitextfield的子類,這種方法我嘗試了,可以達到效果,但是這種方法存在一些問題,同時這種方法的實作代碼有點多,顯然不是最佳方案,最近發現一種比較友善的方法,通過查詢sdk我們發現隻要重寫drawPlaceholderInRect方法就能夠實作,方法中的具體代碼分兩部分:1.設定字型顔色,2.将改顔色應用于預設字型;
[[UIColor colorWithRed:0 green:0 blue:0 alpha:1] setFill];
[[self placeholder] drawInRect: rect withFont:[self font]];
注:代碼中的self指的是uitextfiled對象,rect則是drawPlaceholderInRect的一個參數
通過這種方式就可以任意改變預設字型的顔色了
轉載:http://www.cocoachina.com/bbs/read.php?tid=112023&fpage=158
You can override drawPlaceholderInRect:(CGRect)rect as such to manually render the placeholder text:
- (void) drawPlaceholderInRect:(CGRect)rect {
[[UIColor blueColor] setFill];
[[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]];
}
轉載:http://stackoverflow.com/questions/1340224/iphone-uitextfield-change-placeholder-text-color