UItextField詳解

基本屬性

UITextField *text = [[UITextField alloc]initWithFrame:frame];

(1) //設(shè)置邊框樣式,只有設(shè)置了才會顯示邊框樣式 
  text.borderStyle = UITextBorderStyleRoundedRect;

(2) //設(shè)置輸入框的背景顏色,此時設(shè)置為白色 如果使用了自定義的背景圖片邊框會被忽略掉 
  text.backgroundColor = [UIColor whiteColor];
 
(3) //設(shè)置背景 
注意: 只有在 UITextBorderStyleNone 樣式下,設(shè)置背景圖才會生效,且圖片大小小于 text 的frame時,圖片會拉伸
  text.background = [UIImage imageNamed:@"dd.png"];
 
(4) //設(shè)置背景 
// 設(shè)置enable為NO 時的背景圖片
  text.disabledBackground = [UIImage imageNamed:@"cc.png"];
 
(5) //輸入框中是否有個叉號,在什么時候顯示,用于一次性刪除輸入框中的內(nèi)容
  text.clearButtonMode = UITextFieldViewModeAlways;
 
(6) //每輸入一個字符就變成點 用語密碼輸入
  text.secureTextEntry = YES;
 
(7) //是否糾錯
  text.autocorrectionType = UITextAutocorrectionTypeNo;
 
(8) //再次編輯就清空
  text.clearsOnBeginEditing = YES; 
 
(9) //內(nèi)容對齊方式
  text.textAlignment = UITextAlignmentLeft;
 
(10) //內(nèi)容的垂直對齊方式  
     內(nèi)容對齊方式 
     內(nèi)容的垂直對齊方式  UITextField繼承自UIControl,此類中有一個屬性contentVerticalAlignment 
 _textField.textAlignment = UITextAlignmentLeft;    text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
 
(11) //設(shè)置為YES時文本會自動縮小以適應(yīng)文本窗口大小.默認(rèn)是保持原來大小,而讓長文本滾動 
  textFied.adjustsFontSizeToFitWidth = YES;
 
(12)  //設(shè)置為YES時文本會自動縮小以適應(yīng)文本窗口大小.默認(rèn)是保持原來大小,而讓長文本滾動 
    _textField.adjustsFontSizeToFitWidth = YES; 
 
 //設(shè)置自動縮小顯示的最小字體大小,adjustsFontSizeToFitWidth為YES才會起作用 
  text.minimumFontSize = 20;
 
(13) //設(shè)置鍵盤的樣式
  text.keyboardType = UIKeyboardTypeNumberPad;
 
(14) //首字母是否大寫
  text.autocapitalizationType = UITextAutocapitalizationTypeNone;
 
(15) //return鍵變成什么鍵
 text.returnKeyType =UIReturnKeyDone;
 
(16) //鍵盤外觀
textView.keyboardAppearance=UIKeyboardAppearanceDefault;

(17) //最右側(cè)加圖片是以下代碼  左側(cè)類似
    text.rightView=image;
    text.rightViewMode = UITextFieldViewModeAlways; 

重寫繪制

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

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

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

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

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

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

代理方法

//開始編輯
- (void)textFieldDidBeginEditing:(UITextField *)textField

// 當(dāng)點擊鍵盤的返回鍵([text resignFirstResponder]隱藏鍵盤,[receiver resignFirstResponder])
- (BOOL)textFieldShouldReturn:(UITextField *)textField

//是否循序文本字段開始編輯
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

//是否允許文本字段結(jié)束編輯
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField

//是否允許根據(jù)用戶請求清除內(nèi)容
- (BOOL)textFieldShouldClear:(UITextField *)textField

//文字修改
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
最后編輯于
?著作權(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)容