Swift - UILabel 自动调整为文本长度
Swift - UILabel automatically adjusted to text length
正如之前多次提到的,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,这样它就会正确调整下载大小。下面是一些 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 问题,所以这里的答案与 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
正如之前多次提到的,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,这样它就会正确调整下载大小。下面是一些 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 问题,所以这里的答案与 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