文本控件(UITextField)继承了UIControl控件,所以作为活动控件使用!创建该控件有两种方法:一、拖控件。二、纯代码搞定。其实本质一样。不过还是建议采用纯代码,这样对控件的可定制性非常高。
textField=[[UItextField alloc]initWithFrame:CGRectMake(120.0f, 80.0f, 150.0f, 30.0f)];
[textField setBorderStyle:UITextBorderStyleRoundedRect];//外框类型
textField.placeholder=@”请输入:”;//默认显示的字
textField.secureTextEntry=YES;//是否为密码式的输入框
textField.clearButtonMode=UITextFieldViewModeWhileEditing;
textField.delegate=self;
textField.keyboardType=UIKeyboardTypeDefault;//键盘显示类型
textField.autocapitalizationType=UITextAutocorrectionTypeYes;//是否开启自动提醒更正功能
textField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;//居中显示
typedef enum {
UIControlContentVerticalAlignmentCenter = 0,
UIControlContentVerticalAlignmentTop = 1,
UIControlContentVerticalAlignmentBottom = 2,
UIControlContentVerticalAlignmentFill = 3,
} UIControlContentVerticalAlignment;
textField.textColor=[UIColor redColor];//字体颜色
textField.font=[UIFont fontWithName:@”Times New Roman” size:35];//字体类型以及尺寸
textField.background=[UIImage imageNamed:@”background.png”];//背景图片
[self.view addSubview:textField];
//委托方法
-(void)textFieldDidBeginEditing:(UITextField *)textField
{//点击文本框时会调用该方法
NSLog(@”你开始点击textField了!”);
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{//结束点击时会调用该方法
NSLog(@”你结束点击textField了!”);}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{//点击Return关闭键盘
//关于关闭键盘的常用方法可以参考另一篇博客
//http://blog.csdn.net/it_ds/article/details/44830695
[textField resignFirstResponder];
return YES;
}
//限定文本框的输入字数
-(BOOL)textField:(UITextField )textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString )string
{
if (range.location>=MAX_LENGTH) {
return NO;
}
return YES;}