1 //
2 // ViewController.m
3 // 国际象棋
4 //
5 // Created by 大欢 on 16/1/19.
6 // Copyright © 2016年 bjsxt. All rights reserved.
7 //
8
9 #import "ViewController.h"
10
11 @interface ViewController ()
12
13 @end
14
15 @implementation ViewController
16
17 - (void)viewDidLoad {
18
19 [super viewDidLoad];
20
21 NSArray * array = @[@"车",@"马",@"象",@"王",@"后",@"象",@"马",@"车"];
22
23 CGFloat width = CGRectGetWidth(self.view.frame)/8;
24
25 for (int i = 0; i < 8; i++) {
26 for (int j = 0; j < 8;j++) {
27
28 UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(j * width, i * width + 20, width, width)];
29 label.textAlignment = NSTextAlignmentCenter;
30 if ((i + j)%2 == 0) {
31 label.backgroundColor = [UIColor blackColor];
32 } else {
33 label.backgroundColor = [UIColor whiteColor];
34 }
35
36 if (i == 0 || i == 7) {
37 label.text = array[j];
38 }
39
40 if (i == 1 || i == 6) {
41 label.text = @"兵";
42 }
43
44 if (i == 0 || i == 1) {
45 label.textColor = [UIColor redColor];
46 }
47
48 if (i == 6 || i == 7) {
49 label.textColor = [UIColor greenColor];
50 }
51
52 [self.view addSubview:label];
53 }
54 }
55 }
56 @end
![]()