Swift - UILabel 自动调整为文本长度

Posted

技术标签:

【中文标题】Swift - UILabel 自动调整为文本长度【英文标题】:Swift - UILabel automatically adjusted to text length 【发布时间】:2015-01-21 19:29:08 【问题描述】:

正如之前多次提到的,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()

对宽度有限制

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

提前致谢

【问题讨论】:

我相信有太多的解决方案可以放在一个好的详细答案中,这是我在几秒钟内找到的most simple。 【参考方案1】:

最简单的解决方案是创建标签,将标签添加到视图中,然后设置约束。只要以这样一种方式设置水平约束,标签就知道它的最大宽度可以是多少,那么您就可以为标签调用 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)];

标签的宽度最大为 300,其余的高度取决于 UILabel 中的文本内容

因为这是一个快速的问题,所以这里的答案是一样的 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)))

使用情节提要,您只需执行以下操作:

 @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一样吗?有什么不同吗? 如果您使用 IBOutlet,您将使用 Interface Builder 设置宽度和定位约束。上面的代码展示了如何在代码库中执行此操作,但在 Interface Builder 中,您只需拖动标签并设置其 x、y 约束,然后设置宽度约束,它将以相同的方式工作。确保也将行数设置为 0。 谢谢,我会试一试,如果我成功了告诉你 还是一样,没有任何变化……我会用代码和截图编辑问题,如果你不介意请看一下 你能看看@darren102

以上是关于Swift - UILabel 自动调整为文本长度的主要内容,如果未能解决你的问题,请参考以下文章

禁用 UILabel、UIText 等的自动调整字体。在 iOS/Swift 中?

根据 Swift 中的 UILabel 文本调整 UIView 高度

如何使用自动布局调整 UILabel 宽度以适合文本?

当视图宽度发生变化时,如何使 UILabel 正确调整其尺寸?

UILabel 动态调整大小 - Swift

自动调整 UILabel 文本大小以适应 UILabel 宽度