UI_View01 输出九九乘法表

本文介绍如何使用Objective-C在iOS应用中创建一个动态的乘法表视图。通过循环迭代的方式设置UILabel来显示乘法表的内容,并利用条件判断为每个标签赋予不同的背景颜色以增强视觉效果。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    for (int i=1; i<10; i++) {
        BOOL a = YES;
        for (int j=i; j<10; j++) {
            
//            UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 12, 30, 10)];
//            NSString *str = [NSString stringWithFormat:@"%d*%d=%d",i,j,i*j];
//            label1.text = [NSString stringWithString:str];
//            label1.textAlignment = NSTextAlignmentCenter;
//            label1.font =[UIFont systemFontOfSize:9];
//            
//            UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake((i-1)*35, 50+(j-1)*30, 35, 30)];
//            
//            
//            if (a == YES) {
//                view1.backgroundColor = [UIColor greenColor];
//                a=NO;
//            }else{
//                view1.backgroundColor = [UIColor redColor];
//                a=YES;
//            }
//            [self.window addSubview:view1];
//            [view1 addSubview:label1];
//            
            UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake((i-1)*35, 50+(j-1)*30, 35, 30)];
            NSString *str = [NSString stringWithFormat:@"%d*%d=%d",i,j,i*j];
            label1.text = [NSString stringWithString:str];
            label1.textAlignment = NSTextAlignmentCenter;
            label1.font =[UIFont systemFontOfSize:10];
            
//            UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake((i-1)*35, 50+(j-1)*30, 35, 30)];
            
            
            if (a == YES) {
                label1.backgroundColor = [UIColor greenColor];
                a=NO;
            }else{
                label1.backgroundColor = [UIColor yellowColor];
                a=YES;
            }
            [self.window addSubview:label1];
//            [view1 addSubview:label1];
            
        }
    }
    
    return YES;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值