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,您只需执行以下操作即可:
@interfaceViewController ()@property (nonatomic, weak) IBOutletUILabel *label;
@end@implementationViewController
- (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
- darren102
7
它和IBOutlet标签一样吗?有什么区别吗? - theDC
如果你正在使用 IBOutlet,你可以使用 Interface Builder 来设置宽度和位置的约束。上面的代码展示了如何在代码库中实现,但在 Interface Builder 中,你只需拖动一个标签并设置其 x 和 y 约束,然后设置宽度约束,就可以达到同样的效果。同时,确保将行数设置为 0。 - darren102