一、各程序的使用情况
UILabel算是我们进入UI的第一步,在了解UILabel之前,我们先了解下各程序的使用情况
1、- (void)loadView // 加载xib调用
2、- (void)viewDidLoad //加载完xib调用 如果手写代码 在这里面 实例化所有视图
3、- (void)viewWillAppear:(BOOL)animated // 即将显现的时候调用,可以在 viewWillAppear 里面加载数据
4、-(void)viewDidAppear:(BOOL)animated //已经显示的时候调用(视图已经被渲染到屏幕的时 候)
5、-(void)viewWillDisappear:(BOOL)animated // 视图即将消失在屏幕上得时候调用
6、- (void)viewDidDisappear:(BOOL)animated //视图已经消失在屏幕上得时候调用
7、- (void)didReceiveMemoryWarning //内存不足的时候调用
8、NSLog(@"%s %d”,__func__,__LINE__);//输出执行的步骤和代码行数
二、UILable
1、UILable 文本控件 用来显示文本内容的
CGFloat sWith = CGRectGetWidth([UIScreen mainScreen].bounds);
lable = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, sWith-10*2, 200)];
2 、UILable的各种属性方法
在iOS7.1之前 默认是由边框颜色的(白色),现在默认的是透明的
lable.backgroundColor = [UIColor whiteColor];
// UILable 文本控件 用来显示文本内容的
CGFloat sWith = CGRectGetWidth([UIScreen mainScreen].bounds);
lable = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, sWith-10*2, 200)];
// 在iOS7.1之前 默认是由边框颜色的(白色),现在默认的是透明的
lable.backgroundColor = [UIColor whiteColor];
lable.text = @"杨鹏飞大傻逼";//输入文本
lable.font = [UIFont systemFontOfSize:20];//设置字体
// lable.adjustsFontSizeToFitWidth = YES;//根据宽度自适应显示文字
lable.textColor = [UIColor redColor];//设置字体颜色
lable.shadowColor = [UIColor blackColor];//设置字体阴影
lable.shadowOffset = CGSizeMake(1, -1);//默认(0,-1);第一个参数 负责 左右 ,第二个参数负责 上下
// textAlignment: 设置文本向左向右 或者居中 的方法
lable.textAlignment = NSTextAlignmentCenter;
lable.numberOfLines = 10;
[self.view addSubview:lable];
三、UIButton
1、UIButton 按钮控件 点击按钮 会触发某个事件
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
b.backgroundColor = [UIColor greenColor];
b.frame = CGRectMake(120, 400, 100, 40);
2、属性
1⃣️[b setTitle:@"切换" forState:UIControlStateHighlighted];//设置文本
2⃣️[b setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];//设置文本颜色
3⃣️b.showsTouchWhenHighlighted = YES;//点击的时候显示亮
4⃣️[b setBackgroundImage:[UIImage imageNamed:@"01300000010387130458375241146_s"] forState:UIControlStateNormal];//给button设置常规时的图片
5⃣️ [b setBackgroundImage:[UIImage imageNamed:@"21133735282"] forState:UIControlStateHighlighted];//设置点亮时的图片
6⃣️[b addTarget:self action:@selector(changeTitle:) forControlEvents:UIControlEventTouchUpInside];//验证
7⃣️ 按钮是否被选中,默认为NO
b.selected = NO;
我们创建一个label,两个button(一个切换,一个返回),当我们点击切换button的时候,label显示字体一,点返回的时候,label显示0
lable = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, sWith-10*2, 200)];
lable.backgroundColor = [UIColor whiteColor];
[self.view addSubview:lable];
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
b.backgroundColor = [UIColor greenColor];
b.frame = CGRectMake(120, 400, 100, 40);
[b setTitle:@"切换" forState:UIControlStateHighlighted];
[b addTarget:self action:@selector(changeTitle:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:b];
UIButton *b1 = [UIButton buttonWithType:UIButtonTypeCustom];
b1.backgroundColor = [UIColor greenColor];
b1.frame = CGRectMake(120, 300, 100, 40);
[b1 setTitle:@"返回" forState:UIControlStateHighlighted];
[b1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
b1.showsTouchWhenHighlighted = YES;
[b1 addTarget:self action:@selector(changeTitle:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:b1];
两个button点击触发的方法
- (void)changeTitle:(UIButton *)sender
{
NSLog( @"%d",sender.selected);
if (sender.selected != YES) {
lable.text = @"一";
lable.textColor = [UIColor redColor];
sender.selected = YES;
}else{
lable.text = @"二";
lable.textColor = [UIColor blueColor];
sender.selected = NO;
}
}