Merge branch 'bugfix/fix_left_right_label' into 'release/7_6_0'

Changes to improve Left Right Label

See merge request BPHV_MIPS/mvm_core_ui!393
This commit is contained in:
Pfeil, Scott Robert 2020-04-20 13:34:51 -04:00
commit d3dc669681

View File

@ -14,8 +14,8 @@ import Foundation
// MARK: - Outlets // MARK: - Outlets
//------------------------------------------------------ //------------------------------------------------------
let leftTextLabel = Label.commonLabelB1(true) let leftTextLabel = Label.createLabelBoldBodySmall(true)
let rightTextLabel = Label.commonLabelB1(true) let rightTextLabel = Label.createLabelBoldBodySmall(true)
//------------------------------------------------------ //------------------------------------------------------
// MARK: - Constraints // MARK: - Constraints
@ -23,6 +23,29 @@ import Foundation
var rightTextLabelLeading: NSLayoutConstraint? var rightTextLabelLeading: NSLayoutConstraint?
var leftTextLabelTrailing: NSLayoutConstraint? var leftTextLabelTrailing: NSLayoutConstraint?
var rightTextLabelTrailing: NSLayoutConstraint?
private func constrainLeftLabelOnly() {
deactivateMiddleConstraint()
leftTextLabelTrailing?.isActive = false
leftTextLabelTrailing = layoutMarginsGuide.trailingAnchor.constraint(equalTo: leftTextLabel.trailingAnchor)
leftTextLabelTrailing?.isActive = true
}
private func constrainRightLabelOnly() {
deactivateMiddleConstraint()
rightTextLabelLeading?.isActive = false
rightTextLabelLeading = rightTextLabel.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor)
rightTextLabelLeading?.isActive = true
}
private func deactivateMiddleConstraint() {
leftTextLabelTrailing?.isActive = false
rightTextLabelLeading?.isActive = false
}
//------------------------------------------------------ //------------------------------------------------------
// MARK: - Initialization // MARK: - Initialization
@ -43,8 +66,6 @@ import Foundation
override open func setupView() { override open func setupView() {
super.setupView() super.setupView()
guard subviews.isEmpty else { return }
addSubview(leftTextLabel) addSubview(leftTextLabel)
addSubview(rightTextLabel) addSubview(rightTextLabel)
@ -67,7 +88,7 @@ import Foundation
// Resolves text layout issues found between both dynamically sized labels, number is not exact but performs as required. // Resolves text layout issues found between both dynamically sized labels, number is not exact but performs as required.
if leftTextLabel.hasText && rightTextLabel.hasText { if leftTextLabel.hasText && rightTextLabel.hasText {
let padding = MFStyler.defaultHorizontalPadding(forSize: size) * 2 let padding = MFStyler.defaultHorizontalPadding(forSize: size) * 2
let maximumTextWidth = (size - (padding + 16)) * 0.4 let maximumTextWidth = (size - (padding + Padding.Four)) * 0.4
// Subtracting 10 resolves issues of SE and iPad // Subtracting 10 resolves issues of SE and iPad
rightTextLabel.preferredMaxLayoutWidth = round(maximumTextWidth) - 10 rightTextLabel.preferredMaxLayoutWidth = round(maximumTextWidth) - 10
} else { } else {
@ -84,57 +105,38 @@ import Foundation
leftTextLabel.topAnchor.constraint(equalTo: topAnchor).isActive = true leftTextLabel.topAnchor.constraint(equalTo: topAnchor).isActive = true
leftTextLabel.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor).isActive = true leftTextLabel.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor).isActive = true
let leftTextBottom = leftTextLabel.bottomAnchor.constraint(equalTo: bottomAnchor) let leftTextBottom = bottomAnchor.constraint(equalTo: leftTextLabel.bottomAnchor)
leftTextBottom.priority = UILayoutPriority(249) leftTextBottom.priority = UILayoutPriority(249)
leftTextBottom.isActive = true leftTextBottom.isActive = true
bottomAnchor.constraint(greaterThanOrEqualTo: leftTextLabel.bottomAnchor).isActive = true bottomAnchor.constraint(greaterThanOrEqualTo: leftTextLabel.bottomAnchor).isActive = true
rightTextLabelLeading = rightTextLabel.leadingAnchor.constraint(equalTo: leftTextLabel.trailingAnchor, constant: 16) rightTextLabelLeading = rightTextLabel.leadingAnchor.constraint(equalTo: leftTextLabel.trailingAnchor, constant: Padding.Four)
rightTextLabelLeading?.isActive = true rightTextLabelLeading?.isActive = true
rightTextLabel.topAnchor.constraint(equalTo: topAnchor).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) rightTextLabelTrailing = layoutMarginsGuide.trailingAnchor.constraint(equalTo: rightTextLabel.trailingAnchor)
rightTextLabelTrailing?.isActive = true
let rightTextBottom = bottomAnchor.constraint(equalTo: rightTextLabel.bottomAnchor)
rightTextBottom.priority = UILayoutPriority(rawValue: 249) rightTextBottom.priority = UILayoutPriority(rawValue: 249)
rightTextBottom.isActive = true rightTextBottom.isActive = true
bottomAnchor.constraint(greaterThanOrEqualTo: rightTextLabel.bottomAnchor).isActive = true bottomAnchor.constraint(greaterThanOrEqualTo: rightTextLabel.bottomAnchor).isActive = true
let leftTextWidth = leftTextLabel.widthAnchor.constraint(greaterThanOrEqualTo: layoutMarginsGuide.widthAnchor, multiplier: 0.6) let leftTextWidth = leftTextLabel.widthAnchor.constraint(equalTo: layoutMarginsGuide.widthAnchor, multiplier: 0.6)
leftTextWidth.priority = UILayoutPriority(rawValue: 995) leftTextWidth.priority = UILayoutPriority(rawValue: 995)
leftTextWidth.isActive = true leftTextWidth.isActive = true
let rightTextWidth = rightTextLabel.widthAnchor.constraint(lessThanOrEqualTo: layoutMarginsGuide.widthAnchor, multiplier: 0.4) let rightTextWidth = rightTextLabel.widthAnchor.constraint(equalTo: layoutMarginsGuide.widthAnchor, multiplier: 0.4)
rightTextWidth.priority = UILayoutPriority(rawValue: 906) rightTextWidth.priority = UILayoutPriority(rawValue: 995)
rightTextWidth.isActive = true rightTextWidth.isActive = true
leftTextLabel.setContentHuggingPriority(UILayoutPriority(rawValue: 901), for: .horizontal) rightTextLabel.setContentHuggingPriority(.required, for: .horizontal)
rightTextLabel.setContentHuggingPriority(UILayoutPriority(rawValue: 902), for: .horizontal)
leftTextLabel.setContentHuggingPriority(.required, for: .vertical)
rightTextLabel.setContentHuggingPriority(.required, for: .vertical)
leftTextLabel.setContentCompressionResistancePriority(.required, for: .vertical) leftTextLabel.setContentCompressionResistancePriority(.required, for: .vertical)
rightTextLabel.setContentCompressionResistancePriority(.required, for: .vertical) rightTextLabel.setContentCompressionResistancePriority(.required, for: .vertical)
rightTextLabel.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 902), for: .horizontal) rightTextLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
}
private func constrainLeftLabel() {
deactivateMiddleConstraint()
leftTextLabelTrailing = layoutMarginsGuide.trailingAnchor.constraint(equalTo: leftTextLabel.trailingAnchor)
leftTextLabelTrailing?.isActive = true
}
private func constrainRightLabel() {
deactivateMiddleConstraint()
rightTextLabelLeading = rightTextLabel.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor)
rightTextLabelLeading?.isActive = true
} }
override open func reset() { override open func reset() {
@ -144,31 +146,31 @@ import Foundation
constrainBothLabels() constrainBothLabels()
leftTextLabel.text = "" leftTextLabel.text = ""
rightTextLabel.text = "" rightTextLabel.text = ""
leftTextLabel.textAlignment = .left
rightTextLabel.textAlignment = .right
backgroundColor = nil backgroundColor = nil
} }
private func deactivateMiddleConstraint() {
leftTextLabelTrailing?.isActive = false
rightTextLabelLeading?.isActive = false
}
//------------------------------------------------------ //------------------------------------------------------
// MARK: - Atomization // MARK: - Atomization
//------------------------------------------------------ //------------------------------------------------------
open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) { open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
super.set(with: model, delegateObject, additionalData) super.set(with: model, delegateObject, additionalData)
guard let leftRightLabelModel = model as? LeftRightLabelModel else {
return guard let leftRightLabelModel = model as? LeftRightLabelModel else { return }
}
leftTextLabel.set(with: leftRightLabelModel.leftText, delegateObject, additionalData) leftTextLabel.set(with: leftRightLabelModel.leftText, delegateObject, additionalData)
rightTextLabel.setOptional(with: leftRightLabelModel.rightText, delegateObject, additionalData) rightTextLabel.setOptional(with: leftRightLabelModel.rightText, delegateObject, additionalData)
// Label defaults to .left if no textAlignment value is provided in model.
rightTextLabel.textAlignment = leftRightLabelModel.rightText?.textAlignment ?? .right
if !leftTextLabel.hasText { if !leftTextLabel.hasText {
constrainRightLabel() constrainRightLabelOnly()
} else if !rightTextLabel.hasText { } else if !rightTextLabel.hasText {
constrainLeftLabel() constrainLeftLabelOnly()
} }
} }
} }