【问题标题】:How to implement Tags如何实现标签
【发布时间】:2026-02-09 03:10:02
【问题描述】:

我正在开发一个应用程序,我需要在其中显示每个讨论和每个评论的标签,如下图所示。我正在从类似这样的服务中获取标签。

tags =             (
                Registration,
                Apps,
                PublicSpeaking,
                Marketing,
                Sales
            );

我希望这些标签显示在给定图像的“标签”部分下。

这就是我尝试实现的方式,任何类型的教程或链接都会非常有帮助。

for (int i=0; i< [self.menuListArray count]; i++) {
    NSString *tag_string = [self.menuListArray objectAtIndex:i];
    CGSize tagSize = [tag_string sizeWithFont:[UIFont fontWithName:@"Roboto-Light" size:13.0]
                         constrainedToSize:CGSizeMake(230, FLT_MAX)
                             lineBreakMode:UILineBreakModeTailTruncation];
    UIImageView *tag_bgimage = [[UIImageView alloc]init];
    [tag_bgimage setFrame:CGRectMake(i*tagSize.width+5, stringSize.height+5, tagSize.width+4, 25)];
    [tag_bgimage setBackgroundColor:[UIColor grayColor]];
    [tag_bgimage setUserInteractionEnabled:NO];

    UILabel *tagLabel = [[UILabel alloc]init];
    [tagLabel setFrame:CGRectMake(0, 0, tagSize.width, 25)];
    [tagLabel setBackgroundColor:[UIColor clearColor]];
    [tagLabel setText:tag_string];
    [tagLabel setFont:[UIFont fontWithName:@"Roboto-Light" size:12.0]];
    [tagLabel setTextAlignment:NSTextAlignmentCenter];

    [tag_bgimage addSubview:tagLabel];
    [cell addSubview:tag_bgimage];

}

【问题讨论】:

  • 你能解释一下吗?标签在这里做什么
  • 你能说得更具体点吗.. 你想显示超过 3 个标签还是别的什么?
  • 标签只是为了表明这个讨论属于所有主题。就像在 * 中一样,我们需要设置提问的标签。
  • 标签可以是任意数量。也超过 3 个。
  • cocoacontrols.com/search?utf8=%E2%9C%93&q=token 看这里是显示令牌文件,您可以将其扩展到您的代码中

标签: ios iphone objective-c tags


【解决方案1】:

您可以使用this 控制器。看起来像这样:

【讨论】:

    【解决方案2】:

    我推荐使用 UICollectionView 来实现标签,而不是使用添加框架。它的代码更少、开销更少且易于实现。

    点击此链接实施:

    https://codentrick.com/create-a-tag-flow-layout-with-uicollectionview/

    这就是它的样子。请注意它也很灵活。

    希望对你有帮助

    【讨论】: