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

Ios Swift-UILabel自动调整为文本长度,ios,swift,uilabel,Ios,Swift,Uilabel,正如前面多次提到的,label.sizeToFit()和label.numberOfLines=0都不适合我。一直以来,我都以相同高度的标签结束,文字以“…”结尾 我真的很想保持标签宽度恒定,高度可调,但我在论坛上找到的任何解决方案都不适合我。我知道最好是把它作为对现有问题的评论,但不幸的是,由于我的观点,我不能这样做 你知道有没有其他方法可以实现UILabel的可调高度,这确实有效 以下是我所拥有的: cell.restaurantName.setTranslatesAutoresiz

正如前面多次提到的,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()
宽度受限

我又看到了有三个点的文本


提前感谢

最简单的解决方案是创建标签,将标签添加到视图中,然后设置约束。只要以这样的方式设置水平约束,标签就知道它的最大宽度,然后可以调用标签的sizeThatFits,这样它就可以正确地调整大小。下面是一些客观的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吗