IOS开发之——九宫格-设置图片、标题和按钮(2)

一 概述

本文给上节的九宫格布局中的组件设置响应的属性和效果

  • UIImageView设置图片
  • UILabel设置文字
  • UIButton设置点击效果和文字说明

二 效果图

三 代码

3.1 OC模式下(ViewController.m)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#import "ViewController.h"
//九宫格常量
#define kAppViewW 80 //宽
#define kAppViewH 90 //高
#define kColCount 3 //列
#define kStartY 20

@interface ViewController ()
@property (nonatomic,strong) NSArray *appList;

@end

@implementation ViewController

- (NSArray *)appList
{
if (_appList==nil) {
_appList=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil]];
}
return _appList;
}
- (void)viewDidLoad {
[super viewDidLoad];

//九宫格界面
CGFloat marginX=(self.view.bounds.size.width-kColCount*kAppViewW)/(kColCount+1);
CGFloat marginY=10;

for (int i=0; i<10; i++) {
//行
// 0,1,2 ->0
//3,4,5->1
int row=i/kColCount;
//列
//0,3,6->0
//1,4,7->1
//2,5,8->2
int col=i%kColCount;
CGFloat x=marginX+col*(marginX+kAppViewW);
CGFloat y=kStartY+ marginY+row*(marginY+kAppViewH);

UIView *appView=[[UIView alloc]initWithFrame:CGRectMake(x, y, kAppViewW, kAppViewH)];
//appView.backgroundColor=[UIColor redColor];
[self.view addSubview:appView];
NSDictionary *dict=self.appList[i];

//九宫格背景色
//1->UIImageView
UIImageView *icon=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kAppViewW, 50)];
//icon.backgroundColor=[UIColor greenColor];
//设置图像
icon.image=[UIImage imageNamed:dict[@"icon"]];
//图像填充
icon.contentMode=UIViewContentModeScaleAspectFit;

[appView addSubview:icon];
//2->UILabel->应用程序名称
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(icon.frame), kAppViewW, 20)];
//label.backgroundColor=[UIColor blueColor];
//设置程序名称
label.text=dict[@"name"];
//字体
label.font=[UIFont systemFontOfSize:13.0];
label.textAlignment=NSTextAlignmentCenter;
[appView addSubview:label];

//3->UIButton->下载按钮
UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(label.frame), kAppViewW, 20)];
//button.backgroundColor=[UIColor yellowColor];
//设置背景图片
[button setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"] forState:UIControlStateHighlighted];
[button setTitle:@"下载" forState:UIControlStateNormal];
//字体
button.titleLabel.font=[UIFont systemFontOfSize:12.0];

[appView addSubview:button];
}
}

@end