【问题标题】:iOS create UILabels dynamicallyiOS 动态创建 UILabel
【发布时间】:2011-10-01 20:28:49
【问题描述】:

有时我希望我的视图包含 5 个 UILabels,有时是 3 个,有时是 n

UILabel 的数量取决于从网站获取的数据。

【问题讨论】:

    标签: ios dynamic uilabel


    【解决方案1】:

    您必须在代码中而不是界面构建器中制作它们

     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];
     }
    

    【讨论】:

    • 嗨,我需要创建 UILabel 并根据我的 NSMutableArray 显示数据并将其显示在 UITableview 中。我该怎么做?
    【解决方案2】:

    一般问题的一般答案:

    while (labelsToDisplay) 
    {
        UILabel *label = [[UILabel alloc] initWithFrame:aFrame];
        [label setText:@"someText"];
        [aViewContainer addSubview:label];
        [label release];
    }
    

    【讨论】:

      【解决方案3】:
         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;
         }
      

      【讨论】:

      • 你能扩展你的答案吗?为什么 OP 应该使用你的代码?
      【解决方案4】:
       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];
      

      【讨论】:

        【解决方案5】:
        UILabel *lblTitle=[[UILabel alloc]init];
        [lblTitle setFrame:CGRectMake(0, 0, 100, 100)];
        [lblTitle setText:@"MAK"];
        [lblTitle setBackgroundColor:[UIColor blueColor]];
        [self.view addSubview:lblTitle];
        

        -这里会动态创建UILable。 - 但属性会设置不同。

        【讨论】:

          【解决方案6】:
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-14
          • 2015-03-03
          • 1970-01-01
          • 1970-01-01
          • 2018-08-05
          • 1970-01-01
          相关资源
          最近更新 更多