文本控件UIlabel
使用:
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(20,100,280,100)];
label.text=@"Hello World2,Hello World2,Hello World2,Hello World2,Hello World2";
label.font=[UIFont systemFontOfSize:18];//设置字体和字号
按钮控件UIButton
UIButton *button1=[UIButton buttonWithType:UIButtonTypeSystem];
button1.frame=CGRectMake(40,100,240,30);
button1.backgroundColor=[UIColor blueColor];
[button1 setTitle:@"click me"forState:UIControlStateNormal];//按钮的初始状态为正常状态
[self.view addSubview:button1];
按钮的初始状态可设置为
UIControlStateNormal //正常状态
UIControlStateHighlighted //高亮状态
UIControlStateDisabled //不可用状态
UIControlStateSelected //选中状态 用于一些充当切换开关的按钮
文本输入框控件UItextField
//要遵守UItextFieldDelegate协议
UITextField* textField=[[UITextField alloc]initWithFrame:CGRectMake(20, 100, 280, 30)];
textField.borderStyle=UITextBorderStyleRoundedRect;
textField.delegate=self;
textField.placeholder=@"不超过11位的数字或字母";
textField.clearButtonMode=UITextFieldViewModeWhileEditing;//设置一键清除按钮
//给输入框增加左视图
UIImageView *imageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"image"]];
textField.leftView=imageView;
textField.leftViewMode=UITextFieldViewModeWhileEditing;//编辑时显示
开关控制UISwitch
UISwitch *swi=[[UISwitch alloc]initWithFrame:CGRectMake(100, 400, 100, 40)];
swi.onTintColor=[UIColor greenColor];
swi.tintColor=[UIColor whiteColor];
swi.thumbTintColor=[UIColor whiteColor];
[self.view addSubview:swi];
[swi addTarget:self action:@selector(changeColor:)forControlEvents:UIControlEventValueChanged]; //changeColor方法需要自己在下面实现
选择器控件 UIPickerView
通过代理和数据源的方法对其进行设置和数据源填充,代理模式的应用之一。例如选择时间、出生年月分
UIPickerView选中数据的回调处理
如下:
//显示当前所选择的位置
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSLog(@"%lu%lu",row,component);
}
CALayer对视图进行修饰
//创建圆角控件
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.frame=CGRectMake(100, 600, 100, 100);
btn.backgroundColor=[UIColor blueColor];
btn.layer.masksToBounds=YES;//只有使用YES时对视图边界进行修饰的效果才会显示
btn.layer.cornerRadius=10;//设置圆角的半径
//创建带边框的
btn.layer.borderColor=[UIColor greenColor].CGColor;
btn.layer.borderWidth=5;
//添加阴影
btn.layer.shadowColor=[UIColor greenColor].CGColor;
btn.layer.shadowOffset=CGSizeMake(10, 10);//设置阴影位置与原件的相对偏移
btn.layer.shadowOpacity=1;//设置阴影透明度。不设置默认全透明,无效果
[self.view addSubview:btn];