ios - iOS 9 中 UILabel 中的多行文本

标签 ios xcode interface-builder ios9

我正在处理一个 iOS 项目,但是当我更新到 iOS 9 时,我在 UILabels 中遇到了多行问题。我正在使用 Autolayout

有人知道如何在 iOS 9 中做到这一点吗?

我尝试了不同的方式,例如:

textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.numberOfLines = 0;

(来自其他类似问题)但它没有用。

这是我显示多行的逻辑:

IB配置:

preferedMaxLayout

enter image description here

label config

我以编程方式更新此值:

- (void)configureLabelsMaxLayoutWidth
{
     [self.view layoutIfNeeded];
     self.titleLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.titleLabel.frame);
}

我在 viewWillAppear

上调用了这个方法

最佳答案

通过将 numberOfLines 设置为 0 您告诉标签是多行的,但是可能还有其他因素禁用多行。你在使用自动布局吗?如果是这样,问题可能是标签 Content Compression Resistance Priority 太低,请尝试将其设置为 Required1000

内容压缩阻力告诉 View 引擎您的标签可以按什么优先级压缩。将其设置为必需会强制其不收缩。

在 Interface Builder 中,只需选择标签,点击 Size Inspector(小尺子),然后将其更改为 1000。
enter image description here

或者,在代码中,等效的是:

[label setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];

关于ios - iOS 9 中 UILabel 中的多行文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32823714/

相关文章:

ios - 如何推送以编程方式创建的 UINavigationController 的新 View 宽度?

iphone - 可以使用导航栏返回到上一个 View 而不丢失 View 的数据吗?

ios - swift 3 [字符串 : AnyObject]() gives Cannot call value of non-function type UInt -> Data

ios - 无法在 Interface Builder 的单元格中移动内容 View

objective-c - 如何将 NSViewController 锚定为弹出框?

ios - 内存不足时性能低下 - Swift

ios - 如何在 IOS 中将表值存储到核心数据实体中

objective-c - 将 NSString 写入文件

ios - 在 UINavigationController 中使用 Storyboard引用

ios - 需要按钮多次重复相同的功能