UI_TextField、UIButton
- UITextField
- UIButton
UITextField
NSObject -> UI系 -> UIResponder -> UIView -> UIControl -> UITextField
UITextFiled的使用:
- 开辟空间并初始化(如果本类有初始化方法,使用自己的;否则使用父类的)。
- 设置文本显示,输入相关的属性
- 添加到父视图上,用以显示
- 释放
-
1 {
2 UITextField *userNameTextField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 190, 30)];
3 userNameTextField.borderStyle = UITextBorderStyleRoundedRect;
4 userNameTextField.placeholder = @"手机号/邮箱";
5 [containerView addSubview:userNameTextField];
6 [userNameTextField release];
7 }
UITextField核心功能:文本显示/输入控制/外观配置
文本显示
属性名 描述 示例 text 要显示的文本内容 textField.text = @"Captivity"; textColor 文本内容的颜色 textField.textColor = [UIColor redColor]; textAlignment 文本的对齐方式(水平方向) textField.textAlignment = NSTextAlignmentLeft; font 文本字体 textField.font = [UIFont fontWithName:@"Helvetica-Bold"size:20];
//黑体加粗,20号字placeholder 占位字符串(没有任何输入时,给出的提示字符串) textField.placeholder = @"请输入用户名";
输入控制:
属性名 描述 示例 enabled 是否允许输入 textField.enable = NO;//不允许输入,不弹出键盘
textField.enable = YES;//默认YES,允许输入clearOnBeginEditing 是否开始输入的时候清空输入框的内容 textField.clearOnBeginEditing = YES;//清空 textField.clearOnBeginEditing = NO;//不清空 secureTextEntry 是否文字以圆点格式显示 textField.secureTextEntry = YES;//密码模式textField.secureTextEntry = NO;//普通模式 keyboardType 弹出键盘的类型(枚举值) textField.keyboardType = UIKeyboardTypeNumberPad;//数字键盘 inputView 自定义输入视图(默认是键盘) textField.inputView = myInputView; returnKeyType 键盘右下角return按钮类型(枚举值) textField.returnKeyType = UIReturnKeyNext; inputAccessoryView 输入视图上方的辅助视图(默认nil) textField.inputAccessoryVies = myAccessoryView;
外观控制:边框样式,清除按钮,辅助视图
属性名 描述 示例 borderStyle 边框样式 textField.borderStyle = UITextBorderStyleRoundedRect; clearButtonMode 清除按钮模式 (枚举值) textField.clearButtonMode =
UITextFieldViewModeAlways;//总是显示清除按钮leftView 输入框左视图 textField.leftView = leftView; leftViewMode 左视图的显示模式 textField.leftViewMode = UITextFieldViewModeAlways;
//总是显示左视图rightView 输入框右视图 textField.rightView = rightView; rightViewMode 右视图的显示模式 textField.rightViewMode = UITextFieldViewModeAlways;
自动回收键盘
- 设置代理
- textField.delegate = self;
- 让self称为代理,遵守协议
- UITextFieldDelegate
- 实现代理方法
- [textField resignFirstResponder]
1 /**
2 * UITextField
3 */
4 // 1.创建并设置大小
5 UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMinX(lable.frame), CGRectGetMaxY(lable.frame) + 50, CGRectGetWidth(lable.frame), 35)];
6 textField.tag = 10010;
7 // 2.设置相关属性
8 textField.backgroundColor = [UIColor whiteColor]; //设置背景颜色
9 textField.borderStyle = UITextBorderStyleRoundedRect; //设置文本框风格
10 textField.placeholder = @"匆匆那年,我们究竟说了几遍再见";
11 //设置提示语
12 textField.keyboardType = UIKeyboardTypeEmailAddress; //设置键盘格式
13 textField.clearButtonMode = UITextFieldViewModeWhileEditing;//设置编辑时可全部清除
14 textField.keyboardAppearance = UIKeyboardAppearanceDark; //键盘颜色
15 textField.returnKeyType = UIReturnKeyNext; //改变return为go/next等
16 // textField.secureTextEntry = YES; //设置密码保密格式
17 // textField.enabled = NO; //设置是否可以输入
18 textField.clearsOnBeginEditing = YES; //输入之前,清空上次的内容
19 //设置左视图[UIImage imageNamed:@"IMG_1620.JPG"]
20 UIView *leftView = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 35, 35)]autorelease];
21 leftView.backgroundColor = [UIColor whiteColor];
22 textField.leftView = leftView;
23 textField.leftViewMode = UITextFieldViewModeAlways;
24 UILabel *lefLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 35, 35)];
25 lefLab.text = @" ✉️";
26 lefLab.adjustsFontSizeToFitWidth = YES;
27 [leftView addSubview:lefLab];
28 [lefLab release];
29 // UIImageView *imageView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
30 // imageView.image = [UIImage imageNamed:@"IMG_1623.JPG"];
31 // [leftView insertSubview:imageView atIndex:0];
32 // [imageView release];
33 // 3.添加
34 [self.window addSubview:textField];
35 // 4.release一次
36 [textField release];
UIButton
NSObject -> UI系 -> UIResponder -> UIView -> UIControl ->UIButton
UIButton:(按钮)相应用户点击的空间。
UIButton的使用
- 创建button对象(如果本类有初始化方法,使用自己的;否则使用父类的);
- 设置按钮显示相关的属性
- 为按钮添加点击事件
- 添加按钮到父视图上,用以显示
- 按钮无需释放(因为使用的是类方法创建的button)
-
1 //UIButton
2 UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
3 loginButton.frame = CGRectMake(30, 200, 60, 30);
4 [loginButton setTitle:@"登录"forState:UIControlStateNormal];
5 [loginButton addTarget:self action:@selector(login:)forControlEvents:UIControlEventTouchUpInside];
6 [containerview addSubview:loginButton];
7 }
UIButton添加事件
方法名 描述 示例 addTarget:
action:
forControlEvents:为按钮添加事件,指定按钮点击之后,执行target的action方法 [loginButton addTarget:self
action:@selector(login:)
forControlEvents:UIControlEventTouchUpInside];removeTarget:
action:
forControlEvents:移除按钮的点击事件 [loginButton removeTarget:self
action:@selector(login:)
forControlEvents:UIControlEventTouchUpInside];
外观控制
方法名 描述 示例 setTitle:
forState:设置指定状态下的标题 [loginButton setTitle:@"登录"
forState:UIControlStateNormal];titleForState 获取指定状态下的标题 NSString *normalTitle = [loginButton titleForState:UIControlStateNormal]; setTitleColor:
ForState:设置指定状态下标题颜色 [loginButton setTitleColor:[UIColor redColor]
forState:UIControlStateNormal];titleColorForState: 获取指定状态下标题颜色 UIColor *normalTitleColor = [loginButton
titleColorForState:UIControlStateNormal];setTitleShadowColor:
forState:设置指定状态下标题的阴影颜色 [loginButton setTitleShadowColor:[UIColor redColor]
forState:UIControlStateNormal];titleColorForState: 获取指定状态下的标题阴影颜色 UIColor *normalTitleShadowColor =[loginButton
titleShadowColorForState:UIControlStateNormal];
外观设置(图片)
方法名 描述 示例 setImage:
forState:设置指定状态下的前景图片 [loginButton setImage:[UIImage imageNamed:@"login.png"]
forState:UIControlStateNormal];imageForState 获取指定状态下的前景图片 UIImage *normalImage = [loginButton imageForState:UIControlStateNormal]; setBackgroundImage:
forState:设置指定状态下的背景图片 [loginButton setBackgroundImage:[UIImageimageNamed:@"login.png"]
forState:UIControlStateNormal];backgroundImageForState: 获取指定状态下的背景图片 UIImage *normalImage = [loginButton backgroundImageForState:
UIControlStateNormal];
1 /**
2 * UIButton
3 */
4 // 1.创建button,使用便利构造器,设置样式
5 UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
6 // 2.设置相关属性
7 button.frame = CGRectMake(CGRectGetMinX(textField.frame), CGRectGetMaxY(textField.frame) + 400, 70, 35);
8 // button.showsTouchWhenHighlighted = YES;
9 button.backgroundColor = [UIColor colorWithRed:.9 green:.9 blue:.9 alpha:0.7];
10 [button setTitle:@"Sure" forState:UIControlStateNormal];
11 [button setTitle:@"OrNot" forState:UIControlStateHighlighted];
12 [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
13 [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
14
15 //设置按钮出发的方式
16 [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
17 [button setBackgroundImage:[UIImage imageNamed:@"button_bg"] forState:UIControlStateNormal];
18 // 3.添加
19 [self.window addSubview:button];
20 }

浙公网安备 33010602011771号