Latest state of LeftRightLabel. Requires fine tuning.
This commit is contained in:
parent
83446c3e9b
commit
e8ab746351
@ -11,16 +11,28 @@ import Foundation
|
|||||||
|
|
||||||
@objcMembers open class LeftRightLabelView: ViewConstrainingView {
|
@objcMembers open class LeftRightLabelView: ViewConstrainingView {
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
// MARK: - Properties
|
// MARK: - Outlets
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
|
|
||||||
var leftTextLabel: Label?
|
let leftTextLabel = Label.commonLabelB1(true)
|
||||||
var rightTextLabel: Label?
|
let rightTextLabel = Label.commonLabelB1(true)
|
||||||
|
|
||||||
|
//------------------------------------------------------
|
||||||
|
// MARK: - Constraints
|
||||||
|
//------------------------------------------------------
|
||||||
|
|
||||||
var leftTextWidth: NSLayoutConstraint?
|
var leftTextWidth: NSLayoutConstraint?
|
||||||
var rightTextWidth: NSLayoutConstraint?
|
var rightTextWidth: NSLayoutConstraint?
|
||||||
|
|
||||||
var leftTextLabelTrailing: NSLayoutConstraint?
|
var rightTextLabelLeading: NSLayoutConstraint?
|
||||||
|
|
||||||
|
//------------------------------------------------------
|
||||||
|
// MARK: - Properties
|
||||||
|
//------------------------------------------------------
|
||||||
|
|
||||||
|
var leftRightMarginsValue: CGFloat {
|
||||||
|
return layoutMargins.left + layoutMargins.right
|
||||||
|
}
|
||||||
|
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
// MARK: - Initialization
|
// MARK: - Initialization
|
||||||
@ -38,15 +50,23 @@ import Foundation
|
|||||||
super.init(coder: aDecoder)
|
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()
|
self.init()
|
||||||
setWithJSON(actionMap, delegateObject: delegateObject, additionalData: additionalData)
|
setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData)
|
||||||
}
|
}
|
||||||
|
|
||||||
override open func setupView() {
|
override open func setupView() {
|
||||||
super.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) {
|
override open func updateView(_ size: CGFloat) {
|
||||||
super.updateView(size)
|
super.updateView(size)
|
||||||
|
|
||||||
leftTextLabel?.updateView(size)
|
leftTextLabel.updateView(size)
|
||||||
rightTextLabel?.updateView(size)
|
rightTextLabel.updateView(size)
|
||||||
|
|
||||||
if let leftText = leftTextLabel?.text, let rightText = rightTextLabel?.text, !leftText.isEmpty && !rightText.isEmpty {
|
if leftTextLabel.hasText && rightTextLabel.hasText {
|
||||||
rightTextLabel?.preferredMaxLayoutWidth = size * 0.4
|
rightTextLabel.preferredMaxLayoutWidth = (size - leftRightMarginsValue) * 0.4
|
||||||
}
|
}
|
||||||
|
|
||||||
layoutIfNeeded()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
// MARK: - Setup
|
// 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() {
|
private func constrainBothLabels() {
|
||||||
|
|
||||||
leftTextLabel?.topAnchor.constraint(equalTo: topAnchor).isActive = true
|
leftTextLabel.topAnchor.constraint(equalTo: topAnchor).isActive = true
|
||||||
leftTextLabel?.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
|
leftTextLabel.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor).isActive = true
|
||||||
|
|
||||||
if let rightLabelLeadingAnchor = rightTextLabel?.leadingAnchor {
|
rightTextLabelLeading = rightTextLabel.leadingAnchor.constraint(equalTo: leftTextLabel.trailingAnchor, constant: 16)
|
||||||
leftTextLabelTrailing = leftTextLabel?.trailingAnchor.constraint(equalTo: rightLabelLeadingAnchor, constant: -16)
|
rightTextLabelLeading?.isActive = true
|
||||||
leftTextLabelTrailing?.isActive = true
|
|
||||||
}
|
|
||||||
|
|
||||||
let leftTextBottom = leftTextLabel?.bottomAnchor.constraint(equalTo: bottomAnchor)
|
let leftTextBottom = leftTextLabel.bottomAnchor.constraint(equalTo: bottomAnchor)
|
||||||
leftTextBottom?.priority = UILayoutPriority(249)
|
leftTextBottom.priority = UILayoutPriority(249)
|
||||||
leftTextBottom?.isActive = true
|
leftTextBottom.isActive = true
|
||||||
|
|
||||||
if let leftLabelBottomAnchor = leftTextLabel?.bottomAnchor {
|
bottomAnchor.constraint(greaterThanOrEqualTo: leftTextLabel.bottomAnchor).isActive = true
|
||||||
bottomAnchor.constraint(greaterThanOrEqualTo: leftLabelBottomAnchor).isActive = true
|
|
||||||
}
|
|
||||||
|
|
||||||
rightTextLabel?.topAnchor.constraint(equalTo: topAnchor).isActive = true
|
rightTextLabel.topAnchor.constraint(equalTo: topAnchor).isActive = true
|
||||||
rightTextLabel?.trailingAnchor.constraint(equalTo: trailingAnchor).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)
|
let rightTextBottom = rightTextLabel.bottomAnchor.constraint(equalTo: bottomAnchor)
|
||||||
rightTextBottom?.priority = UILayoutPriority(rawValue: 249)
|
rightTextBottom.priority = UILayoutPriority(rawValue: 249)
|
||||||
rightTextBottom?.isActive = true
|
rightTextBottom.isActive = true
|
||||||
|
|
||||||
if let rightLabelBottomAnchor = rightTextLabel?.bottomAnchor {
|
bottomAnchor.constraint(greaterThanOrEqualTo: rightTextLabel.bottomAnchor).isActive = true
|
||||||
bottomAnchor.constraint(greaterThanOrEqualTo: rightLabelBottomAnchor).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?.priority = UILayoutPriority(rawValue: 995)
|
||||||
leftTextWidth?.isActive = true
|
leftTextWidth?.isActive = true
|
||||||
|
|
||||||
let leftTextHeight = leftTextLabel?.heightAnchor.constraint(greaterThanOrEqualToConstant: 0)
|
rightTextWidth = rightTextLabel.widthAnchor.constraint(lessThanOrEqualTo: widthAnchor, multiplier: 0.4, constant: -leftRightMarginsValue)
|
||||||
leftTextHeight?.priority = UILayoutPriority(rawValue: 901)
|
|
||||||
leftTextHeight?.isActive = true
|
|
||||||
|
|
||||||
rightTextWidth = rightTextLabel?.widthAnchor.constraint(lessThanOrEqualTo: widthAnchor, multiplier: 0.4)
|
|
||||||
rightTextWidth?.priority = UILayoutPriority(rawValue: 906)
|
rightTextWidth?.priority = UILayoutPriority(rawValue: 906)
|
||||||
rightTextWidth?.isActive = true
|
rightTextWidth?.isActive = true
|
||||||
|
|
||||||
let rightTextHeight = rightTextLabel?.heightAnchor.constraint(greaterThanOrEqualToConstant: 0)
|
leftTextLabel.setContentHuggingPriority(UILayoutPriority(rawValue: 900), for: .horizontal)
|
||||||
rightTextHeight?.priority = UILayoutPriority(rawValue: 901)
|
rightTextLabel.setContentHuggingPriority(UILayoutPriority(rawValue: 901), for: .horizontal)
|
||||||
rightTextHeight?.isActive = true
|
|
||||||
|
|
||||||
if let leftLabelBaslineAnchor = leftTextLabel?.firstBaselineAnchor {
|
leftTextLabel.setContentHuggingPriority(.required, for: .vertical)
|
||||||
rightTextLabel?.firstBaselineAnchor.constraint(equalTo: leftLabelBaslineAnchor).isActive = true
|
rightTextLabel.setContentHuggingPriority(.required, for: .vertical)
|
||||||
}
|
|
||||||
|
|
||||||
leftTextLabel?.setContentHuggingPriority(UILayoutPriority(rawValue: 901), for: .horizontal)
|
leftTextLabel.setContentCompressionResistancePriority(.required, for: .vertical)
|
||||||
rightTextLabel?.setContentHuggingPriority(UILayoutPriority(rawValue: 902), for: .horizontal)
|
rightTextLabel.setContentCompressionResistancePriority(.required, for: .vertical)
|
||||||
|
rightTextLabel.setContentCompressionResistancePriority(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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private func constrainLeftLabel() {
|
private func constrainLeftLabel() {
|
||||||
|
|
||||||
leftTextLabelTrailing?.constant = 0
|
rightTextLabelLeading?.isActive = false
|
||||||
leftTextWidth = leftTextLabel?.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 1.0)
|
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?.priority = UILayoutPriority(rawValue: 999)
|
||||||
leftTextWidth?.isActive = true
|
leftTextWidth?.isActive = true
|
||||||
|
|
||||||
leftTextLabel?.setContentHuggingPriority(.required, for: .vertical)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private func constrainRightLabel() {
|
private func constrainRightLabel() {
|
||||||
|
|
||||||
leftTextLabelTrailing?.constant = 0
|
rightTextLabelLeading?.isActive = false
|
||||||
rightTextWidth = rightTextLabel?.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 1.0)
|
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?.priority = UILayoutPriority(rawValue: 999)
|
||||||
rightTextWidth?.isActive = true
|
rightTextWidth?.isActive = true
|
||||||
|
}
|
||||||
|
|
||||||
|
override open func resetConstraints() {
|
||||||
|
super.resetConstraints()
|
||||||
|
|
||||||
rightTextLabel?.setContentHuggingPriority(.required, for: .vertical)
|
leftTextWidth?.isActive = false
|
||||||
|
rightTextWidth?.isActive = false
|
||||||
|
constrainBothLabels()
|
||||||
}
|
}
|
||||||
|
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
// MARK: - Atomization
|
// MARK: - Atomization
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
|
|
||||||
open override func setAsMolecule() {
|
|
||||||
super.setAsMolecule()
|
|
||||||
defaultState()
|
|
||||||
}
|
|
||||||
|
|
||||||
open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) {
|
open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) {
|
||||||
super.setWithJSON(json, delegateObject: delegateObject as? MVMCoreUIDelegateObject, additionalData: additionalData)
|
super.setWithJSON(json, delegateObject: delegateObject as? MVMCoreUIDelegateObject, additionalData: additionalData)
|
||||||
|
|
||||||
guard let dictionary = json else { return }
|
guard let dictionary = json else { return }
|
||||||
|
|
||||||
leftTextLabel?.setWithJSON(dictionary.optionalDictionaryForKey("leftText"), 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)
|
rightTextLabel.setWithJSON(dictionary.optionalDictionaryForKey("rightText"), delegateObject: delegateObject as? MVMCoreUIDelegateObject, additionalData: additionalData)
|
||||||
|
|
||||||
if let backgroundColorHex = dictionary[KeyBackgroundColor] as? String {
|
if let backgroundColorHex = dictionary[KeyBackgroundColor] as? String {
|
||||||
backgroundColor = UIColor.mfGet(forHex: backgroundColorHex)
|
backgroundColor = UIColor.mfGet(forHex: backgroundColorHex)
|
||||||
}
|
}
|
||||||
|
|
||||||
if let leftText = leftTextLabel?.text, leftText.isEmpty {
|
if !leftTextLabel.hasText {
|
||||||
constrainRightLabel()
|
constrainRightLabel()
|
||||||
} else if let rightText = rightTextLabel?.text, rightText.isEmpty {
|
} else if !rightTextLabel.hasText {
|
||||||
constrainLeftLabel()
|
constrainLeftLabel()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user