IOS开发控件视图day03:控件常用属性(Label、TextFile、Button、image、imageView)

1、Label
(1)声明

@property(weak,nonatomic)IBOutlet UILabel *label1;
//IBOutlet关联控件,storyboard中按住ctrl键拖线关联

也可以直接创建:

UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(40, 240, 200, 20)];

(2)定义属性

label1.frame = CGRectMake(40, 240, 200, 20); //坐标大小(X轴,Y轴,宽,高)
label1.font = [UIFont systemFontOfSize:13];//字体尺寸
label1.textColor = [UIColor blackColor];//字体颜色:黑色
label1.backgroundColor = [UIColor redColor];//背景颜色
//RGB设置颜色:取值范围0~1.0f,需要/255 ; alpha透明度:取值范围0~1.0f,1表示不透明
//label1.backgroundColor = [UIColor colorWithRed:230.0f/255.0f green:70.0f/255.0f blue:70.0f/255.0f alpha:1.0];
label1.textAlignment = NSTextAlignmentLeft;//字体对齐方式:左对齐
label1.text = @"李小伟牛逼!";//文本内容
//下面两行设置UILabel多行显示
label1.lineBreakMode = NSLineBreakByCharWrapping;
label1.numberOfLines = 0;
[label1 sizeToFit];//设置大小自适应
label1.layer.borderWidth = 1.0f;//设置边框
label1.layer.cornerRadius = 4.0f;//设置圆角

[self.view addSubview:label1];//添加到视图

2、Textfile
(1)声明

@property(weak,nonatomic)IBOutlet UItextfile *txt1;

也可以直接创建:

UITextField *txt1 = [[UITextField alloc]initWithFrame:CGRectMake(40, 200, 200, 20)];

(2)定义属性

txt1.textColor =[UIColor blackColor];//输入的字体颜色
txt1.backgroundColor = [UIColor clearColor];//输入框文本背景颜色
txt1.keyboardType = UIKeyboardTypeNumberPad;//键盘格式:数字键盘
txt1.font = [UIFont systemFontOfSize:14];//输入的文本尺寸
txt1.textAlignment = NSTextAlignmentLeft;//输入文本内容对齐方式:左对齐
txt1.placeholder = @"请输入";//占位符,输入框提示文字,默认为灰色
txt1.secureTextEntry = YES;//设置输入框密码形式,隐藏输入内容
//设置一键清空小叉子按钮:当文本框在编辑时出现
txt1.clearButtonMode = UITextFieldViewModeWhileEditing;
//clearButtonMode还有三个属性:
//UITextFieldViewModeNever,  清空按钮永不出现
//UITextFieldViewModeUnlessEditing  不编辑的时候出现
//UITextFieldViewModeAlways 只要输入框有内容就出现
txt1.layer.borderWidth = 1.0f;//设置边框
txt1.layer.cornerRadius = 4.0f;//设置圆角

[self.view addSubview:txt1];//添加到视图

3、Button
(1)声明

@property(weak,nonatomic)IBOutlet UIButton *btn1;

也可以直接创建:

UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];

(2)定义属性

 btn1 = [[UIButton alloc] initWithFrame:CGRectMake(16, 62, 10, 19)];//坐标大小
    //CGRect rect1 = CGRectMake(16, 62, 10, 19);
    //btn1.frame = rect1;
btn1.tag = 10001;//按钮唯一标识
[btn1 setImage:[UIImage imageNamed:@"图片名称"] forState:(UIControlStateNormal)];//按钮背景图片
//按钮状态
//普通状态(默认Default):UIControlStateNormal
//高亮状态(按钮按下去手指未松开的时候highlight):UIControlStateHighlight
//失效状态(若enabled为NO,就是处于disable状态,按钮不可点击):UIControlStateDisabled
[btn1 setTitle:@"按钮" forState:UIControlStateNormal];//按钮文本
btn1.titleLabel.font = [UIFont systemFontOfSize:16];//文本尺寸
[btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];//文本颜色
btn1.backgroundColor = [UIColor redColor];//按钮背景颜色
btn1.layer.cornerRadius = 4.0f;//按钮圆角
btn1.layer.masksToBounds = YES;//对button内lab约束
btn1.layer.borderColor = [[UIColor redColor]CGColor];//按钮边框颜色
btn1.layer.borderWidth = 1.0;//按钮边框宽度
[btn1 addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];//创建触发函数,函数名为login

[self.view addSubview:btn1];//添加到视图

4、image

UIImage *img = [UIImage imageWithContentsOfFile:@"/Users/WenYu007/Desktop/img001.png"];

5、imageView

UIImageView *imageView1 = [[UIImageView alloc]init];//创建视图对象
//UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
imageView1.frame = CGRectMake(83, 133, 216, 36);//设置图片尺寸
imageView1.center = self.view.center;//让图片在中间位置显示   
imageView1.image = [UIImage imageNamed:@"logo"];//加载图片
//给imageView添加响应点击事件  
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickImage:)];
[imageView1 addGestureRecognizer:tapGesture];
imageView1.userInteractionEnabled = YES; 

[self.view addSubview:imageView1];//添加到视图

你可能感兴趣的:(IOS控件基础)