iOS界面編程-UITextField

一、介紹

        UITextField繼承自UICotrol-》UIView-》UIResponder-》NSObject,一個UITextField對象是一個用來顯示可編輯文本和當用戶按下返回鍵時,發送動作消息給目標對象。典型使用於獲取用戶少量文字輸入,完成快速動作,例如基於用戶輸入的文字的搜索。

        另外對於那些基於文字的行爲,UITextField支持覆蓋視圖的顯示去顯示另外的信息,你可以使用自定義覆蓋視圖去顯示諸如書籤按鈕或搜索圖標。UITextField類也提供內置的按鈕用來清除當前文字。

      文本域對象,提供委託對象用來處理與編輯相關的通知。你可以使用這個委託去定製控件的編輯行爲以及提供某種動作發生後的嚮導。具體操作可以查看UITextFieldDelegate委託對象。

二、相關方法和屬性

 1、管理鍵盤

   當用戶點擊一個文本輸入框,文本輸入框變成第一響應對象自動請求系統顯示鍵盤。因爲系統鍵盤顯示後,肯定會遮蓋住一些用戶界面,造成當前輸入框顯示不完整。用戶需要自己進行處理,以便在輸入數據時,不遮擋輸入界面。另外一些系統對象,如table view 幫助滑動第一響應者到合適的地方。但是如果table view滑動到底端之後還是需要用戶自己去處理

    用戶可以設置當按下鍵盤中的某個按鈕,通常如return鍵,可以用來隱藏鍵盤。主要是通過調用resignFirstResponder方法完成操作,這樣當調用此方法,則完成當前的編輯會話操作。

   用戶可以定製鍵盤的外表,通過UITextInputTraits protocol 提供的屬性去操作。你可以使用這些屬性去指定鍵盤的類型。你可以使用這些屬性指定鍵盤的輸入類型。你也可以配置鍵盤的輸入行爲,例如輸入的單詞首字母大寫。

  2.鍵盤通知

 當系統顯示或者隱藏鍵盤時,會發出一些 鍵盤通知事件。這些通知包含了鍵盤的一些信息,諸如鍵盤的大小,可以用來移動視圖。註冊這些通知是得到這些鍵盤信息的唯一方式,鍵盤傳遞的相關消息如下:

 3狀態保存



4、訪問文字屬性

@property(nonatomic,copy) NSString *text 文本域顯示的文字

@property(nonatomic,copy) NSAttributedString *attributedText textfield顯示的文本樣式

@property(nonatomic,copy) NSString *placeholder 沒有輸入文字時,顯示的佔位符。

@property(nonatomic,copy) NSAttributedString *attributedPlaceholder  佔位字符的文本樣式。

@property(nonatomic,copy) NSDictionary <NSString *,id> *defaultTextAttributes

@property(nonatomic,strong) UIFont *font 文字字體

@property(nonatomic,strong) UIColor *textColor 文字顏色,默認爲黑色,不能設置爲nil

@property(nonatomic)NSTextAlignment textAlignment 文字對齊方式

@property(nonatomic,copy) NSDictionary <NSString *,id> *typingAttributes  用戶正在輸入文字的屬性


 5、設置文字的尺寸大小

@property(nonatomic)BOOL adjustsFontSizeToFitWidth 是否自動

@property(nonatomic)CGFloat minimumFontSize 允許的最小的字體


6、管理編輯行爲

@property(nonatomic,readonly, getter=isEditing)BOOL editing 布爾值,獲取是否處於編輯狀態。

@property(nonatomic)BOOL clearsOnBeginEditing 用來設置是否開始編輯時,清除以前的狀態。

@property(nonatomic)BOOL clearsOnInsertion 

@property(nonatomic)BOOL allowsEditingTextAttributes


7、設置背景外表

@property(nonatomic)UITextBorderStyle borderStyle  邊框樣式

@property(nonatomic,strong) UIImage *background 背景圖片

@property(nonatomic,strong) UIImage *disabledBackground textfied不可用時,顯示的背景

@property(nonatomic)UITextFieldViewMode clearButtonMode

@property(nonatomic,strong) UIView *leftView

@property(nonatomic)UITextFieldViewMode leftViewMode

@property(nonatomic,strong) UIView *rightView

@property(nonatomic)UITextFieldViewMode rightViewMode


8、訪問委託

@property(nonatomic,weak) id<UITextFieldDelegate > delegate

9 繪圖和定位覆蓋

一般這些方法不能直接調用,需要重寫這些方法

- (CGRect)textRectForBounds:(CGRect)bounds 返回文本的繪製區域

- (void)drawTextInRect:(CGRect)rect

- (CGRect)placeholderRectForBounds:(CGRect)bounds

- (void)drawPlaceholderInRect:(CGRect)rect

- (CGRect)borderRectForBounds:(CGRect)bounds

- (CGRect)editingRectForBounds:(CGRect)bounds

- (CGRect)clearButtonRectForBounds:(CGRect)bounds

10、代替系統的輸入視圖

@property(readwrite,strong) UIView *inputView

@property(readwrite,strong) UIView *inputAccessoryView

三、實際例子

    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 74, self.view.frame.size.width - 50, 31)];
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.placeholder = @"please input password";
//  textField.secureTextEntry = YES;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
//  textField.keyboardType = UIKeyboardTypeEmailAddress;
    textField.returnKeyType = UIReturnKeyDone;
    textField.font = [UIFont systemFontOfSize:14.0f];
    textField.textColor = [UIColor cyanColor];
    textField.delegate = self;
    textField.contentVerticalAlignment = UIViewContentModeCenter;
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(0, 0, 44, 44);
    [button setTitle:@"test" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    
    [textField setRightView:button];
    [textField setRightViewMode:UITextFieldViewModeAlways];
    [self.view addSubview:textField];



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