/*
設(shè)置背景色、背景圖片昔案。設(shè)置了背景圖片后尿贫,背景色設(shè)置無效
如果background為nil,disabledBackground無效
*/
_textField.background = [UIImage imageNamed:@"圖片名字"];
_textField.disabledBackground = [UIImage imageNamed:@"圖片名字"];
/*
設(shè)置為YES時(shí)文本會(huì)自動(dòng)縮小以適應(yīng)文本窗口大小.默認(rèn)是保持原來大小,而讓長(zhǎng)文本滾動(dòng)
*/
_textField.adjustsFontSizeToFitWidth = YES;
//設(shè)置自動(dòng)縮小顯示的最小字體大小踏揣,adjustsFontSizeToFitWidth為YES才會(huì)起作用
_textField.minimumFontSize = 10;
_textField.font = [UIFont systemFontOfSize:20.0f];
/*
內(nèi)容對(duì)齊方式
內(nèi)容的垂直對(duì)齊方式 UITextField繼承自UIControl,此類中有一個(gè)屬性contentVerticalAlignment
*/
_textField.textAlignment = UITextAlignmentLeft;
_textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
/*
輸入框樣式
*/
typedef enum {
UITextBorderStyleNone,
UITextBorderStyleLine,
UITextBorderStyleBezel,
UITextBorderStyleRoundedRect
} UITextBorderStyle;
_textField.borderStyle = UITextBorderStyleBezel;
/*
每輸入一個(gè)字符就變成點(diǎn) 用語密碼輸入
*/
_textField.secureTextEntry = YES;
/*
輸入框中是否有個(gè)叉號(hào)庆亡,在什么時(shí)候顯示,用于一次性刪除輸入框中的內(nèi)容
*/
typedef enum {
UITextFieldViewModeNever, 重不出現(xiàn)
UITextFieldViewModeWhileEditing, 編輯時(shí)出現(xiàn)
UITextFieldViewModeUnlessEditing, 除了編輯外都出現(xiàn)
UITextFieldViewModeAlways 一直出現(xiàn)
} UITextFieldViewMode;
_textField.clearButtonMode = UITextFieldViewModeWhileEditing;
/*
開始編輯捞稿,清空數(shù)據(jù)
*/
_textField.clearsOnBeginEditing = NO;
/*
開始編輯又谋,清空數(shù)據(jù)
*/
typedef enum {
UITextAutocorrectionTypeDefault, 默認(rèn)
UITextAutocorrectionTypeNo, 不自動(dòng)糾錯(cuò)
UITextAutocorrectionTypeYes, 自動(dòng)糾錯(cuò)
} UITextAutocorrectionType;
_textField.autocorrectionType = UITextAutocorrectionTypeYes;
/*
//首字母是否大寫
*/
typedef enum {
UITextAutocapitalizationTypeNone, 不自動(dòng)大寫
UITextAutocapitalizationTypeWords, 單詞首字母大寫
UITextAutocapitalizationTypeSentences, 句子的首字母大寫
UITextAutocapitalizationTypeAllCharacters, 所有字母都大寫
} UITextAutocapitalizationType;
_textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
/*
鍵盤
*/
typedef enum {
UIKeyboardTypeDefault, 默認(rèn)鍵盤,支持所有字符
UIKeyboardTypeASCIICapable, 支持ASCII的默認(rèn)鍵盤
UIKeyboardTypeNumbersAndPunctuation, 標(biāo)準(zhǔn)電話鍵盤娱局,支持+*#字符
UIKeyboardTypeURL, URL鍵盤彰亥,支持.com按鈕 只支持URL字符
UIKeyboardTypeNumberPad, 數(shù)字鍵盤
UIKeyboardTypePhonePad, 電話鍵盤
UIKeyboardTypeNamePhonePad, 電話鍵盤,也支持輸入人名
UIKeyboardTypeEmailAddress, 用于輸入電子 郵件地址的鍵盤
UIKeyboardTypeDecimalPad, 數(shù)字鍵盤 有數(shù)字和小數(shù)點(diǎn)
UIKeyboardTypeTwitter, 優(yōu)化的鍵盤衰齐,方便輸入@任斋、#字符
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
} UIKeyboardType;
_textField.keyboardType = UIKeyboardTypeDefault;
/*
return鍵變成什么鍵
*/
typedef enum {
UIReturnKeyDefault, 默認(rèn) 灰色按鈕,標(biāo)有Return
UIReturnKeyGo, 標(biāo)有Go的藍(lán)色按鈕
UIReturnKeyGoogle,標(biāo)有Google的藍(lán)色按鈕耻涛,用語搜索
UIReturnKeyJoin,標(biāo)有Join的藍(lán)色按鈕
UIReturnKeyNext,標(biāo)有Next的藍(lán)色按鈕
UIReturnKeyRoute,標(biāo)有Route的藍(lán)色按鈕
UIReturnKeySearch,標(biāo)有Search的藍(lán)色按鈕
UIReturnKeySend,標(biāo)有Send的藍(lán)色按鈕
UIReturnKeyYahoo,標(biāo)有Yahoo的藍(lán)色按鈕
UIReturnKeyYahoo,標(biāo)有Yahoo的藍(lán)色按鈕
UIReturnKeyEmergencyCall, 緊急呼叫按鈕
} UIReturnKeyType;
_textField.returnKeyType = UIReturnKeyDone;
/*
鍵盤外觀
*/
typedef enum {
UIKeyboardAppearanceDefault废酷, 默認(rèn)外觀,淺灰色
UIKeyboardAppearanceDark抹缕, 深灰 石墨色
UIKeyboardAppearanceLight 淺灰色
} UIReturnKeyType;
_textField.keyboardAppearance=UIKeyboardAppearanceDefault;
/*
設(shè)置左右的兩個(gè)view
*/
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image_right"]];
imgView.frame = CGRectMake(0, 0, 20, 20);
_textField.rightView = imgView;
_textField.rightViewMode = UITextFieldViewModeAlways; // 同上
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image_left"]];
imgView.frame = CGRectMake(0, 0, 20, 20);
_textField.leftView = imgView;
_textField.leftViewMode = UITextFieldViewModeAlways; // 同上
重寫繪制行為
除了UITextField對(duì)象的風(fēng)格選項(xiàng)澈蟆,你還可以定制化UITextField對(duì)象,為他添加許多不同的重寫方法卓研,來改變文本字段的顯示行為趴俘。這些方法都會(huì)返回一個(gè)CGRect結(jié)構(gòu),制定了文本字段每個(gè)部件的邊界范圍鉴分。以下方法都可以重寫哮幢。
– textRectForBounds: //重寫來重置文字區(qū)域
– drawTextInRect: //改變繪文字屬性.重寫時(shí)調(diào)用super可以按默認(rèn)圖形屬性繪制,若自己完全重寫繪制函數(shù),就不用調(diào)用super了.
– placeholderRectForBounds: //重寫來重置占位符區(qū)域
– drawPlaceholderInRect: //重寫改變繪制占位符屬性.重寫時(shí)調(diào)用super可以按默認(rèn)圖形屬性繪制,若自己完全重寫繪制函數(shù)志珍,就不用調(diào)用super了.
– borderRectForBounds: //重寫來重置邊緣區(qū)域
– editingRectForBounds: //重寫來重置編輯區(qū)域
– clearButtonRectForBounds: //重寫來重置clearButton位置,改變size可能導(dǎo)致button的圖片失真
– leftViewRectForBounds:
– rightViewRectForBounds:
/**
* 設(shè)置Placeholder
*/
- (void)customWithPlaceholder: (NSString *)placeholder color: (UIColor *)color font: (UIFont *)font {
self.placeholder = placeholder;
[self setValue:顏色 forKeyPath:@"_placeholderLabel.color"];
}
/**
* 控制編輯文本的位置
*/
-(CGRect)editingRectForBounds:(CGRect)bounds {
CGRect inset = CGRectMake(bounds.origin.x, bounds.origin.y+13.5f, bounds.size.width, bounds.size.height);
return inset;
}
/**
* 控制顯示文本的位置
*/
-(CGRect)textRectForBounds:(CGRect)bounds {
CGRect inset = CGRectMake(bounds.origin.x, bounds.origin.y+13.5f, bounds.size.width, bounds.size.height);
return inset;
}
/**
清除按鈕位置
*/
-(CGRect)clearButtonRectForBounds:(CGRect)bounds {
CGRect inset = CGRectMake(bounds.origin.x, bounds.origin.y+13.5f, bounds.size.width, bounds.size.height);
CGRect clearButtonRect = [super clearButtonRectForBounds:inset];
return clearButtonRect;
}
- (CGRect)placeholderRectForBounds:(CGRect)bounds{
CGRect inset = CGRectMake(bounds.origin.x, bounds.origin.y+13.5f, bounds.size.width, bounds.size.height);
return inset;
}