iOS开发之 UITextField

来源:互联网 发布:网络用语打call的意思 编辑:程序博客网 时间:2024/05/22 04:41

一、UITextField文本输入的控件


UITextField -文本输入控件,它就和我们登录qq上输入账号和密码的输入框是一样的



1、初始化uitextField

UITextField *pewTextField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 40)];

pewTextField 设置背景图片

1⃣️. pewTextField.background = [UIImage imageNamed:@“F23C46DE-8B36-4048-8365-3FB4643922E9”];//设置不禁用时的背景

 2⃣️.pewTextField.disabledBackground = [UIImage imageNamed:@“D205413E-C6AF-484E-A586-4982B97F6D3F"];//设置禁用时的背景


2.   enabled是否禁用控件 默认是yes,没有禁用

             pewTextField.enabled = YES;


3.pewTextField 自身的属性

    1⃣️pewTextField.borderStyle = UITextBorderStyleRoundedRect;//设置边框的类型

    2⃣️pewTextField.placeholder = @"请输入密码";//提示的文字,当编辑时消失

    3⃣️pewTextField.textAlignment = NSTextAlignmentCenter;//显示的文本居中

    4⃣️pewTextField.secureTextEntry = YES;//设置成不是明文显示

    5⃣️pewTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;//设置键盘输入样式

    6⃣️pewTextField.keyboardAppearance = UIKeyboardAppearanceDark;//设置键盘显示的样式

    7⃣️pewTextField.returnKeyType = UIReturnKeyDefault;//设置返回键的样式

    8⃣️pewTextField.clearButtonMode = UITextFieldViewModeWhileEditing;//设置清除按钮什么时候出现



4.在TextField 左边或者右边添加一个视图

    UIView *left = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 40)];

    left.layer.cornerRadius = 4;//设置拐角

    left.backgroundColor = [UIColor redColor];

    pewTextField.leftView = left;//把需要放到TextField的左边或者右边的视图 赋值给TextField的左边或者右边

    pewTextField.leftViewMode = UITextFieldViewModeAlways;//还需要设置左边或者右边视图的样式


5.代理 

//    代理:让别人帮忙做某件事儿 自己在本类中实现不了得功能 让其他类帮咱们实现

//    咱们需要让pewTextField帮咱们 获得输入完成后的字符串

//    如果要使用代理需添加 代理的协议


对于TextField,我们在使用的时候,一般会用到它的代理方法,正如我们得判断自己是否开始在这个输入框输入东西,或者是我们已经在这个输入框输入完毕了呢 ?我们就使用代理,在使用代理的时候,我们导入代理,并且得给TextField挂上代理

如下:

@interface ViewController : UIViewController<UITextFieldDelegate>

我们还得给 TextField 挂上代理,一般在初始化TextField的地方挂上

pewTextField.delegate = self;//如果代理方法不执行,先检查是否挂上代理

在这里我们就不把TextField 的代理全部列举出来了,我们看看TextField常用的两个代理方法


//开始编辑的时候使用- (void)textFieldDidBeginEditing:(UITextField *)textField{    NSLog(@"已经开始编辑");}//编辑结束的时候调用- (void)textFieldDidEndEditing:(UITextField *)textField{    NSLog(@"%@",textField.text);    }







0 0
原创粉丝点击