iOS——UITextField

一、UITextField 關係圖



二、UITextField 屬性

1. 保存 UITextField 對象的文本內容;默認爲 nil

@property(nullable,nonatomic,copy)  NSString * text; 


2. 保存 UITextField 對象的文本顏色;默認爲 nil,使用黑色

@property(nullable,nonatomic,strong)UIColor * textColor;


3. 保存 UITextField 對象的文本字體大小;默認爲 nil,使用 12 號

@property(nullable,nonatomic,strong)UIFont * font;


4. 保存 UITextField 對象的文本對齊方式;默認爲 NSLeftTextAlignment

@property(nonatomicNSTextAlignment textAlignment;

    // NSTextAlignment 枚舉
    typedef NS_ENUM(NSInteger, NSTextAlignment) {
        NSTextAlignmentLeft      = 0,   // 左對齊
        NSTextAlignmentCenter    = 1,   // 右對齊
        NSTextAlignmentRight     = 2,   // 居中
        NSTextAlignmentJustified = 3,   // 在一個段落的最後一行自然對齊
        NSTextAlignmentNatural   = 4,   // 默認的對齊
    }

5. 保存 UITextField 對象的邊框樣式;默認爲 UITextBordeStyleNone

@property(nonatomicUITextBorderStyle borderStyle;

    //UITextFiledStyle 枚舉
    typedef NS_ENUM(NSInteger, UITextBorderStyle) {
        UITextBorderStyleNone,       //無邊框
        UITextBorderStyleLine,       //黑色線邊框
        UITextBorderStyleBezel,      //有邊框和陰影
        UITextBorderStyleRoundedRect // 圓角矩形
    };



6. 保存 UITextField 對象的佔位符文本;默認爲空,且字符是亮灰色

@property(nullable,nonatomic,copyNSString * placeholder;


7. 再次編輯 UITextField 對象是否清空當前的內容;默認爲 NO

@property(nonatomicBOOL clearsOnBeginEditing;


8. 設置字體大小是否隨邊框寬度自適應;默認爲 NO

當字體大小過大以至於整個邊框不能夠全部顯示時,會隨着 minimumFontSize 的值減少,當已經填滿整個邊框則不會再減少

@property(nonatomicBOOL adjustsFontSizeToFitWidth;


9. 保存 UITextField 對象的最小字體;默認爲 0

@property(nonatomicCGFloat minimumFontSize; 


10. 設置 UITextField 對象的背景圖片;默認爲空,會拉伸圖片

@property(nullable,nonatomic,strong)UIImage * background;


11. 設置 UITextField 對象不可用時的背景圖片;默認爲空,如果沒有設置 background,則忽視

@property(nullable,nonatomic,strong)UIImage * disabledBackground;


12. 保存 UITextField 對象是否處於編輯

@property(nonatomic,readonly,getter=isEditing)BOOL editing;


13. 設置清除按鈕的樣式,就是文本框右面的叉叉;默認 UITextFieldViewModeNever

@property(nonatomicUITextFieldViewMode clearButtonMode;

    //UITextFieldViewMode 枚舉
    typedef NS_ENUM(NSInteger, UITextFieldViewMode) {
        UITextFieldViewModeNever,         // 從不顯示
        UITextFieldViewModeWhileEditing,  //處於編輯模式顯示
        UITextFieldViewModeUnlessEditing, //非編輯模式顯示
        UITextFieldViewModeAlways         // 總是顯示
    };

14. 設置 UITextField 對象的左視圖,

@property(nullable,nonatomic,strong)UIView * leftView;


15. 設置 UITextField 對象的左視圖的顯示模式;默認是 UITextFieldViewModeNever

@property(nonatomicUITextFieldViewMode leftViewMode; 


16. 設置 UITextField 對象的右視圖

@property(nullable,nonatomic,strong)UIView * rightView;


17. 設置 UITextField 對象的右視圖的顯示模式;默認是 UITextFieldViewModeNever

@property(nonatomicUITextFieldViewMode rightViewMode;

左視圖與右視圖中間就是文本區域


18. 當 UITextField 對象稱爲第一響應者時,彈出的視圖(類似於鍵盤)

@property (nullable,readwrite,strong)UIView *inputView;  


19. 當 UITextField 對象稱爲第一響應者時,彈出的輔助視圖(類似於鍵盤)       

@property (nullable,readwrite,strong)UIView *inputAccessoryView;


20. 設置是否允許再次編輯時在內容中間插入內容;默認爲 NO,即只能子啊頭尾插入新的內容

@property(nonatomic)BOOL clearsOnInsertion;


21. UITextField 的委託

@property(nullable,nonatomic,weakid<UITextFieldDelegate> delegate;


三、UITextField方法


1. 重繪方法

除了 UITextField 對象的系統風格外,還可以自定義 UITextField 對象的風格,此時就可以重寫如下的方法,這些方法都返回 CGPoint,指定了每個部件的邊界範圍
①重置邊緣區域

- (CGRect)borderRectForBounds:(CGRect)bounds;


②重置文字區域

- (CGRect)textRectForBounds:(CGRect)bounds;


③重置佔位符區域

- (CGRect)placeholderRectForBounds:(CGRect)bounds;


④重置編輯區域

- (CGRect)editingRectForBounds:(CGRect)bounds;


⑤重置 clearButton 的區域

- (CGRect)clearButtonRectForBounds:(CGRect)bounds;


⑥重置 leftView 的區域

- (CGRect)leftViewRectForBounds:(CGRect)bounds;


⑦重置 rightView 的區域

- (CGRect)rightViewRectForBounds:(CGRect)bounds;


⑧重寫 UITextField 對象的文本的方法

- (void)drawTextInRect:(CGRect)rect;


⑨重寫 UITextField 對象的佔位符文本的方法

- (void)drawTextInRect:(CGRect)rect;


2. UIView(UITextField)

①取消第一響應者

- (BOOL)endEditing:(BOOL)force;


3. UITextFieldDelegate 協議方法

①點擊 UITextField 對象的輸入框時觸發該方法,返回 YES 則進入編輯模式,否則不進入

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;


②獲得焦點時觸發該方法,成爲第一響應者

- (void)textFieldDidBeginEditing:(UITextField *)textField;


③結束編輯時觸發該方法,返回 YES 允許停止編輯或註銷第一響應者,否則 不允許編輯結束

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;


④失去第一響應者時觸發該方法

- (void)textFieldDidEndEditing:(UITextField *)textField;


詢問 文本是否可以被替換,如果返回 YES 則可被替換,否則不能被替換

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;


⑦點擊清除按鈕時觸發該方法,返回 YES 可以清除,否則不能清除

- (BOOL)textFieldShouldClear:(UITextField *)textField;


⑧點擊返回按鈕時觸發該方法,返回 YES 可以返回,否則不能返回

- (BOOL)textFieldShouldReturn:(UITextField *)textField;


四、UITextInputTraits 協議

1. 設置鍵盤的風格樣式,默認是UIKeyboardTypeDefault 

@property(nonatomic) UIKeyboardType keyboardType; 

typedef NS_ENUM(NSInteger, UIKeyboardType) {
    UIKeyboardTypeDefault,                 // 默認鍵盤樣式
    UIKeyboardTypeASCIICapable,            // 輸入字母的鍵盤
    UIKeyboardTypeNumbersAndPunctuation,   //輸入數字符號鍵盤
    UIKeyboardTypeURL,                     //輸入 URL 鍵盤
    UIKeyboardTypeNumberPad,               //輸入數字鍵盤(九鍵)
    UIKeyboardTypePhonePad,                //輸入電話號碼鍵盤
    UIKeyboardTypeNamePhonePad,            //輸入人名和電話號碼鍵盤
    UIKeyboardTypeEmailAddress,            //輸入郵件地址鍵盤
    UIKeyboardTypeDecimalPad NS_ENUM_AVAILABLE_IOS(4_1),   // 輸入數字和小數點的鍵盤
    UIKeyboardTypeTwitter NS_ENUM_AVAILABLE_IOS(5_0),      // 容易輸入 @ 和 # 的鍵盤
    UIKeyboardTypeWebSearch NS_ENUM_AVAILABLE_IOS(7_0),    // 搜索網頁的鍵盤
    UIKeyboardTypeASCIICapableNumberPad NS_ENUM_AVAILABLE_IOS(10_0), // A number pad (0-9) that will always be ASCII digits.
    UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,   // 廢棄

};

2. 設置鍵盤的返回鍵類型;默認是 UIReturnKeyTypeDefault

@property(nonatomic) UIReturnKeyType returnKeyType;

typedef NS_ENUM(NSInteger, UIReturnKeyType) {
    UIReturnKeyDefault,        // 顯示 return
    UIReturnKeyGo,             // 顯示 Go
    UIReturnKeyGoogle,         // 顯示 Search
    UIReturnKeyJoin,           // 顯示 Join
    UIReturnKeyNext,           // 顯示 Next
    UIReturnKeyRoute,          // 顯示 Route
    UIReturnKeySearch,         // 顯示 Search
    UIReturnKeySend,           // 顯示 Send
    UIReturnKeyYahoo,          // 顯示 Search
    UIReturnKeyDone,           // 顯示 Done
    UIReturnKeyEmergencyCall,  // 顯示 EmergencyCall
    UIReturnKeyContinue NS_ENUM_AVAILABLE_IOS(9_0),  // 顯示 Continue
};

3. 設置是否自動大寫

@property(nonatomic) UITextAutocapitalizationType autocapitalizationType;

typedef NS_ENUM(NSInteger, UITextAutocapitalizationType) {
    UITextAutocapitalizationTypeNone,          // 不能自動有效
    UITextAutocapitalizationTypeWords,         // 單詞開頭的情況下自動有效,在輸入單詞時第一個最爲大學,剩下的字母爲小寫
    UITextAutocapitalizationTypeSentences,     // 文章開頭的情況下自動有效,在輸入很長的文章的時,第一個單詞的第一個字母爲大寫,其餘均爲小寫
    UITextAutocapitalizationTypeAllCharacters, // 一值自動有效,一直是大寫
};


4. 設置自動矯正功能;默認是 UITextAutocorrectionTypeDefalut

@property(nonatomic) UITextAutocorrectionType autocorrectionType;

typedef NS_ENUM(NSInteger, UITextAutocorrectionType) {
    UITextAutocorrectionTypeDefault,   // 默認
    UITextAutocorrectionTypeNo,        // 禁止自動矯正
    UITextAutocorrectionTypeYes,       // 開啓自動矯正
};


5. 設置鍵盤外觀

@property(nonatomic) UIKeyboardAppearance keyboardAppearance;

typedef NS_ENUM(NSInteger, UIKeyboardAppearance) {
    UIKeyboardAppearanceDefault,// 默認外觀,淺灰色
    UIKeyboardAppearanceDark NS_ENUM_AVAILABLE_IOS(7_0),   // 深灰色
    UIKeyboardAppearanceLight NS_ENUM_AVAILABLE_IOS(7_0),  // 淺灰色
    UIKeyboardAppearanceAlert = UIKeyboardAppearanceDark,  // 廢棄
};

6. 設置是否安全輸入;默認是 NO

@property(nonatomic,getter=isSecureTextEntry) BOOL secureTextEntry;

發佈了31 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章