UITextField的基本使用
1. 創(chuàng)建UITextField
????UITextField *textField = [[UITextField alloc] initWithFrame:(CGRectMake(0, 224, SCREEN_Width, 40))];? 初始化
2.?UITextField????屬性
????文本框文本內(nèi)容(顯示的內(nèi)容) ? ?
? ??textField.text = @"請(qǐng)輸入"; ? ?
? ??textField.delegate =?self;? ? 簽訂代理為self
? ??textField.placeholder = @"請(qǐng)輸入"; ? ?設(shè)置缺省時(shí)顯示的灰度字符串(text屬性沒(méi)有值的時(shí)候顯示的占位字符串)
? ??attributedText(NSAttributedString)? ? 可以通過(guò)富文本來(lái)進(jìn)行創(chuàng)建和獲取文本內(nèi)容
? ??attributedPlaceholder(NSAttributedString)????通過(guò)AttributedString設(shè)置缺省字符串
? ??textField.textColor?= [UIColor greenColor]; ? ?設(shè)置文本內(nèi)容字體顏色
? ??textField.textAlignment = NSTextAlignmentCenter; ? ?設(shè)置文本內(nèi)容居中(NSTextAlignmentLeft 居左,?NSTextAlignmentRight 居右)
? ??textField.font = [UIFont systemFontOfSize:12]; ? ?設(shè)置文本內(nèi)容字體大小
? ??textField.borderStyle = UITextBorderStyleNone;
? ??UITextBorderStyle 枚舉類(lèi)型
? ??????UITextBorderStyleNone ? ?沒(méi)有熱河邊框
? ??????UITextBorderStyleLine????線性邊框
? ??????UITextBorderStyleBezel????陰影效果邊框
? ??????UITextBorderStyleRoundedRect????原型效果邊框
? ??clearsOnBeginEditing(BOOL)? ? (設(shè)置是否在開(kāi)始編輯時(shí)清空輸入框內(nèi)容)
? ??adjustsFontSizeToFitWidth(BOOL) ? ?設(shè)置字體大小是否跟隨寬度自適應(yīng)(默認(rèn)為NO)
? ??minimumFontSize(CGFloat)? ? 設(shè)置最小字體大小
? ??background(UIImage)? ? 設(shè)置背景圖片 (會(huì)被拉伸)
? ??disabledBackground(UIImage) 設(shè)置禁用時(shí)的背景圖片
? ??editing (BOOL) ? ?是否正在編輯(只讀屬性)
? ??allowsEditingTextAttributes(BOOL)? ??是否允許更改字符屬性字典
? ??typingAttributes(NSDictionary) ? ?設(shè)置屬性字典
? ??clearButtonMode(UITextFieldViewMode)????設(shè)置清除按鈕的顯示模式
? ??????UITextFieldViewMode????枚舉類(lèi)型
? ??????????UITextFieldViewModeNever ? ?從不顯示
? ??????????UITextFieldViewModeWhileEditing ? ?編輯的時(shí)候顯示
? ??????????UITextFieldViewModeUnlessEditing ? ?非編輯的時(shí)候顯示
? ??????????UITextFieldViewModeAlways ? ?任何時(shí)候都顯示
? ??leftView(UIView) ? ?設(shè)置輸入框左邊的view
? ??rightView(UIView)????設(shè)置輸入框右邊的view
? ??rightViewMode(UITextFieldViewMode) ? ?設(shè)置輸入框右視圖的顯示模式
? ??inputView or inputAccessoryView?(UIView)????設(shè)置輸入框成為第一響應(yīng)時(shí)彈出的視圖和輔助視圖(類(lèi)似鍵盤(pán))
????clearsOnInsertion(BOOL)????這個(gè)屬性設(shè)置是否允許再次編輯時(shí)在內(nèi)容中間插入內(nèi)容 ? ?
? ??UIKeyboardType?keyboardType枚舉類(lèi)型鍵盤(pán)類(lèi)型
? ??????UIKeyboardTypeDefault???? 默認(rèn)鍵盤(pán):支持所有字符
????????UIKeyboardTypeASCIICapable????支持ASCII的默認(rèn)鍵盤(pán)
????????UIKeyboardTypeNumbersAndPunctuation????標(biāo)準(zhǔn)電話鍵盤(pán),支持+*#等符號(hào)
????????UIKeyboardTypeURL ? ?URL鍵盤(pán),有.com按鈕;只支持URL字符
????????UIKeyboardTypeNumberPad????數(shù)字鍵盤(pán)
????????UIKeyboardTypePhonePad????電話鍵盤(pán)
????????UIKeyboardTypeNamePhonePad????電話鍵盤(pán),也支持輸入人名字
? ??????UIKeyboardTypeEmailAddress????用于輸入電子郵件地址的鍵盤(pán)?
? ??[self.viewaddSubview:textField]; ? ?添加顯示輸入框
3.?UITextField代理(UITextFieldDelegate)方法已經(jīng)鍵盤(pán)響應(yīng)與注銷(xiāo)方法
? ??[textField resignFirstResponder];? ? 設(shè)置textfield為第一響應(yīng)這(回收鍵盤(pán))
? ??[textField becomeFirstResponder]; ? ?是否需要進(jìn)入頁(yè)面就輸入框就處于開(kāi)始編輯狀態(tài)
? ??- (BOOL)endEditing:(BOOL)force;????注銷(xiāo)第一響應(yīng)(収鍵盤(pán))
? ??- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;? ??點(diǎn)擊輸入框時(shí)觸發(fā)的方法,返回YES則可以進(jìn)入編輯狀態(tài),NO則不能。
? ??- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;? ??將要結(jié)束編輯時(shí)調(diào)用的方法,返回YES則可以結(jié)束編輯狀態(tài),NO則不能
? ??- (void)textFieldDidBeginEditing:(UITextField *)textField;????開(kāi)始編輯時(shí)調(diào)用的方法
? ??- (void)textFieldDidEndEditing:(UITextField *)textField;????結(jié)束編輯調(diào)用的方法
? ??- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; ? ?當(dāng)輸入框內(nèi)字符發(fā)生變化時(shí)時(shí)調(diào)用的方法
? ??- (BOOL)textFieldShouldClear:(UITextField *)textField;? ??點(diǎn)擊清除按鈕時(shí)調(diào)用的函數(shù),返回YES則可以清除,返回NO則不能清除
? ??- (BOOL)textFieldShouldReturn:(UITextField *)textField;? ? 點(diǎn)擊鍵盤(pán)上的return觸發(fā)的方法
4. 拓展UITextField限制字?jǐn)?shù)
????- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:????(NSString*)string{
????????NSIntegermaxLength = 6;//設(shè)置限制字?jǐn)?shù)
????????if(textField.tag ==100) {
????????????NSString* toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
????????????if (toBeString.length > maxLength && range.length! = 1){
????????????????textField.text = [toBeString substringToIndex:maxLength];
????????????????returnNO;
????????????}}returnYES;}
5. 回收鍵盤(pán)點(diǎn)擊空白self.view
? ??-(void)touchesBegan:(NSSet *)toucheswithEvent:(UIEvent*)event{
? ? ????[self.textField resignFirstResponder];
????}