UILabel 实现国际象棋棋盘界面

使用UILabel文本标签实现国际棋盘界面



- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    //设置棋子的宽和高,此处设置宽高都相同

    CGFloat eachWidth = [UIScreen mainScreen].bounds.size.width/8;

    //CGFloat eachHeight = [UIScreen mainScreen].bounds.size.height/8;

    

    //文字数组

    NSArray *arr1 = @[@"",@"",@"",@"",@"",@"",@"",@""];

    for (int i = 0; i < 8; ++i) {

        for (int j = 0; j < 8; ++j) {

            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(j * eachWidth, i * eachWidth + 20, eachWidth, eachWidth)];

            if ((i + j) % 2 == 0) {

                label.backgroundColor = [UIColor blackColor];

            }else{

                label.backgroundColor = [UIColor whiteColor];

            }

            if (i == 0 || i == 7) {

                label.text = arr1[j];

            }else if (i == 1 || i == 6){

                label.text = @"";

            }

            if (i == 0 || i == 1) {

                label.textColor = [UIColor redColor];

            }else if(i == 6 || i == 7){

                label.textColor = [UIColor greenColor];

            }

            label.textAlignment = NSTextAlignmentCenter;

            [self.view addSubview:label];

            


        }

    }

    

}

你可能感兴趣的:(ios,UILabel,标签,国际象棋)