[英]Two labels next to each other, truncate one
I have two labels next to each other, but the right one truncates even when I set truncate rules on the left one. 我有两个彼此相邻的标签,但是即使我在左边的标签上设置了截断规则,右边的标签也会被截断。
My code: 我的代码:
// Club name
labelFirst = [[UILabel alloc] init];
labelFirst.adjustsFontSizeToFitWidth = NO;
[labelFirst setLineBreakMode:NSLineBreakByTruncatingTail];
[labelFirst setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.contentView addSubview:labelFirst];
labelSecond = [[UILabel alloc] init];
[labelSecond setAdjustsFontSizeToFitWidth:NO];
[labelSecond setTranslatesAutoresizingMaskIntoConstraints:NO];
[labelSecond setText:NSLocalizedString(@"IsCancelled", nil)];
[self.contentView addSubview:labelSecond];
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-73-[label]-[label2]-10-|" options:0 metrics:nil views:@{@"label": labelFirst, @"label2": labelSecond}]
[self.contentView addConstraints:constraint];
How should I do this? 我应该怎么做?
This is where Content Compression Resistance Priority comes into play. 这是内容压缩优先级发挥作用的地方。 Set these values higher and lower based on which one you want compressed first.
根据要首先压缩的值将这些值设置为较高和较低。 Higher for the label that you want take priority in resisting compression.
要在抗压缩方面优先考虑的标签较高。 Lower for the label that you want compressed (truncated) first.
降低要首先压缩(截断)的标签。
You can do it programmatically (default is 750): 您可以通过编程方式执行此操作(默认值为750):
[labelFirst setContentCompressionResistancePriority:749
forAxis:UILayoutConstraintAxisHorizontal];
Or in interface builder under the size inspector tab: 或在“大小检查器”选项卡下的界面构建器中:
Here is another answer going into more detail on how they work. 这是另一个答案,将详细介绍它们如何工作。
NSLineBreakByTruncatingTail-它是标签的默认LineBreakMode值,将其更改为labelSecond以具有区别
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.