iOS 九宫格的实现

#import <UIKit/UIKit.h>

 

@interface ViewController : UIViewController

 

@property(strong,nonatomic) UILabel *lblNums;

 

@end

 

#import "ViewController.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

 

 int x=50,y=50,width=50,height=50;

    int a=1;

    for (int i=0; i<3; i++)

    {

        for (int j=0;j<3; j++)

        {

            self.lblNums=[[UILabel alloc] initWithFrame:CGRectMake(x+j*55, y+i*55, width, height)];

            

           self.lblNums.backgroundColor=[UIColor colorWithRed:(arc4random()%12/11.0) green:(arc4random()%23/22.0) blue:(arc4random()%34/33.0) alpha:1.0];

            

            NSString *newnum=[@(a) stringValue];

                self.lblNums.text=newnum;

                self.lblNums.textAlignment=1;

                a++;

                [self.view addSubview:self.lblNums];

        }

    }

 

    

}

 

posted @ 2016-03-28 20:51  唐唐_010  阅读(363)  评论(0编辑  收藏  举报