效果图:
核心代码:
//
// LiuJieViewController.m
// 01-应用管理
//
// Created by XinYou on 15-2-3.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import "LiuJieViewController.h"
@interface LiuJieViewController ()
@property (nonatomic, strong) NSArray *apps;
@end
@implementation LiuJieViewController
- (NSArray *)apps{
if (_apps == nil) {
// 获取app.plist的路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil];
// 加载数据
_apps = [NSArray arrayWithContentsOfFile:path];
}
return _apps;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 总列数
int totalColumns = 3;
// 应用的尺寸(宽高)
CGFloat appViewW = 85;
CGFloat appViewH = 90;
// 应用之间的间距
CGFloat marginX = (self.view.frame.size.width - appViewW * totalColumns) / (totalColumns + 1);
CGFloat marginY = 15;
for (int index = 0; index < self.apps.count; index++) {
NSDictionary *appInfo = self.apps[index];
// 1,整个应用对应一个UIView
UIView *appView = [[UIView alloc] init];
// [appView setBackgroundColor:[UIColor blueColor]];
// 计算行号和列号
int row = index / totalColumns;
int col = index % totalColumns;
CGFloat appViewX = marginX + col * (appViewW + marginX);
CGFloat appViewY = 30 + row * (appViewH + marginY);
appView.frame = CGRectMake(appViewX, appViewY, appViewW, appViewH);
[self.view addSubview:appView];
// 2,应用图标
UIImageView *iconView = [[UIImageView alloc] init];
CGFloat iconViewW = 45;
CGFloat iconViewH = 45;
CGFloat iconViewX = (appViewW - iconViewW) / 2;
CGFloat iconViewY = 0;
// 设置位置和尺寸
iconView.frame = CGRectMake(iconViewX, iconViewY, iconViewW, iconViewH);
// [iconView setBackgroundColor:[UIColor redColor]];
iconView.image = [UIImage imageNamed:appInfo[@"icon"]];
[appView addSubview:iconView];
// 3,应用名字
UILabel *nameLabel = [[UILabel alloc] init];
CGFloat nameLabelW = appViewW;
CGFloat nameLabelH = 20;
CGFloat nameLabelX = 0;
CGFloat nameLabelY = iconViewY + iconViewH;
// 设置位置和尺寸
nameLabel.frame = CGRectMake(nameLabelX, nameLabelY, nameLabelW, nameLabelH);
// [nameLabel setBackgroundColor:[UIColor greenColor]];
// 设置文字
nameLabel.text = appInfo[@"name"];
// 设置字体
nameLabel.font = [UIFont systemFontOfSize:13];
// 设置文字居中
nameLabel.textAlignment = NSTextAlignmentCenter;
[appView addSubview:nameLabel];
// 4,下载按钮
UIButton *downloadBtn = [[UIButton alloc] init];
CGFloat downloadBtnX = 12;
CGFloat downloadBtnY = nameLabelY + nameLabelH;
CGFloat downloadBtnW = appViewW - 2 *downloadBtnX;
CGFloat downloadBtnH = 20;
downloadBtn.frame = CGRectMake(downloadBtnX, downloadBtnY, downloadBtnW, downloadBtnH);
// [downloadBtn setBackgroundColor:[UIColor grayColor]];
// 设置默认的背景图片
UIImage *normalImage = [UIImage imageNamed:@"buttongreen"];
[downloadBtn setBackgroundImage:normalImage forState:UIControlStateNormal];
// 设置高亮状态下得背景图片
UIImage *highlightedImage = [UIImage imageNamed:@"buttongreen_highlighted"];
[downloadBtn setBackgroundImage:highlightedImage forState:UIControlStateHighlighted];
// 设置按钮的文字
[downloadBtn setTitle:@"下载" forState:UIControlStateNormal];
// 下面这种方式也可以设置按钮的文字,但是不推荐使用。
// 因为按钮是分状态的,用这种方式无法设置指定状态下的文字
// downloadBtn.titleLabel.text = @"下载";
// 设置按钮的文字字体
downloadBtn.titleLabel.font = [UIFont systemFontOfSize:13];
[appView addSubview:downloadBtn];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end