UITextField-修改占位文字和光標(biāo)的顏色,大小

一.設(shè)置占位文字的顏色

方法一:利用富文本

/** 手機(jī)號(hào)輸入框 */

@property(weak,nonatomic)IBOutletUITextField*phoneTextField;

- (void)viewDidLoad {? ?

[superviewDidLoad];// 創(chuàng)建一個(gè)富文本對(duì)象???? NSMutableDictionary*attributes = [NSMutableDictionarydictionary];// 設(shè)置富文本對(duì)象的顏色attributes[NSForegroundColorAttributeName] = [UIColorwhiteColor];// 設(shè)置UITextField的占位文字? self.phoneTextField.attributedPlaceholder= [[NSAttributedStringalloc] initWithString:@"手機(jī)號(hào)"attributes:attributes];}

方法二:利用Runtime獲取私有的屬性名稱(chēng),利用KVC設(shè)置屬性

// 設(shè)置占位文字的顏色為紅色(注意下面的'self'代表你要修改占位文字的UITextField控件)[selfsetValue:[UIColorredColor] forKeyPath:@"_placeholderLabel.textColor"];

注意:_placeholderLabel.textColor是不可亂寫(xiě)的哦,我們是怎么獲取到這個(gè)屬性的呢?請(qǐng)看下文:

// 只調(diào)用一次(自定義UITextField)+ (void)initialize {? ? [selfgetIvars];}// 獲取私有變量名稱(chēng)+ (void)getIvars {unsignedintcount =0;? ? Ivar *ivars = class_copyIvarList([UITextFieldclass], &count);for(inti =0; i < count; i++) {? ? ? ? Ivar ivar = ivars[i];NSLog(@"%s----%s", ivar_getName(ivar), ivar_getTypeEncoding(ivar));? ? }}

查看打印,找出可能的屬性名稱(chēng),試試便知;

完整代碼:自定義的UITextField,獲取到焦點(diǎn)(編輯狀態(tài))的時(shí)候是白色,失去焦點(diǎn)(非編輯狀態(tài))的時(shí)候是灰色:

#import"YCTextField.h"????? #import#define YCplaceholderTextColor @"_placeholderLabel.textColor"?????????

@implementationYCTextField+ (void)initialize {? ? [selfgetIvars];}// 獲取私有變量名稱(chēng)

+ (void)getIvars {unsignedintcount =0;? ?? Ivar *ivars = class_copyIvarList([UITextFieldclass], &count);for(inti =0; i < count; i++) {? ? ? ? Ivar ivar = ivars[i];NSLog(@"%s----%s", ivar_getName(ivar), ivar_getTypeEncoding(ivar));? ? }}- (void)awakeFromNib {// 設(shè)置光標(biāo)的顏色self.tintColor=self.textColor;}// 獲取到焦點(diǎn)- (BOOL)becomeFirstResponder {// 利用運(yùn)行時(shí)獲取key,設(shè)置占位文字的顏色[selfsetValue:self.textColorforKeyPath:YCplaceholderTextColor];return[superbecomeFirstResponder];}// 失去焦點(diǎn)- (BOOL)resignFirstResponder {// 利用運(yùn)行時(shí)獲取key,設(shè)置占位文字的顏色[selfsetValue:[UIColorgrayColor] forKeyPath:YCplaceholderTextColor];return[superresignFirstResponder];}@end

方法三.將占位文字畫(huà)上去(重寫(xiě)- (void)drawPlaceholderInRect:(CGRect)rect;)

- (void)drawPlaceholderInRect:(CGRect)rect{? ? [[UIColororangeColor] set];? ? [self.placeholderdrawInRect:rect withFont:[UIFontsystemFontOfSize:20]];}

二.設(shè)置光標(biāo)顏色

第一種: [[UITextField appearance]setTintColor:[UIColor blackColor]];

這種方法將影響所有TextField

第二種: 設(shè)置光標(biāo)的顏色self.tintColor= [UIColorredColor];

三.設(shè)置占位文字的偏移

重寫(xiě)-(CGRect)placeholderRectForBounds:(CGRect)bounds;方法

可以用來(lái)設(shè)置光標(biāo)與占位的間距

//控制placeHolder的位置,左右縮20-(CGRect)placeholderRectForBounds:(CGRect)bounds{//return CGRectInset(bounds, 20, 0);CGRectinset =CGRectMake(bounds.origin.x+50, bounds.origin.y, bounds.size.width-10, bounds.size.height);//更好理解些returninset;}

擴(kuò)充:系統(tǒng)還提供了很多類(lèi)似的方法

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

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

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

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

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

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

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

– leftViewRectForBounds:

– rightViewRectForBounds:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容