iOS开发(4)UITextField
? UITextField是UI控件中的文本輸入框控件,擁有自己的代理可以監(jiān)聽用戶輸入的數(shù)據(jù)。
//創(chuàng)建一個(gè)UITextField
? ? UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(120.0f, 80.0f, 150.0f, 30.0f)];
? ? //外框類型
? ? [textField setBorderStyle:UITextBorderStyleRoundedRect];
? ? //默認(rèn)顯示的字
? ? textField.placeholder = @"input";
? ? //輸入的內(nèi)容是否保密 主要用于密碼輸入
? ? textField.secureTextEntry = NO;
? ? //自動(dòng)校正樣式
? ? textField.autocorrectionType = UITextAutocorrectionTypeNo;
? ? textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
? ? //return按鈕的樣式
? ? textField.returnKeyType = UIReturnKeyDone;
? ? //編輯時(shí)會(huì)出現(xiàn)個(gè)修改X? 點(diǎn)擊后會(huì)清空內(nèi)容
? ? textField.clearButtonMode = UITextFieldViewModeWhileEditing;
? ? //下次輸入時(shí)是否清空上次的內(nèi)容
? ? textField.clearsOnBeginEditing = NO;
?? ?
? ? textField.delegate = self;//設(shè)置代理
下面介紹一下UITextField的代理
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
? ? NSLog(@"開始編輯時(shí)會(huì)走這個(gè)方法");
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
? ? NSLog(@"結(jié)束編輯時(shí)會(huì)走這個(gè)方法");
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
? ? NSLog(@"是否可以開始編輯");
? ? return YES;
}
//隱藏鍵盤
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
? ? //return之前讓textfield通過下面方法失去第一響應(yīng)者,也就是隱藏鍵盤
? ? [textField resignFirstResponder];
? ? return YES;
}
//限制輸入文本的長度
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
? ? //這里可以對(duì)輸入框輸入的內(nèi)容增加限制
? ? NSLog(@"%d ,%d",range.location,range.length);
? ? NSLog(@"%@",string);
? ? if (range.location >= 20)
? ? ? ? return NO;// return NO to not change text
? ? if ([string isEqualToString:@"d"])
? ? ? ? return NO;
? ? return NO;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
? ? return NO;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
//? ? textField.text = @"";
? ? return YES;
}
關(guān)于TextField一個(gè)很重要的部分就是如何去監(jiān)聽鍵盤在屏幕的高度,因?yàn)楹芏鄷r(shí)候我們整個(gè)輸入框彈起來的時(shí)候會(huì)遮住其他一些圖像。下一節(jié)將介紹一下鍵盤的通知。
總結(jié)
以上是生活随笔為你收集整理的iOS开发(4)UITextField的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IOS开发调用系统相机和打开闪光灯
- 下一篇: iOS开发(5)动态监听键盘通知