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

时间:2015-01-21 19:29:09

标签: ios swift uilabel

如前所述,label.sizeToFit()和label.numberOfLines = 0都不适用于我。我总是以相同的标签高度结束,文字以“......”结尾。

我真的希望保持标签宽度不变,高度可调,但我在论坛周围发现的任何解决方案都不适合我。我知道最好将其作为对现有问题的评论,但不幸的是,由于我的观点,我无法做到这一点。

你知道其他任何可以实现UILabel高度可调的方法吗?

这就是我所拥有的:

    cell.restaurantName.setTranslatesAutoresizingMaskIntoConstraints(false)
    cell.restaurantName.numberOfLines = 0
    cell.restaurantName.sizeThatFits(CGSizeMake(CGFloat(172.0),    CGFloat(MAXFLOAT)))

enter image description here  然后我尝试与下面发布的完全相同

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

约束宽度

enter image description here

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

提前致谢

1 个答案:

答案 0 :(得分:3)

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

使用故事板,您可以执行以下操作:

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