iOS 动态创建 UILabels

发布于 2024-11-19 06:01:43 字数 109 浏览 5 评论 0原文

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

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

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

The number of UILabels depends on data that's fetched from a website.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

尤怨 2024-11-26 06:01:43

您必须用代码而不是界面生成器来制作它们

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

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];
 }
等往事风中吹 2024-11-26 06:01:43

通用问题的通用答案:

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

A generic answer for a generic question:

while (labelsToDisplay) 
{
    UILabel *label = [[UILabel alloc] initWithFrame:aFrame];
    [label setText:@"someText"];
    [aViewContainer addSubview:label];
    [label release];
}
ま柒月 2024-11-26 06:01:43
   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;
   }
   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;
   }
对风讲故事 2024-11-26 06:01:43
 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];
 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];
深爱不及久伴 2024-11-26 06:01:43
UILabel *lblTitle=[[UILabel alloc]init];
[lblTitle setFrame:CGRectMake(0, 0, 100, 100)];
[lblTitle setText:@"MAK"];
[lblTitle setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:lblTitle];

- 这里UILable将被动态创建。
- 但属性的设置会有所不同。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文