diff --git a/MVMCoreUI/Atoms/Views/LeftRightLabelView.swift b/MVMCoreUI/Atoms/Views/LeftRightLabelView.swift index fb924453..c415ab03 100644 --- a/MVMCoreUI/Atoms/Views/LeftRightLabelView.swift +++ b/MVMCoreUI/Atoms/Views/LeftRightLabelView.swift @@ -11,16 +11,28 @@ import Foundation @objcMembers open class LeftRightLabelView: ViewConstrainingView { //------------------------------------------------------ - // MARK: - Properties + // MARK: - Outlets //------------------------------------------------------ - var leftTextLabel: Label? - var rightTextLabel: Label? + let leftTextLabel = Label.commonLabelB1(true) + let rightTextLabel = Label.commonLabelB1(true) + + //------------------------------------------------------ + // MARK: - Constraints + //------------------------------------------------------ var leftTextWidth: NSLayoutConstraint? var rightTextWidth: NSLayoutConstraint? - var leftTextLabelTrailing: NSLayoutConstraint? + var rightTextLabelLeading: NSLayoutConstraint? + + //------------------------------------------------------ + // MARK: - Properties + //------------------------------------------------------ + + var leftRightMarginsValue: CGFloat { + return layoutMargins.left + layoutMargins.right + } //------------------------------------------------------ // MARK: - Initialization @@ -38,15 +50,23 @@ import Foundation super.init(coder: aDecoder) } - public convenience init(actionMap: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) { + public convenience init(json: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) { self.init() - setWithJSON(actionMap, delegateObject: delegateObject, additionalData: additionalData) + setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData) } override open func setupView() { super.setupView() - translatesAutoresizingMaskIntoConstraints = false - defaultState() + + guard subviews.isEmpty else { return } + + addSubview(leftTextLabel) + addSubview(rightTextLabel) + + leftTextLabel.textAlignment = .left + rightTextLabel.textAlignment = .right + + constrainBothLabels() } //------------------------------------------------------ @@ -56,147 +76,110 @@ import Foundation override open func updateView(_ size: CGFloat) { super.updateView(size) - leftTextLabel?.updateView(size) - rightTextLabel?.updateView(size) + leftTextLabel.updateView(size) + rightTextLabel.updateView(size) - if let leftText = leftTextLabel?.text, let rightText = rightTextLabel?.text, !leftText.isEmpty && !rightText.isEmpty { - rightTextLabel?.preferredMaxLayoutWidth = size * 0.4 + if leftTextLabel.hasText && rightTextLabel.hasText { + rightTextLabel.preferredMaxLayoutWidth = (size - leftRightMarginsValue) * 0.4 } - - layoutIfNeeded() } //------------------------------------------------------ // MARK: - Setup //------------------------------------------------------ - func defaultState() { - - if leftTextLabel == nil && rightTextLabel == nil { - - let leftTextLabel = Label.commonLabelB1(true) - let rightTextLabel = Label.commonLabelB1(true) - - leftTextLabel.translatesAutoresizingMaskIntoConstraints = false - rightTextLabel.translatesAutoresizingMaskIntoConstraints = false - - self.leftTextLabel = leftTextLabel - self.rightTextLabel = rightTextLabel - - addSubview(leftTextLabel) - addSubview(rightTextLabel) - - leftTextLabel.textAlignment = .left - rightTextLabel.textAlignment = .right - - constrainBothLabels() - } - } - private func constrainBothLabels() { - leftTextLabel?.topAnchor.constraint(equalTo: topAnchor).isActive = true - leftTextLabel?.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true + leftTextLabel.topAnchor.constraint(equalTo: topAnchor).isActive = true + leftTextLabel.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor).isActive = true - if let rightLabelLeadingAnchor = rightTextLabel?.leadingAnchor { - leftTextLabelTrailing = leftTextLabel?.trailingAnchor.constraint(equalTo: rightLabelLeadingAnchor, constant: -16) - leftTextLabelTrailing?.isActive = true - } + rightTextLabelLeading = rightTextLabel.leadingAnchor.constraint(equalTo: leftTextLabel.trailingAnchor, constant: 16) + rightTextLabelLeading?.isActive = true - let leftTextBottom = leftTextLabel?.bottomAnchor.constraint(equalTo: bottomAnchor) - leftTextBottom?.priority = UILayoutPriority(249) - leftTextBottom?.isActive = true + let leftTextBottom = leftTextLabel.bottomAnchor.constraint(equalTo: bottomAnchor) + leftTextBottom.priority = UILayoutPriority(249) + leftTextBottom.isActive = true - if let leftLabelBottomAnchor = leftTextLabel?.bottomAnchor { - bottomAnchor.constraint(greaterThanOrEqualTo: leftLabelBottomAnchor).isActive = true - } + bottomAnchor.constraint(greaterThanOrEqualTo: leftTextLabel.bottomAnchor).isActive = true - rightTextLabel?.topAnchor.constraint(equalTo: topAnchor).isActive = true - rightTextLabel?.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true + rightTextLabel.topAnchor.constraint(equalTo: topAnchor).isActive = true + let rightLayout = layoutMarginsGuide.trailingAnchor.constraint(equalTo: rightTextLabel.trailingAnchor) + rightLayout.priority = UILayoutPriority(rawValue: 995) + rightLayout.isActive = true - let rightTextBottom = rightTextLabel?.bottomAnchor.constraint(equalTo: bottomAnchor) - rightTextBottom?.priority = UILayoutPriority(rawValue: 249) - rightTextBottom?.isActive = true + let rightTextBottom = rightTextLabel.bottomAnchor.constraint(equalTo: bottomAnchor) + rightTextBottom.priority = UILayoutPriority(rawValue: 249) + rightTextBottom.isActive = true - if let rightLabelBottomAnchor = rightTextLabel?.bottomAnchor { - bottomAnchor.constraint(greaterThanOrEqualTo: rightLabelBottomAnchor).isActive = true - } + bottomAnchor.constraint(greaterThanOrEqualTo: rightTextLabel.bottomAnchor).isActive = true - leftTextWidth = leftTextLabel?.widthAnchor.constraint(greaterThanOrEqualTo: widthAnchor, multiplier: 0.6) + leftTextWidth = leftTextLabel.widthAnchor.constraint(greaterThanOrEqualTo: widthAnchor, multiplier: 0.6, constant: -leftRightMarginsValue) leftTextWidth?.priority = UILayoutPriority(rawValue: 995) leftTextWidth?.isActive = true - let leftTextHeight = leftTextLabel?.heightAnchor.constraint(greaterThanOrEqualToConstant: 0) - leftTextHeight?.priority = UILayoutPriority(rawValue: 901) - leftTextHeight?.isActive = true - - rightTextWidth = rightTextLabel?.widthAnchor.constraint(lessThanOrEqualTo: widthAnchor, multiplier: 0.4) + rightTextWidth = rightTextLabel.widthAnchor.constraint(lessThanOrEqualTo: widthAnchor, multiplier: 0.4, constant: -leftRightMarginsValue) rightTextWidth?.priority = UILayoutPriority(rawValue: 906) rightTextWidth?.isActive = true - let rightTextHeight = rightTextLabel?.heightAnchor.constraint(greaterThanOrEqualToConstant: 0) - rightTextHeight?.priority = UILayoutPriority(rawValue: 901) - rightTextHeight?.isActive = true + leftTextLabel.setContentHuggingPriority(UILayoutPriority(rawValue: 900), for: .horizontal) + rightTextLabel.setContentHuggingPriority(UILayoutPriority(rawValue: 901), for: .horizontal) - if let leftLabelBaslineAnchor = leftTextLabel?.firstBaselineAnchor { - rightTextLabel?.firstBaselineAnchor.constraint(equalTo: leftLabelBaslineAnchor).isActive = true - } + leftTextLabel.setContentHuggingPriority(.required, for: .vertical) + rightTextLabel.setContentHuggingPriority(.required, for: .vertical) - leftTextLabel?.setContentHuggingPriority(UILayoutPriority(rawValue: 901), for: .horizontal) - rightTextLabel?.setContentHuggingPriority(UILayoutPriority(rawValue: 902), for: .horizontal) - - leftTextLabel?.setContentHuggingPriority(.required, for: .vertical) - rightTextLabel?.setContentHuggingPriority(.required, for: .vertical) - - leftTextLabel?.setContentCompressionResistancePriority(.required, for: .vertical) - rightTextLabel?.setContentCompressionResistancePriority(.required, for: .vertical) - rightTextLabel?.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 902), for: .horizontal) + leftTextLabel.setContentCompressionResistancePriority(.required, for: .vertical) + rightTextLabel.setContentCompressionResistancePriority(.required, for: .vertical) + rightTextLabel.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 902), for: .horizontal) } private func constrainLeftLabel() { - leftTextLabelTrailing?.constant = 0 - leftTextWidth = leftTextLabel?.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 1.0) + rightTextLabelLeading?.isActive = false + layoutMarginsGuide.trailingAnchor.constraint(equalTo: leftTextLabel.trailingAnchor).isActive = true + leftTextWidth?.isActive = false + leftTextWidth = leftTextLabel.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 1.0, constant: -leftRightMarginsValue) leftTextWidth?.priority = UILayoutPriority(rawValue: 999) leftTextWidth?.isActive = true - - leftTextLabel?.setContentHuggingPriority(.required, for: .vertical) } private func constrainRightLabel() { - leftTextLabelTrailing?.constant = 0 - rightTextWidth = rightTextLabel?.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 1.0) + rightTextLabelLeading?.isActive = false + rightTextLabel.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor).isActive = true + layoutMarginsGuide.leadingAnchor.constraint(equalTo: rightTextLabel.leadingAnchor).isActive = true + rightTextWidth?.isActive = false + rightTextWidth = rightTextLabel.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 1.0, constant: -leftRightMarginsValue) rightTextWidth?.priority = UILayoutPriority(rawValue: 999) rightTextWidth?.isActive = true + } + + override open func resetConstraints() { + super.resetConstraints() - rightTextLabel?.setContentHuggingPriority(.required, for: .vertical) + leftTextWidth?.isActive = false + rightTextWidth?.isActive = false + constrainBothLabels() } //------------------------------------------------------ // MARK: - Atomization //------------------------------------------------------ - open override func setAsMolecule() { - super.setAsMolecule() - defaultState() - } - open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) { super.setWithJSON(json, delegateObject: delegateObject as? MVMCoreUIDelegateObject, additionalData: additionalData) guard let dictionary = json else { return } - leftTextLabel?.setWithJSON(dictionary.optionalDictionaryForKey("leftText"), delegateObject: delegateObject as? MVMCoreUIDelegateObject, additionalData: additionalData) - rightTextLabel?.setWithJSON(dictionary.optionalDictionaryForKey("rightText"), delegateObject: delegateObject as? MVMCoreUIDelegateObject, additionalData: additionalData) + leftTextLabel.setWithJSON(dictionary.optionalDictionaryForKey("leftText"), delegateObject: delegateObject as? MVMCoreUIDelegateObject, additionalData: additionalData) + rightTextLabel.setWithJSON(dictionary.optionalDictionaryForKey("rightText"), delegateObject: delegateObject as? MVMCoreUIDelegateObject, additionalData: additionalData) if let backgroundColorHex = dictionary[KeyBackgroundColor] as? String { backgroundColor = UIColor.mfGet(forHex: backgroundColorHex) } - if let leftText = leftTextLabel?.text, leftText.isEmpty { + if !leftTextLabel.hasText { constrainRightLabel() - } else if let rightText = rightTextLabel?.text, rightText.isEmpty { + } else if !rightTextLabel.hasText { constrainLeftLabel() } }