Swift - UILabel自动调整文本长度

4
正如之前多次提到的那样,label.sizeToFit()和label.numberOfLines=0对我都不起作用。每次我最终得到的标签高度都是以“…”结束的文本相同。
我真的希望保持标签宽度恒定,而高度可调节,但是我在论坛上找到的任何解决方案都不适用于我。我知道把它作为现有问题的评论会更好,但不幸的是由于我的积分账户,我无法做到这一点。
你知道其他实际有效的实现UILabel可调节高度的方法吗?
以下是我的内容:
    cell.restaurantName.setTranslatesAutoresizingMaskIntoConstraints(false)
    cell.restaurantName.numberOfLines = 0
    cell.restaurantName.sizeThatFits(CGSizeMake(CGFloat(172.0),    CGFloat(MAXFLOAT)))

这里输入图片描述然后我尝试完全按照下面发布的内容进行操作。

cell.restaurantName.numberOfLines = 0
cell.restaurantName.sizeToFit()

限制宽度的约束

在此输入图片描述

再次看到带有三个点的文本

提前致谢


我相信有太多的解决方案可以放入一个好的详细答案中,这是我在几秒钟内找到的最简单的 - A-Live
1个回答

4

最简单的解决方案是创建标签,将标签添加到视图中,然后设置约束条件。只要水平约束已经设置好了,标签就知道自己的最大宽度,这样你就可以调用label的sizeThatFits方法使其正确地调整大小。以下是一些Objective C代码,可以完全满足你的需求。

UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.translatesAutoresizingMaskIntoConstraints = NO;
label.numberOfLines = 0;
label.text = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
[self.view addSubview:label];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-(10)-[label(300)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(10)-[label]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label)]];
[label sizeThatFits:CGSizeMake(300.0, MAXFLOAT)];

既然这是一个Swift问题,那么以下是Swift语言的答案:

var label: UILabel = UILabel(frame: CGRectZero)
label.setTranslatesAutoresizingMaskIntoConstraints(false)
label.numberOfLines = 0
label.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
view.addSubview(label)

let views = Dictionary(dictionaryLiteral: ("label", label))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-(10)-[label(300)]", options: NSLayoutFormatOptions(0), metrics: nil, views: views))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-(10)-[label]", options: NSLayoutFormatOptions(0), metrics: nil, views: views))
label.sizeThatFits(CGSizeMake(CGFloat(300.0), CGFloat(MAXFLOAT)))

使用storyboard,您只需执行以下操作即可:

Storyboard-label-constraints

 @interface ViewController ()

@property (nonatomic, weak) IBOutlet UILabel *label;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.label.numberOfLines = 0;
    self.label.text = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
    [self.label sizeToFit];
}

@end

它和IBOutlet标签一样吗?有什么区别吗? - theDC
如果你正在使用 IBOutlet,你可以使用 Interface Builder 来设置宽度和位置的约束。上面的代码展示了如何在代码库中实现,但在 Interface Builder 中,你只需拖动一个标签并设置其 x 和 y 约束,然后设置宽度约束,就可以达到同样的效果。同时,确保将行数设置为 0。 - darren102
还是老样子,没有改变...我会编辑问题并附上代码和截图,请您看一下,如果您不介意的话。 - theDC
你能看一下吗,@darren102。 - theDC
是的,终于成功了!这都是因为宽度限制的问题。一旦我添加了它,Xcode就会出现高度模糊的警告,有没有什么方法可以消除这个警告?但我很高兴它能够正常工作,谢谢Darren! - theDC
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接