日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

改变UITextField placeHolder颜色、字体

發(fā)布時(shí)間:2023/12/15 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 改变UITextField placeHolder颜色、字体 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

我們有時(shí)需要定制化UITextField對(duì)象的風(fēng)格,可以添加許多不同的重寫方法,來(lái)改變文本字段的顯示行為。這些方法都會(huì)返回一個(gè)CGRect結(jié)構(gòu),制定了文本字段每個(gè)部件的邊界范圍,甚至修改placeHolder顏色,字體。


– textRectForBounds:  ????//重寫來(lái)重置文字區(qū)域

– drawTextInRect:  ???????//改變繪文字屬性.重寫時(shí)調(diào)用super可以按默認(rèn)圖形屬性繪制,若自己完全重寫繪制函數(shù),就不用調(diào)用super.

– placeholderRectForBounds:  //重寫來(lái)重置占位符區(qū)域

– drawPlaceholderInRect:  //重寫改變繪制占位符屬性.重寫時(shí)調(diào)用super可以按默認(rèn)圖形屬性繪制,若自己完全重寫繪制函數(shù),就不用調(diào)用super

– borderRectForBounds:  //重寫來(lái)重置邊緣區(qū)域

– editingRectForBounds:  //重寫來(lái)重置編輯區(qū)域

– clearButtonRectForBounds:  //重寫來(lái)重置clearButton位置,改變size可能導(dǎo)致button的圖片失真

– leftViewRectForBounds:

– rightViewRectForBounds:


通過(guò)– drawPlaceholderInRect:方法可改變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+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?=?@"請(qǐng)輸入帳號(hào)信息";

????_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];

總結(jié)

以上是生活随笔為你收集整理的改变UITextField placeHolder颜色、字体的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。