【问题标题】:Dynamically adding multiline label in a view在视图中动态添加多行标签
【发布时间】:2015-09-11 20:34:46
【问题描述】:

我在我的应用程序中动态创建 UILabel 以显示食谱的方向列表。标签正确填充,一个接一个地显示所有项目。

问题是当文本在标签的下一行出现时,它与下一个标签重叠。

我已将 numberOfLines 设置为 0,并将 lineBreakMode 设置为 NSLineBreakByWordWrapping。这有助于标签在多行上显示文本。

我已尝试调整标签的高度,正如您将在代码中看到的那样,但它不起作用。

如何防止标签多行导致标签重叠?

这是用多行填充标签的代码:

//add all the directions to the uiview
    for (int i = 0; i < self.recipe.directions.count; i++)
    {
        UILabel *label =  [[UILabel alloc] initWithFrame: CGRectMake(0,(i+1)*25,280,25)];
        label.lineBreakMode = NSLineBreakByWordWrapping; //multiple lines in a label
        label.numberOfLines = 0;
        label.text =[NSString stringWithFormat: @"%@", self.recipe.directions[i]];
        [label sizeToFit]; // resize the width and height to fit the text
        NSLog(@"Actual height is: %f", label.frame.size.height); // Use this for spacing any further elements


        CGSize expectedLabelSize = [label.text sizeWithFont:label.font
                                            constrainedToSize:label.frame.size
                                                lineBreakMode:NSLineBreakByWordWrapping];
        //adjust the label the the new height.
        CGRect newFrame = label.frame;
        newFrame.size.height = expectedLabelSize.height;
        label.frame = newFrame;
        [self.directionsView addSubview:label];

    }

【问题讨论】:

    标签: ios objective-c uilabel


    【解决方案1】:

    不要使用(i+1)*25 的 y 位置来初始化标签,而应该存储最后一个标签的底部位置

    CGFloat lastLabelBottomCoordinate = 25;
    CGFloat spaceBetweenLines = 10;
    for (int i = 0; i < 10; i++)
    {
        UILabel *label =  [[UILabel alloc] initWithFrame: CGRectMake(0, lastLabelBottomCoordinate + spaceBetweenLines,280,25)];
        label.lineBreakMode = NSLineBreakByWordWrapping; //multiple lines in a label
        label.numberOfLines = 0;
        label.text =[NSString stringWithFormat: @"This is a very long text to see if the text have more than 2 lines"];
        [label sizeToFit]; // resize the width and height to fit the text
        NSLog(@"Actual height is: %f", label.frame.size.height); // Use this for spacing any further elements
    
    
        CGSize expectedLabelSize = [label.text boundingRectWithSize:CGSizeMake(label.frame.size.width, MAXFLOAT)
                                                            options:NSStringDrawingUsesLineFragmentOrigin
                                                         attributes:@{NSFontAttributeName : label.font}
                                                            context:nil].size;
        //adjust the label the the new height.
        CGRect newFrame = label.frame;
        newFrame.size.height = expectedLabelSize.height;
        label.frame = newFrame;
    
        lastLabelBottomCoordinate = label.frame.origin.y + label.frame.size.height;
        [self.view addSubview:label];
    
    }
    

    看起来是这样的:

    【讨论】:

    • 谢谢。这样可行。但是文本之间的距离太大了。我怎样才能减少它?此外,不推荐使用 sizeWithFont。可以用什么代替?
    • 解决了距离问题。刚刚初始化 lastLabelBottomCoordinate = 25
    • 我刚刚编辑了我的答案。对于标签之间的间距,您应该更改标签之间的间距变量。对于不推荐使用的方法,现在您需要使用boundingRectWithSize:options:attributes:context: 方法
    • 太棒了。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多