简体   繁体   English

两个标签彼此相邻,截断一个

[英]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.

相关问题 NSLayoutConstraint和“附加”两个标签相互 - NSLayoutConstraint and “attach” two labels to each other UIStackView动态地彼此相邻添加2个标签,没有多余的间距 - UIStackView dynamcially add 2 labels next to each other with no extra spacing 在UICollectionView中将两个部分放在一起 - Placing two sections next to each other in a UICollectionView Flutter:在彼此相邻的两列中显示内容 - Flutter: Display content in two columns next to each other iOS-约束-UITableViewCell中彼此相邻的两个视图 - iOS - Constraints - Two Views next to each other in UITableViewCell 如何格式化右侧相邻的两个 UIBarButtonItem? - How to format two UIBarButtonItems next to each other on the right? 三个Uiview彼此相邻,相等的宽度,使用全屏宽度,并且在移除一个后,其他两个会获得全屏宽度吗? - Three Uiview next to each other ,equal width, use full screen-width and after remove one the other two gets full screen-width? 带有pickerView的每行两个标签 - two labels for each row with pickerView 如何在一个窗口中显示两个控制器并互相交谈 - How to display two controllers in one window and talk to each other 在一个函数中以编程方式呈现两个UIViewControllers - Present two UIViewControllers on top of each other programmatically in one function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM