其他元素过宽时,在水平UIStackview中看不到标签

时间:2019-11-21 06:25:29

标签: ios swift uistackview

我有一个水平的UIStackView。其中包含以下元素;

UILabel - UILabel (lets call it second label)

第二个标签具有adjustsFontSizeToFitWidth = true属性和最小比例因子。当它的文本太长时,它会缩小到一些最小尺寸。第一个标签具有firstLabel.setContentCompressionResistancePriority(.init(rawValue: 749), for: .horizontal)属性以抵制第二个标签。在这种情况下,一切正常。

此UIStackView正在以编程方式创建,有时还会再获得三个元素

UILabel - UIImageView - UIView(dummy) - UIImageView - UILabel

UIImageViews具有12个常数点的特定widthAnchor。 问题是:在这种情况下,labelWithInfoAndDownArrow,如果第一个UILabel包含长文本,则第二个UILabel不会显示在屏幕上。我该怎么解决?

  override init(frame: CGRect) {
    super.init(frame: frame)

    self.translatesAutoresizingMaskIntoConstraints = false

    firstLabel.numberOfLines = 1
    firstLabel.font = UIFont.init(name: "Metropolis-Regular", size: 12)
    firstLabel.setContentCompressionResistancePriority(.init(rawValue: 249), for: .horizontal)
    secondLabel.numberOfLines = 1
    secondLabel.textAlignment = .right
    secondLabel.font = UIFont.init(name: "Metropolis-ExtraBold", size: 14)

    secondLabel.adjustsFontSizeToFitWidth = true
    secondLabel.minimumScaleFactor = 10/14

    horizontalStackView.addArrangedSubview(firstLabel)
    horizontalStackView.addArrangedSubview(secondLabel)

    iconImageView.widthAnchor.constraint(equalToConstant: 12).isActive = true
    infoImageView.widthAnchor.constraint(equalToConstant: 15).isActive = true

    addSubview(horizontalStackView)
    horizontalStackView.topAnchor.constraint(equalTo: topAnchor).isActive = true
    horizontalStackView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
    horizontalStackView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
    horizontalStackView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true

}

convenience init(firstLabelText: String, secondLabelText: String) {
    self.init()
    firstLabel.text = firstLabelText
    secondLabel.text = secondLabelText
    accessibilityIdentifier = firstLabelText
}

convenience init(firstLabelText: String, secondLabelText: String,labelType: LabelType) {
    self.init(firstLabelText: firstLabelText, secondLabelText: secondLabelText)
    self.labelType = labelType

    switch labelType {
    case .labelWithInfoAndDownArrow:
        iconImageView.image = UIImage(named: "droparrow-down-icon-grey")
        horizontalStackView.insertArrangedSubview(infoImageView, at: 1)
        horizontalStackView.insertArrangedSubview(UIView(), at: 2)
        horizontalStackView.insertArrangedSubview(iconImageView, at: 4)
        let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleInfoButtonTap(_:)))
        infoImageView.isUserInteractionEnabled = true
        infoImageView.addGestureRecognizer(tap)
        infoImageView.accessibilityIdentifier = firstLabelText
        if #available(iOS 11.0, *) {
            horizontalStackView.setCustomSpacing(0, after: iconImageView)
            horizontalStackView.setCustomSpacing(0, after: firstLabel)
        } else {
            // Fallback on earlier versions
            horizontalStackView.spacing = 0.0
        }
    case .defaultLabel:
        break
    }

}

编辑,如果我将第一个标签压缩优先级设为749。第二种情况已解决,但它打破了第一种情况,即当第二个标签不适合屏幕时,第二个标签的字体变小。

1 个答案:

答案 0 :(得分:0)

您可以尝试添加:

        firstLabel.widthAnchor.constraint(greaterThanOrEqualToConstant: 120).isActive = true
        secondLabel.widthAnchor.constraint(greaterThanOrEqualToConstant: 120).isActive = true

将120替换为您想要的firstLabel和secondLabel的最小大小。

您可以将其与您的

结合使用
        firstLabel.setContentCompressionResistancePriority(.init(rawValue: 249), for: .horizontal)        

或者没有它取决于您期望的结果...

让我知道怎么回事:D