IOS UI TextFiled常用总结

首先先创建一个UITextField作为例子
//1.输入框的基本使用(创建显示,回收键盘)
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 100, 300, 30)];
    //输入框默认没有边框, 添加边框
    textField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:textField];
以下是常用属性
//2.常用属性
    //<1>文本字体
    //textField.font
    //<2>文本颜色
    //textField.textColor
    //<3>设置密码输入键盘
    textField.secureTextEntry = YES;
    //<4>设置键盘类型(设置数字键盘)
    textField.keyboardType = UIKeyboardTypeNumberPad;
    //<5>设置清除模式
    textField.clearButtonMode = UITextFieldViewModeAlways;
    //<6>设置空白提示(输入提示)
    textField.placeholder = @"请输入密码";
    //<7>设置是否自动大写
    textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    //<8>是否自动显示修正后的单词
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    //<9>设置文本对齐方式
    textField.textAlignment = NSTextAlignmentRight;
    //<10>设置背景图片(需要边框类型设置为UITextBorderStyleNone)
    textField.background = [UIImage imageNamed:@"back.png"];
    //<11>左侧提示图片
    textField.leftViewMode = UITextFieldViewModeAlways;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    imageView.image = [UIImage imageNamed:@"Icon-Small-50.png"];
    textField.leftView = imageView;
关闭键盘的两种方式
第一种:textField放弃第一响应者身份,使用这种方式要记得加入UITextFieldDelegate协议和自身delegate的设置,
即textField.delegate = self;
这样才能实现点击return按钮回收键盘功能
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    //放弃作为第一响应者
    [textField resignFirstResponder];
    return YES;
}
第二种是点击空白处回收键盘,注意:要是使用textField放弃第一响应者身份这个方式要记得把
textField设置为全局变量,
因为这个方法名里没有textField这个参数
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //[_textField resignFirstResponder];
    [self.view endEditing:YES];
}
与textField有关的属性就总结到这里,希望对初学者提供帮助

版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/yuqingzhude/p/4836542.html