IOS UITextField 使用與方法解讀

字號(hào):


    UITextField是IOS開(kāi)發(fā)中用戶(hù)交互中重要的一個(gè)控件,常被用來(lái)做賬號(hào)密碼框,輸入信息框等。
    初始化一個(gè)文字框:
    UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 30, 100, 100)];
    設(shè)置和獲取文字框文字:
    @property(nonatomic,copy) NSString *text;
    通過(guò)AttributedString創(chuàng)建和獲取文字:
    @property(nonatomic,copy) NSAttributedString *attributedText;
    設(shè)置字體顏色屬性:
    @property(nonatomic,retain) UIColor *textColor;
    設(shè)置字體屬性:
    @property(nonatomic,retain) UIFont *font;
    設(shè)置字體對(duì)齊格式:
    @property(nonatomic)NSTextAlignment textAlignment;
    設(shè)置輸入框風(fēng)格:
    @property(nonatomic) UITextBorderStyle borderStyle;
    這個(gè)風(fēng)格是一個(gè)枚舉,如下:
    typedef NS_ENUM(NSInteger, UITextBorderStyle) {
    //沒(méi)有任何邊框
    UITextBorderStyleNone,
    //線(xiàn)性邊框
    UITextBorderStyleLine,
    //陰影效果邊框
    UITextBorderStyleBezel,
    //原型效果邊框
    UITextBorderStyleRoundedRect
    };
    設(shè)置默認(rèn)字體屬性
    @property(nonatomic,copy) NSDictionary *defaultTextAttributes;
    這個(gè)屬性的設(shè)置會(huì)影響到全部字體的屬性。
    設(shè)置缺省時(shí)顯示的灰度字符串
    @property(nonatomic,copy) NSString *placeholder;
    通過(guò)AttributedString設(shè)置缺省字符串
    @property(nonatomic,copy) NSAttributedString *attributedPlaceholder;
    設(shè)置是否在開(kāi)始編輯時(shí)清空輸入框內(nèi)容
    @property(nonatomic) BOOL clearsOnBeginEditing;
    設(shè)置字體大小是否隨寬度自適應(yīng)(默認(rèn)為NO)
    @property(nonatomic) BOOL adjustsFontSizeToFitWidth;
    設(shè)置最小字體大小
    @property(nonatomic) CGFloat minimumFontSize;
    設(shè)置背景圖片(會(huì)被拉伸)
    @property(nonatomic,retain) UIImage *background;
    設(shè)置禁用時(shí)的背景圖片
    @property(nonatomic,retain) UIImage *disabledBackground;
    是否正在編輯(只讀屬性)
    @property(nonatomic,readonly,getter=isEditing) BOOL editing;
    是否允許更改字符屬性字典
    @property(nonatomic) BOOL allowsEditingTextAttributes;
    設(shè)置屬性字典
    @property(nonatomic,copy) NSDictionary *typingAttributes;
    設(shè)置清除按鈕的顯示模式
    @property(nonatomic) UITextFieldViewMode clearButtonMode;
    這是一個(gè)枚舉,如下:
    typedef NS_ENUM(NSInteger, UITextFieldViewMode) {
    //從不顯示
    UITextFieldViewModeNever,
    //編輯的時(shí)候顯示
    UITextFieldViewModeWhileEditing,
    //非編輯的時(shí)候顯示
    UITextFieldViewModeUnlessEditing,
    //任何時(shí)候都顯示
    UITextFieldViewModeAlways
    };
    設(shè)置輸入框左邊的view
    @property(nonatomic,retain) UIView *leftView;
    設(shè)置輸入框左視圖的顯示模式
    @property(nonatomic) UITextFieldViewMode leftViewMode;
    設(shè)置輸入框右邊的view
    @property(nonatomic,retain) UIView *rightView;
    設(shè)置輸入框右視圖的顯示模式
    @property(nonatomic) UITextFieldViewMode rightViewMode;
    設(shè)置輸入框成為第一響應(yīng)時(shí)彈出的視圖和輔助視圖(類(lèi)似鍵盤(pán))
    @property (readwrite, retain) UIView *inputView;
    @property (readwrite, retain) UIView *inputAccessoryView;
    這個(gè)屬性設(shè)置是否允許再次編輯時(shí)在內(nèi)容中間插入內(nèi)容
    @property(nonatomic) BOOL clearsOnInsertion;
    注銷(xiāo)第一響應(yīng)(収鍵盤(pán))
    - (BOOL)endEditing:(BOOL)force;
    UITextFieldDelegate 代理中的方法
    點(diǎn)擊輸入框時(shí)觸發(fā)的方法,返回YES則可以進(jìn)入編輯狀態(tài),NO則不能。
    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
    開(kāi)始編輯時(shí)調(diào)用的方法
    - (void)textFieldDidBeginEditing:(UITextField *)textField;
    將要結(jié)束編輯時(shí)調(diào)用的方法,返回YES則可以結(jié)束編輯狀態(tài),NO則不能
    - (BOOL)textFieldShouldEndEditing:(UITextField *)textField;
    結(jié)束編輯調(diào)用的方法
    - (void)textFieldDidEndEditing:(UITextField *)textField;
    輸入字符時(shí)調(diào)用的方法
    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
    點(diǎn)擊清除按鈕時(shí)調(diào)用的函數(shù),返回YES則可以清除,點(diǎn)擊NO則不能清除
    - (BOOL)textFieldShouldClear:(UITextField *)textField;
    點(diǎn)擊return鍵觸發(fā)的函數(shù)
    - (BOOL)textFieldShouldReturn:(UITextField *)textField;