1.初始化textfield并設(shè)置位置及大小
UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)];
2.設(shè)置邊框樣式铃在,只有設(shè)置了才會(huì)顯示邊框樣式
text.borderStyle = UITextBorderStyleRoundedRect;
typedef enum
{
UITextBorderStyleNone,
UITextBorderStyleLine,
UITextBorderStyleBezel,
UITextBorderStyleRoundedRect
} UITextBorderStyle;
3.設(shè)置輸入框的背景顏色策彤,此時(shí)設(shè)置為白色 如果使用了自定義的背景圖片邊框會(huì)被忽略掉
text.backgroundColor = [UIColor whiteColor];
4.設(shè)置背景
text.background = [UIImage imageNamed:@"dd.png"];
5.設(shè)置背景
text.disabledBackground = [UIImage imageNamed:@"cc.png"];
6.當(dāng)輸入框沒有內(nèi)容時(shí)奥吩,水印提示 提示內(nèi)容為password
text.placeholder = @"password";
7.設(shè)置輸入框內(nèi)容的字體樣式和大小
text.font = [UIFont fontWithName:@"Arial" size:20.0f];
8.設(shè)置字體顏色
text.textColor = [UIColor redColor];
9.輸入框中是否有個(gè)叉號(hào)册养,在什么時(shí)候顯示,用于一次性刪除輸入框中的內(nèi)容
text.clearButtonMode = UITextFieldViewModeAlways;
typedef enum {
UITextFieldViewModeNever, 重不出現(xiàn)
UITextFieldViewModeWhileEditing, 編輯時(shí)出現(xiàn)
UITextFieldViewModeUnlessEditing, 除了編輯外都出現(xiàn)
UITextFieldViewModeAlways 一直出現(xiàn)
} UITextFieldViewMode;
10.輸入框中一開始就有的文字
text.text = @"一開始就在輸入框的文字";
11.每輸入一個(gè)字符就變成點(diǎn) 用語(yǔ)密碼輸入(一般是密碼的隱藏)
text.secureTextEntry = YES;
12.是否糾錯(cuò)
text.autocorrectionType = UITextAutocorrectionTypeNo;
typedef enum {
UITextAutocorrectionTypeDefault, 默認(rèn)
UITextAutocorrectionTypeNo, 不自動(dòng)糾錯(cuò)
UITextAutocorrectionTypeYes, 自動(dòng)糾錯(cuò)
} UITextAutocorrectionType;
13.再次編輯就清空
text.clearsOnBeginEditing = YES;
14.內(nèi)容對(duì)齊方式
text.textAlignment = UITextAlignmentLeft;
15.內(nèi)容的垂直對(duì)齊方式 UITextField繼承自UIControl,此類中有一個(gè)屬性contentVerticalAlignment
text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
16.設(shè)置為YES時(shí)文本會(huì)自動(dòng)縮小以適應(yīng)文本窗口大小.默認(rèn)是保持原來大小,而讓長(zhǎng)文本滾動(dòng)
textFied.adjustsFontSizeToFitWidth = YES;
17.設(shè)置自動(dòng)縮小顯示的最小字體大小
text.minimumFontSize = 20;
18.設(shè)置鍵盤的樣式
text.keyboardType = UIKeyboardTypeNumberPad;
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;
19.首字母是否大寫
text.autocapitalizationType = UITextAutocapitalizationTypeNone;
typedef enum {
UITextAutocapitalizationTypeNone, 不自動(dòng)大寫
UITextAutocapitalizationTypeWords, 單詞首字母大寫
UITextAutocapitalizationTypeSentences, 句子的首字母大寫
UITextAutocapitalizationTypeAllCharacters, 所有字母都大寫
} UITextAutocapitalizationType;
20.return鍵變成什么鍵
text.returnKeyType =UIReturnKeyDone;
typedef enum {
UIReturnKeyDefault, 默認(rèn) 灰色按鈕禁舷,標(biāo)有Return
UIReturnKeyGo, 標(biāo)有Go的藍(lán)色按鈕
UIReturnKeyGoogle,標(biāo)有Google的藍(lán)色按鈕,用語(yǔ)搜索
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;
21.鍵盤外觀
textView.keyboardAppearance=UIKeyboardAppearanceDefault毅往;
typedef enum {
UIKeyboardAppearanceDefault牵咙, 默認(rèn)外觀,淺灰色
UIKeyboardAppearanceAlert攀唯, 深灰 石墨色
} UIReturnKeyType;
22.設(shè)置代理 用于實(shí)現(xiàn)協(xié)議
text.delegate = self;
23.把textfield加到視圖中
[self.window addSubview:text];
24.最右側(cè)加圖片是以下代碼 左側(cè)類似
UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];
text.rightView=image;
text.rightViewMode = UITextFieldViewModeAlways;
typedef enum {
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
} UITextFieldViewMode;
25.按return鍵鍵盤往下收 becomeFirstResponder
類要采用UITextFieldDelegate協(xié)議
text.delegate = self; 聲明text的代理是我洁桌,我會(huì)去實(shí)現(xiàn)把鍵盤往下收的方法 這個(gè)方法在UITextFieldDelegate里所以我們要采用UITextFieldDelegate這個(gè)協(xié)議
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[text resignFirstResponder]; //主要是[receiver resignFirstResponder]在哪調(diào)用就能把receiver對(duì)應(yīng)的鍵盤往下收
return YES;
}
26.重寫繪制行為
除了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:
27.委托方法
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
//返回一個(gè)BOOL值,指定是否循序文本字段開始編輯
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
//開始編輯時(shí)觸發(fā)敢订,文本字段將成為first responder
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
//返回BOOL值王污,指定是否允許文本字段結(jié)束編輯,當(dāng)編輯結(jié)束枢析,文本字段會(huì)讓出first responder
//要想在用戶結(jié)束編輯時(shí)阻止文本字段消失玉掸,可以返回NO
//這對(duì)一些文本字段必須始終保持活躍狀態(tài)的程序很有用,比如即時(shí)消息
return NO;
}
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//當(dāng)用戶使用自動(dòng)更正功能醒叁,把輸入的文字修改為推薦的文字時(shí)司浪,就會(huì)調(diào)用這個(gè)方法泊业。
//這對(duì)于想要加入撤銷選項(xiàng)的應(yīng)用程序特別有用
//可以跟蹤字段內(nèi)所做的最后一次修改啊易,也可以對(duì)所有編輯做日志記錄,用作審計(jì)用途吁伺。
//要防止文字被改變可以返回NO
//這個(gè)方法的參數(shù)中有一個(gè)NSRange對(duì)象租谈,指明了被改變文字的位置篮奄,建議修改的文本也在其中
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
//返回一個(gè)BOOL值指明是否允許根據(jù)用戶請(qǐng)求清除內(nèi)容
//可以設(shè)置在特定條件下才允許清除內(nèi)容
return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
//返回一個(gè)BOOL值,指明是否允許在按下回車鍵時(shí)結(jié)束編輯
//如果允許要調(diào)用resignFirstResponder 方法割去,這回導(dǎo)致結(jié)束編輯窟却,而鍵盤會(huì)被收起[textField resignFirstResponder];
//查一下resign這個(gè)單詞的意思就明白這個(gè)方法了
return YES;
}