我的iOS学习历程 - UIView和UILabel
今天主要说的是UIView和UILabel的属性和应用
首先我将今天讲的方法总结在下面
UIView
1 初始化一个UIView,起始点 从屏幕的左上角(0,0)点 开始计算
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
2 加背景颜色
view.backgroundColor = [UIColor redColor];
3 添加到window上面,实际上添加进一个数组中
[self.window addSubview:view];
4 取出子视图
NSArray *subView = self.window.subviews;
5 获取中心点(x = frame.origin.x + frame.size.width / 2,y同x)
CGPoint center = view.center;
NSLog(@”%@”,NSStringFromCGPoint(center));
6 更改中心点坐标
view.center = CGPointMake(200, 300);
7 bounds 边界 可以控制子视图的坐标系,默认bounds的起始点 就是从(0,0)开始
改变父视图的bounds 相当于改变子视图的坐标系(也就是原点坐标),父视图是不发生变化 只是更改子视图的位置
view.bounds = CGRectMake(0, 100, 200, 100);
8 添加子视图(必须把子视图添加到父视图的范围之内否则无法交互)
[view addSubview:view1];
视图属性
9 隐藏视图( 如果父视图被隐藏 那么父视图上面所有子视图也会被隐藏)
aView.hidden = NO;// YES为显示 NO为隐藏
10 透明属性 (如果父视图的透明度发生变化 那么上面的所有子视图都会发生变化)
aView.alpha = 0.5
11 取出某一子视图的父视图
UIView *superView = bView.superview;
12 添加一个标签tag值(重点) 给视图加标记 然后可以用viewWithTag取出(不能给0)
aView.tag = 10;
通过tag值取出视图
UIView *tView = [self.window viewWithTag:10];
13 把视图插入到指定的位置
[self.window insertSubview:bView atIndex:0];
14在指定的视图上面添加子视图
[self.window insertSubview:bView aboveSubview:aView];
15 在指定的视图下面添加子视图
[self.window insertSubview:bView belowSubview:aView];
16 把某一个子视图放在最前面
[self.window bringSubviewToFront:aView];
17 把某一个子视图放在最后面
[self.window sendSubviewToBack:aView];
18 交换两个子视图的位置
[self.window exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
19 从父视图上删除
[bView removeFromSuperview];
UILabel
20 显示文本的控件label初始化
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
label.backgroundColor = [UIColor yellowColor];
21 添加字
label.text = @”Application windows are expected to have a root view”;
22 设置字体对齐方式(0: 左对齐 1: 居中 2: 右对齐)
label.textAlignment = 0;
23 设置字体颜色
label.textColor = [UIColor redColor];
24 设置字体字号(大小)
label.font = [UIFont systemFontOfSize:18];
25 获取系统所有安装的字体
NSArray *fontArray = [UIFont familyNames];
26 设置字体类型
label.font = [UIFont fontWithName:@”Apple Color Emoji” size:12];
27 设置阴影颜色
label.shadowColor = [UIColor blackColor];
28 调整阴影的位置 (y正值阴影向下偏 x正值向右偏)
label.shadowOffset = CGSizeMake(0, 15);
29 多行显示
完全显示 填-1 或者 0;超出范围就无法显示了
label.numberOfLines = 0;
30 设置断行模式
label.lineBreakMode = 5;
UIView例题:
// UIView 代表一个矩形区域
// 初始化一个UIView
// 起始点 从屏幕的左上角(0,0)点 开始计算
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
// 加背景颜色
view.backgroundColor = [UIColor redColor];
// 添加到window上面
[self.window addSubview:view];
UILabel例题:(由于没有学习图片 暂时用label代替)
UILabel *movieIconLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 20, 100, 200)];
movieIconLabel.backgroundColor = [UIColor grayColor];
movieIconLabel.text = @"敢死队";
movieIconLabel.textAlignment = 1;
[self.window addSubview:movieIconLabel];
[movieIconLabel release];
UILabel *ratingLabel = [[UILabel alloc]initWithFrame:CGRectMake(150, 20, 100, 30)];
ratingLabel.backgroundColor = [UIColor whiteColor];
ratingLabel.text = @"评分: 7.1";
[self.window addSubview:ratingLabel];
[ratingLabel release];
UILabel *rating_countLabel = [[UILabel alloc]initWithFrame:CGRectMake(270, 20, 100, 30)];
rating_countLabel.backgroundColor = [UIColor whiteColor];
rating_countLabel.text = @"(4925评论)";
[self.window addSubview:rating_countLabel];
[rating_countLabel release];
UILabel *dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(150, 60, 100, 30)];
dateLabel.backgroundColor = [UIColor whiteColor];
dateLabel.text = @"20140901";
[self.window addSubview:dateLabel];
[dateLabel release];
UILabel *runtimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(150, 110, 100, 30)];
runtimeLabel.backgroundColor = [UIColor whiteColor];
runtimeLabel.text = @"126 min";
[self.window addSubview:runtimeLabel];
[runtimeLabel release];
UILabel *genresLabel = [[UILabel alloc]initWithFrame:CGRectMake(150, 160, 150, 30)];
genresLabel.backgroundColor = [UIColor whiteColor];
genresLabel.text = @"动作/冒险/惊悚";
[self.window addSubview:genresLabel];
[genresLabel release];
UILabel *countryLabel = [[UILabel alloc]initWithFrame:CGRectMake(150, 200, 100, 30)];
countryLabel.backgroundColor = [UIColor whiteColor];
countryLabel.text = @"美国|法国";
[self.window addSubview:countryLabel];
[countryLabel release];
UILabel *makeNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 220, 100, 50)];
makeNameLabel.backgroundColor = [UIColor whiteColor];
makeNameLabel.text = @"制作人";
makeNameLabel.font = [UIFont systemFontOfSize:24];
[self.window addSubview:makeNameLabel];
[makeNameLabel release];
UILabel *actorsLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 280, 350, 80)];
actorsLabel.backgroundColor = [UIColor whiteColor];
actorsLabel.text = @"西尔维斯特•史泰龙 Sylvester Stallone,杰斯•斯坦森 Jason Statham,梅尔•吉布森 MelGibson,李连杰 Jet Li";
actorsLabel.numberOfLines = 0;
[self.window addSubview:actorsLabel];
[actorsLabel release];
UILabel *moviePlotLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 350, 150, 40)];
moviePlotLabel.backgroundColor = [UIColor whiteColor];
moviePlotLabel.text = @"电影情节";
moviePlotLabel.font = [UIFont systemFontOfSize:24];
[self.window addSubview:moviePlotLabel];
[moviePlotLabel release];
UILabel *plotLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 380, 350, 300)];
plotLabel.backgroundColor = [UIColor whiteColor];
plotLabel.text = @"在第敢死队3 剧照敢死队3 剧照 (50张)三部的故事中,巴尼(西尔维斯特·史泰龙饰)与克里斯马斯(杰森·斯坦森饰)领衔的敢死队将正面迎战昔日战友、如今的军火枭雄康拉德·斯通班克斯(梅尔·吉布森饰)。斯通班克斯曾侥幸死里逃生过一次,他对敢死队下达了绝杀令巴尼则另有一番打算:为迎战强敌,巴尼决定给敢死队注入新鲜血液,招募了更快更强的高科技战斗新生,搭配长枪硬炮的硬汉前辈,展开一番大决战.";
plotLabel.numberOfLines = 0;
[self.window addSubview:plotLabel];
[plotLabel release];