iOS UITextField 使用與方法解讀

UITextField是IOS開發中用戶交互中重要的一個控件,常被用來作帳號密碼框,輸入信息框等。less

初始化一個文字框:函數

UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 30, 100, 100)];

設置和獲取文字框文字:字體

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

經過AttributedString建立和獲取文字:spa

@property(nonatomic,copy)   NSAttributedString  *attributedText;代理

設置字體顏色屬性:code

@property(nonatomic,retain) UIColor  *textColor;圖片

設置字體屬性:開發

@property(nonatomic,retain) UIFont  *font;字符串

設置字體對齊格式:

@property(nonatomic)NSTextAlignment     textAlignment; 

設置輸入框風格:

@property(nonatomic) UITextBorderStyle    borderStyle; 

這個風格是一個枚舉,以下:

typedef NS_ENUM(NSInteger, UITextBorderStyle) {
    //沒有任何邊框
    UITextBorderStyleNone,
    //線性邊框
    UITextBorderStyleLine,
    //陰影效果邊框
    UITextBorderStyleBezel,
    //原型效果邊框
    UITextBorderStyleRoundedRect
};

設置默認字體屬性

@property(nonatomic,copy)  NSDictionary *defaultTextAttributes;

這個屬性的設置會影響到所有字體的屬性。

設置缺省時顯示的灰度字符串

@property(nonatomic,copy)  NSString  *placeholder; 

經過AttributedString設置缺省字符串

@property(nonatomic,copy)  NSAttributedString  *attributedPlaceholder;

設置是否在開始編輯時清空輸入框內容

@property(nonatomic) BOOL  clearsOnBeginEditing;

設置字體大小是否隨寬度自適應(默認爲NO)

@property(nonatomic)  BOOL   adjustsFontSizeToFitWidth; 

設置最小字體大小

@property(nonatomic) CGFloat    minimumFontSize;

設置背景圖片(會被拉伸)

@property(nonatomic,retain) UIImage *background;

設置禁用時的背景圖片

@property(nonatomic,retain) UIImage  *disabledBackground;

是否正在編輯(只讀屬性)

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

是否容許更改字符屬性字典

@property(nonatomic) BOOL allowsEditingTextAttributes;

設置屬性字典

@property(nonatomic,copy) NSDictionary *typingAttributes;

設置清除按鈕的顯示模式

@property(nonatomic) UITextFieldViewMode  clearButtonMode;

這是一個枚舉,以下:

typedef NS_ENUM(NSInteger, UITextFieldViewMode) {
    //從不顯示
    UITextFieldViewModeNever,
    //編輯的時候顯示
    UITextFieldViewModeWhileEditing,
    //非編輯的時候顯示
    UITextFieldViewModeUnlessEditing,
    //任什麼時候候都顯示
    UITextFieldViewModeAlways
};

設置輸入框左邊的view

@property(nonatomic,retain) UIView *leftView;

設置輸入框左視圖的顯示模式

@property(nonatomic)  UITextFieldViewMode  leftViewMode; 

 

設置輸入框右邊的view

@property(nonatomic,retain) UIView *rightView;

設置輸入框右視圖的顯示模式

@property(nonatomic)  UITextFieldViewMode  rightViewMode; 

設置輸入框成爲第一響應時彈出的視圖和輔助視圖(相似鍵盤)

@property (readwrite, retain) UIView *inputView;             
@property (readwrite, retain) UIView *inputAccessoryView;

這個屬性設置是否容許再次編輯時在內容中間插入內容

@property(nonatomic) BOOL clearsOnInsertion;

註銷第一響應(収鍵盤)

- (BOOL)endEditing:(BOOL)force;

UITextFieldDelegate 代理中的方法

點擊輸入框時觸發的方法,返回YES則能夠進入編輯狀態,NO則不能。

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

開始編輯時調用的方法   
- (void)textFieldDidBeginEditing:(UITextField *)textField;   

將要結束編輯時調用的方法,返回YES則能夠結束編輯狀態,NO則不能

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

結束編輯調用的方法  
- (void)textFieldDidEndEditing:(UITextField *)textField;  

輸入字符時調用的方法     
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;  

點擊清除按鈕時調用的函數,返回YES則能夠清除,點擊NO則不能清除

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

點擊return鍵觸發的函數          
- (BOOL)textFieldShouldReturn:(UITextField *)textField; 

 

專一技術,熱愛生活,交流技術,也作朋友。

——琿少 QQ羣:203317592

相關文章
相關標籤/搜索