iOS UITextField常用的設(shè)置

設(shè)置邊框樣式,只有設(shè)置了才會顯示邊框樣式

text.borderStyle = UITextBorderStyleRoundedRect;

typedef enum {
UITextBorderStyleNone,
UITextBorderStyleLine,
UITextBorderStyleBezel,
UITextBorderStyleRoundedRect
} UITextBorderStyle;

輸入框中是否有個叉號,在什么時候顯示,用于一次性刪除輸入框中的內(nèi)容

text.clearButtonMode = UITextFieldViewModeAlways;
  ```
>typedef enum {
 UITextFieldViewModeNever,  重不出現(xiàn)
 UITextFieldViewModeWhileEditing, 編輯時出現(xiàn)
 UITextFieldViewModeUnlessEditing,  除了編輯外都出現(xiàn)
 UITextFieldViewModeAlways   一直出現(xiàn)
} UITextFieldViewMode;

每輸入一個字符就變成點 用語密碼輸入

text.secureTextEntry = YES;


是否糾錯

text.autocorrectionType = UITextAutocorrectionTypeNo;

>typedef enum {
 UITextAutocorrectionTypeDefault, 默認(rèn)
 UITextAutocorrectionTypeNo,   不自動糾錯
 UITextAutocorrectionTypeYes,  自動糾錯
} UITextAutocorrectionType;

再次編輯就清空

text.clearsOnBeginEditing = YES;


設(shè)置為YES時文本會自動縮小以適應(yīng)文本窗口大小.默認(rèn)是保持原來大小,而讓長文本滾動  

textFied.adjustsFontSizeToFitWidth = YES;


設(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ù)點
 UIKeyboardTypeTwitter,        優(yōu)化的鍵盤,方便輸入@、#字符
 UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, 
} UIKeyboardType;

首字母是否大寫

text.autocapitalizationType = UITextAutocapitalizationTypeNone;

>typedef enum {
 UITextAutocapitalizationTypeNone, 不自動大寫
 UITextAutocapitalizationTypeWords,  單詞首字母大寫
 UITextAutocapitalizationTypeSentences,  句子的首字母大寫
 UITextAutocapitalizationTypeAllCharacters, 所有字母都大寫
} UITextAutocapitalizationType;

return鍵變成什么鍵

text.returnKeyType =UIReturnKeyDone;

>typedef enum {
 UIReturnKeyDefault, 默認(rèn) 灰色按鈕,標(biāo)有Return
 UIReturnKeyGo,      標(biāo)有Go的按鈕
 UIReturnKeyGoogle,標(biāo)有Google的按鈕
 UIReturnKeyJoin,標(biāo)有Join的按鈕
 UIReturnKeyNext,標(biāo)有Next的按鈕
 UIReturnKeyRoute,標(biāo)有Route的按鈕
 UIReturnKeySearch,標(biāo)有Search的按鈕
 UIReturnKeySend,標(biāo)有Send的按鈕
 UIReturnKeyYahoo,標(biāo)有Yahoo的按鈕
 UIReturnKeyYahoo,標(biāo)有Yahoo的按鈕
 UIReturnKeyEmergencyCall, 緊急呼叫按鈕
} UIReturnKeyType;

鍵盤外觀

textView.keyboardAppearance=UIKeyboardAppearanceDefault;

>typedef enum {
UIKeyboardAppearanceDefault, 默認(rèn)外觀,淺灰色
UIKeyboardAppearanceAlert,     深灰 石墨色

} UIReturnKeyType;
***
###限制只能輸入一定長度的字符

使用UITextFieldDelegate的方法實現(xiàn)

-(BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
{
//string指此時輸入的那個字符 textField指此時正在輸入的那個輸入框 返回YES就是可以改變輸入框的值

NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //得到輸入框的內(nèi)容

if (self.myTextField == textField)  //判斷是否是我們想要限定的那個輸入框
{ 
    if ([toBeString length] > 20) { //如果輸入框內(nèi)容大于20則彈出警告
       UIAlertView *alert =[ [UIAlertView alloc] initWithTitle:@"警告" message:@"超過輸入最大長度" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil];
        [alert show]; 
        return NO; 
    } 
} 
return YES; 

}

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

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

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