Group creation UI object in objective-c

| | August 8, 2015

I have this code. I would like to make it more compact. Is it possible to add the groups similar controls?

UILabel *controlFreeName = [[UILabel alloc] init];
controlFreeName.frame = CGRectMake(40.0f, 70.0f, 120.0f, 50.0f);
controlFreeName.text = @"Free";

UILabel *controlNoFreeName = [[UILabel alloc] init];
controlNoFreeName.frame = CGRectMake(40.0f, 110.0f, 120.0f, 50.0f);
controlNoFreeName.text = @"No free";

UILabel *controlEquipmentName = [[UILabel alloc] init];
controlEquipmentName.frame = CGRectMake(40.0f, 150.0f, 120.0f, 50.0f);
controlEquipmentName.text = @"Equipment";

UILabel *controlTypeName = [[UILabel alloc] init];
controlTypeName.frame = CGRectMake(40.0f, 190.0f, 180.0f, 50.0f);
controlTypeName.text = @"Type";

One Response to “Group creation UI object in objective-c”

  1. Joe Hallenbeck on November 30, -0001 @ 12:00 AM

    Try something like this

    NSArray * myArray = [[NSArray alloc]initWithObjects:@"Test1",@"Test2",@"Test2", nil];
    
    for (int i = 0; i < 3; i++)
    {
        UILabel * myLabel = [[UILabel alloc]initWithFrame:CGRectMake(10*5*i, 10*5*i, 100, 100)];
        myLabel.text = [myArray objectAtIndex:i];
        int r = arc4random() % 255;
        int g = arc4random() % 255;
        int b = arc4random() % 255;
        myLabel.backgroundColor = [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1];
        [self.view addSubview: myLabel];
    }
    

    enter image description here

Leave a Reply