UITextField

1.初始化
UITextField *textField=[[UITextField alloc]initWithFrame:CGRectMake(100,100,100,100,100)];
2.設(shè)置占位字符
textField.placeholder=@"請(qǐng)輸入手機(jī)號(hào)";
3.設(shè)置鍵盤樣式 keyboardType
textField.keyboardType=UIKeyboardTypeNumberPad; 現(xiàn)在是純數(shù)字鍵盤

// UIKeyboardTypeDefault, 默認(rèn)的
// UIKeyboardTypeASCIICapable, 顯示一個(gè)鍵盤可以輸入ASCII字符,非ASCII鍵盤保持活躍
// UIKeyboardTypeNumbersAndPunctuation, 數(shù)字和各種標(biāo)點(diǎn)符號(hào)
// UIKeyboardTypeNumberPad, 數(shù)字鍵盤
// UIKeyboardTypePhonePad, 電話薄(1 - 9 * 0 #,字母的數(shù)字)
// UIKeyboardTypeDecimalPad 帶小數(shù)點(diǎn)的數(shù)字鍵盤
4.設(shè)置要輸入字體的顏色
textField.textColor=[UIColor redColor];
5.設(shè)置邊框樣式
textField.borderStyle=UITextBorderStyleNone;

UITextBorderStyleNone,    默認(rèn)   沒有邊框
UITextBorderStyleLine,      矩形
UITextBorderStyleBezel,    矩形
UITextBorderStyleRoundedRect    角有弧度的矩形

6.給邊框下設(shè)置一條虛線
邊框的高為大于1時(shí)上面添加的字符串才可以顯示出效果
UILabel lineLable=[[UILabel alloc]initWithFrame:CGRectMake(100, 149, 150, 2)];
當(dāng)邊框背景為白色時(shí),才能凸顯出上面字符串的顏色
[lineLable setBackgroundColor:[UIColor whiteColor]];
[lineLable setText:@"- - - - - - - - - - - - -"];
lineLable.textColor=[UIColor blackColor];
[self.window addSubview:lineLable];
7.給某文本框輸入字設(shè)置安全樣式 secure Text Entry安全的文本輸入
textField.secureTextEntry=YES;
8.設(shè)置textField能不能輸入的屬性
textField.enabled=YES;
textField.enabled=NO;
9.設(shè)置右側(cè)小叉號(hào)(枚舉值) clear Button Mode清除內(nèi)容的按鈕的模式
textField.clearButtonMode=UITextFieldViewModeAlways; 一直顯示
// UITextFieldViewModeNever, 不顯示出來,也就是此時(shí)沒有此鍵
// UITextFieldViewModeWhileEditing, 當(dāng)textField處于編輯狀態(tài)時(shí),此鍵顯示
// UITextFieldViewModeUnlessEditing, 當(dāng)編輯完成時(shí),此鍵顯示
10.首字母大寫(暫時(shí)不會(huì)用)
textField.autocapitalizationType ;
11.給編輯框設(shè)置 左視圖(以像素大小30
30的為例)

  直接給相框內(nèi)放置圖片     圖片的大小就是相框的大小
  UIImageView  *imageView=[ [UIImageView    alloc]initWithImage:[UIImage     imageNamed:@"左視圖.jpg"] ];
   將相框放到編輯框的左側(cè)
   textField.leftView=imageView;
   設(shè)置顯示模式                        后面的與條目9是一樣的,四種模式
   textField.leftViewMode=UITextFieldViewModeAlways;

12.給編輯框設(shè)置 右視圖(以像素大小30*30的為例)
方法同上,只是left改成right

13.代理方法 設(shè)置點(diǎn)擊按鈕,則按鈕上的圖片或者文字返回到輸入框中
給上面的textField設(shè)置tag值
textField.tag=2000;
使得textField稱為代理 因此要導(dǎo)入代理協(xié)議
textField.delegate=self; 在此工程的.h文件中導(dǎo)入U(xiǎn)ITextFieldDelegate協(xié)議

   在按鈕的方法中設(shè)置點(diǎn)擊事件
   //得到輸入框                                                                        
   UITextField  *myTextField=(UITextField *)[self.window       viewWithTag:2000];
  //得到標(biāo)題                              如果按鈕上是圖片,則是imageForState
  NSString   *title=[sender      titleForState:UIControlStateNormal];
  //將標(biāo)題顯示在輸入框      并且拼接字符串
   myTextField.text=[myTextField     stringByAppendingString: title];

14.回收鍵盤 取消第一響應(yīng)者
[myTextField resignFirstResponder];
15.通過點(diǎn)擊系統(tǒng)鍵盤的return 回收鍵盤
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}

16.自定義鍵盤
設(shè)置鍵盤的大小區(qū)域
UIView *keyBoardView=[[UIView alloc]initWithFrame:CGRectMake(0, 0,
CGRectGetWidth(self.window.frame),256)];
鍵盤的區(qū)域與所要關(guān)聯(lián)的輸入框
myTextField.inputView=keyBoardView;
17.協(xié)議代理方法:UITextFieldDelegate 所有的協(xié)議方法都是可選的(optional)
流程:
a.-(BOOL)textFieldShouldBeginEditing:
[UITextField *]textField; 是否開始編輯狀態(tài)
b.-(void)textFieldDidBeginEditing:(UITextField *)textField 已經(jīng)開始編輯狀態(tài)
c.-(BOOL)textFieldShouldEndEditing:(UITextField *)是否結(jié)束編輯狀態(tài)
d.-(void)textFieldDidEndEditing:(UITextField *)textField 已經(jīng)結(jié)束編輯狀態(tài)
e.-(BOOL)textFieldShouldReturn:(UITextField *) //點(diǎn)擊return按鈕所執(zhí)行的代理方法

18.改變占位字符的文字顏色
textField.placeholder = @"username is in here!";
[textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
[textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];

19.輸入文字時(shí)執(zhí)行的代理方法

  • (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容