本文主要是介绍UI-UITextField,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一般属性:
UITextField *textfield = [[UITextField alloc]init];textfield.bounds = CGRectMake(0, 0, 200, 25);textfield.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds) - 50);//边框类型(圆角矩形)textfield.borderStyle = UITextBorderStyleRoundedRect;textfield.placeholder = @"请输入文本";textfield.font = [UIFont systemFontOfSize:15];//配置是否首字母大写(需要要首字母大写)textfield.autocapitalizationType = UITextAutocapitalizationTypeNone;//配置是否自动纠错textfield.autocorrectionType = UITextAutocapitalizationTypeNone;//配置清除按钮显示状态,默认不显示(全部清除)textfield.clearButtonMode = UITextFieldViewModeWhileEditing;//配置键盘显示类型
// textfield.keyboardType = UIKeyboardTypeNumberPad;//配置安全输入(显示小点,输密码的时候要用)textfield.secureTextEntry = YES;// //委托
// textfield.delegate = self;[self.view addSubview:textfield];[textfield release];
如果要给它加上限制,监听它那就要用委托了
textfield.secureTextEntry = YES;
在延展中遵守协议
@interface ViewController () <UITextFieldDelegate>{UIButton *_lightbutton;UILabel *_label1;
}
协议里的一些方法:
#pragma mark - <UITextFieldDelegate>
//文本输入过滤
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{//限制长度if (range.location > 11) {return NO;}BOOL shouldChangeCharacter = YES;// 只能输入数字的时候
// BOOL shouldChangeCharacter = NO;//删除键判断if ([string length] == 0) {shouldChangeCharacter = YES;}//过滤数字if ([@"1234567890" rangeOfString:string].location != NSNotFound) {//不允许输入数字shouldChangeCharacter = NO;//只允许输入数字
// shouldChangeCharacter = YES;}return shouldChangeCharacter;
}//配置是否可以开始编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{NSLog(@"%@",NSStringFromSelector(_cmd));return YES;}
//开始编辑
- (void)textFieldDidBeginEditing:(UITextField *)textField{NSLog(@"%@",NSStringFromSelector(_cmd));}
//编辑完成
- (void)textFieldDidEndEditing:(UITextField *)textField{NSLog(@"%@",NSStringFromSelector(_cmd));if ([textField.text length] > 0) {_label1.text = textField.text;//调整_lable1的大小CGSize size = [self sizeWithString:_label1.textfont:_label1.fontconstraint:CGSizeMake(150, 250)];CGRect frame = _label1.frame;frame.size = size;_label1.frame = frame;}}
//配置是否相应return键
- (BOOL)textFieldShouldReturn:(UITextField *)textField{NSLog(@"%@",NSStringFromSelector(_cmd));//收起键盘[textField resignFirstResponder];return YES;
}
测试的时候加以了一个:UILable:(显示输入的内容)
_label1 = [[UILabel alloc]init];_label1.bounds = CGRectMake(0, 0, 150, 30);_label1.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height*2/3);_label1.text = @"haha";_label1.backgroundColor = [UIColor grayColor];//配置显示函数 0表示不限制_label1.numberOfLines = 0;//配置换行模式_label1.lineBreakMode = NSLineBreakByWordWrapping | NSLineBreakByTruncatingTail;[self.view addSubview:_label1];[_label1 release];
这篇关于UI-UITextField的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!