UI四大常用控件:UILabel, UITextField, UIButton, UIImageView及相关小习题
UILabel
- UILabel(标签): 是显示文本的控件
- UILabel是UIView子类,UILabel是能显⽰示文字的视图。
UILabel *userNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(30, 100, 100, 30)];
userNameLabel.text = @“用户名”;
[containerView addSubview:userNameLabel];
[userNameLabel release];
注:containerView 已经添加到根视图上
UILabel属性设置:
UITextField
- UITextField(输入框):是控制文本输入和显示的控件。
- UITextField允许用户编辑文字(输入)
// 使⽤用初始化⽅方法创建对象
UITextField *userNameTextField = [[UITextField
alloc] initWithFrame:CGRectMake(100, 100, 190, 30)];
// 设置边框⻛风格
userNameTextField.borderStyle =
UITextBorderStyleRoundedRect;
// 设置占位符
userNameTextField.placeholder = @“⼿手机号/邮箱";
[containerView addSubview:userNameTextField];
[userNameTextField release];
占位符:
placeholder:占位字符串(没有任何输入时,给出的提示字符串)
textField.placeholder = @“请输⼊入⽤用户名”;
输入控制属性:
外观属性:
TextField常用代理方法:
//当textField将要开始编辑的时候告诉委托人
- (BOOL)textFieldShouldBeginEditing:(UITextField
*)textField
//当textField已经编辑的时候告诉委托人
- (void)textFieldDidBeginEditing:(UITextField
*)textField
//当textField将要完成编辑的时候告诉委托人
- (BOOL)textFieldShouldEndEditing:(UITextField
*)textField
//当textField已经完成编辑的时候告诉委托人
- (void)textFieldDidEndEditing:(UITextField *)textField
//当点击键盘上回车按键时候告诉委托人
- (BOOL)textFieldShouldReturn:(UITextField *)textField
//- 注: 委托⼈人即delegate
UIButton
- UIButton(按钮):是响应用户点击的控件。
UIbutton初始化方法相对不同
// 便利构造器方法创建对象
UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
loginButton.frame = CGRectMake(30, 200, 60, 30);
// 设置button的标题
[loginButton setTitle:@"登录" forState:UIControlStateNormal];
// 添加点击事件
[loginButton addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
[containerView addSubview:loginButton];
UIButton外观
外观二
UIImageView
- UIImageView是IOS里用来显示图片的类
创建image:
// 图片文件路径
NSString *path = [[NSBundle mainBundle]
pathForResource:@"1" ofType:@"jpg"];
// 创建一个UIImage对象,使用initWithContentOfFile: 方法
UIImage *image = [UIImage
imageWithContentsOfFile:path];
// 创建一个UIImageView对象,使用initWithImage: 方法
UIImageView *imageView = [[UIImageView alloc]
initWithImage:image];
imageView.frame = CGRec