iOS create UILabels dynamically

后端 未结 6 777
甜味超标
甜味超标 2020-12-31 05:54

Sometimes I want my view to contain 5 UILabels, sometimes 3 and sometimes n.

The number of UILabels depends on data that\'s fetched fro

相关标签:
6条回答
  • 2020-12-31 06:33
     UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(125, 12,170,20)];
                lbl.text=@"IOS";
                lbl.textAlignment = NSTextAlignmentCenter;
                lbl.textColor = [UIColor whiteColor];
                lbl.font = [UIFont fontWithName:@"AlNile" size:10.0];
                lbl.backgroundColor=[[UIColor redColor]colorWithAlphaComponent:0.5f];
                lbl.layer.borderColor=[UIColor blackColor].CGColor;
                lbl.layer.borderWidth=1.0f;
                lbl.layer.cornerRadius = 6.0f;
                [self.view addSubview:lbl];
    
    0 讨论(0)
  • 2020-12-31 06:40

    Create a TextView to show the text on the labels and a NSArray to contain the data.

    For more information:

    http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html

    http://developer.apple.com/library/ios/documentation/uikit/reference/UITextView_Class/Reference/UITextView.html

    0 讨论(0)
  • 2020-12-31 06:41

    A generic answer for a generic question:

    while (labelsToDisplay) 
    {
        UILabel *label = [[UILabel alloc] initWithFrame:aFrame];
        [label setText:@"someText"];
        [aViewContainer addSubview:label];
        [label release];
    }
    
    0 讨论(0)
  • 2020-12-31 06:53
       NSArray *dataArray;
       float xCoordinate=10.0,yCoordinate=10.0,width=100,height=40; 
       float ver_space=20.0;
       for (int i = 0; i <dataArray.count; i++)
       {
           UILabel *label =  [[UILabel alloc] initWithFrame: CGRectMake(xCoordinate,yCoordinate,width,height)];
           label.text = [dataArray objectAtIndex:i];
           [self.view addSubview:label];
    
           yCoordinate=yCoordinate+height+ver_space;
       }
    
    0 讨论(0)
  • 2020-12-31 06:55
    UILabel *lblTitle=[[UILabel alloc]init];
    [lblTitle setFrame:CGRectMake(0, 0, 100, 100)];
    [lblTitle setText:@"MAK"];
    [lblTitle setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:lblTitle];
    

    -Here UILable will be created dynamically. -but property will be set differently.

    0 讨论(0)
  • 2020-12-31 06:56

    You'll have to make them in code instead of interface builder

     for (int i = 0; i < n; i++)
     {
        UILabel *label =  [[UILabel alloc] initWithFrame: CGRectMake(/* where you want it*/)];
        label.text = @"text"; //etc...
        [self.view addSubview:label];
        [label release];
     }
    
    0 讨论(0)
提交回复
热议问题