动态添加标签

发布于 2024-10-31 02:11:50 字数 223 浏览 4 评论 0原文

我需要一些指导。我正在解析 XML 并从中获取成员名称。名称应显示在标签上。现在我对任意数量的成员名称感到困惑。如何根据会员姓名的数量放置标签。

示例:如果我得到 5 个名称,则应显示 5 个带有名称的标签。

有什么好的编程指导吗?

我有一个滚动视图,并且已经添加了一个 UIVew 作为子视图来显示一些静态标签。现在名称的标签应该显示在 UIView 上。

谢谢..

I need some guidance. I am parsing an XML and get the members name from it. The name should be displayed on label . Now I am confused due to arbitrary number of members name. How can I put labels according to number of member's name.

Example : if I get 5 names then 5 label should be displayed with name .

any good programming guidance ?

I have a scrollview and already added a UIVew as a subview to display some static label. now name's label should be displayed on UIView.

Thanks..

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

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

发布评论

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

评论(4

凉月流沐 2024-11-07 02:11:50
for (NSInteger i = 0, y = 50; i < [memberNameArray count]; i++, y += 30) {
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, y, 100, 25)];
    nameLabel.text = [memberNameArray objectAtIndex:i];
    [myScollView addSubview:nameLabel];
    [nameLabel release];
}

假设 memberNameArray 是包含要显示的名称的 NSString 数组。您还需要根据滚动视图中标签的位置更改框架的 x、y、宽度、高度和 y += 30 值。

for (NSInteger i = 0, y = 50; i < [memberNameArray count]; i++, y += 30) {
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, y, 100, 25)];
    nameLabel.text = [memberNameArray objectAtIndex:i];
    [myScollView addSubview:nameLabel];
    [nameLabel release];
}

Assuming memberNameArray is the array of NSString which contains the names to display. And also you need to change the x, y, width, height values of the frame and y += 30 based on the position of the labels in scroll view.

对你而言 2024-11-07 02:11:50

下面的代码可用作根据数组值获取标签数量的参考。

为每个 UILabel 设置正确的框架...

 for(int i = 0 ; i  < [myArrayOfValue count]; i++)
    {
        UILabel* myLabel = [[UILabel alloc]initWithFrame:CGRectMake(myX,(myY + i*myHeight),myWidth,myHeight);
        myLabel.text = [myArrayOfValue objectAtIndex:i];

        ...........
        ............
        [myView addSubview:myLabel];
         myLabel.tag = i ;
        [myLabel release];
    }

The below code could be used as reference for getting number of label depending on Array values..

Set the correct frame for each UILabel...

 for(int i = 0 ; i  < [myArrayOfValue count]; i++)
    {
        UILabel* myLabel = [[UILabel alloc]initWithFrame:CGRectMake(myX,(myY + i*myHeight),myWidth,myHeight);
        myLabel.text = [myArrayOfValue objectAtIndex:i];

        ...........
        ............
        [myView addSubview:myLabel];
         myLabel.tag = i ;
        [myLabel release];
    }
凉城 2024-11-07 02:11:50

UILabel *lblname=[[UILabel alloc]initWithFrame:CGRectMake(x,y,height,width)];
[lblname setText:@"任意文本"];
[lblname setBackgroundColor:[UIColor clearColor]];
[lblname setFont:[UIFont systemFontOfSize:15]];
[lblname setNumberOfLines:2];
[lblname setTextAlignment:NSTextAlignmentCenter];
[self.view addSubview:lblname];

UILabel *lblname=[[UILabel alloc]initWithFrame:CGRectMake(x,y,height,width)];
[lblname setText:@"any text"];
[lblname setBackgroundColor:[UIColor clearColor]];
[lblname setFont:[UIFont systemFontOfSize:15]];
[lblname setNumberOfLines:2];
[lblname setTextAlignment:NSTextAlignmentCenter];
[self.view addSubview:lblname];

雪花飘飘的天空 2024-11-07 02:11:50

for(int i = 0 ; i < [myArrayOfValue 计数]; i++)
{
UILabel* myLabel = [[UILabel alloc]initWithFrame:CGRectMake(myX,(myY + i*myHeight),myWidth,myHeight);
myLabel.text = [myArrayOfValue objectAtIndex:i];

    ...........
    ............
    [myView addSubview:myLabel];
     myLabel.tag = i ;
    [myLabel release];
}

for(int i = 0 ; i < [myArrayOfValue count]; i++)
{
UILabel* myLabel = [[UILabel alloc]initWithFrame:CGRectMake(myX,(myY + i*myHeight),myWidth,myHeight);
myLabel.text = [myArrayOfValue objectAtIndex:i];

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