Added logic for added control over label constraints.

This commit is contained in:
Christiano, Kevin 2019-05-31 12:33:56 -04:00
parent c884b97fff
commit c4baffd679
2 changed files with 100 additions and 61 deletions

View File

@ -9,13 +9,19 @@
import Foundation
@available(*, unavailable)
@objcMembers open class LeftRightLabelView: ViewConstrainingView {
//------------------------------------------------------
// MARK: - Properties
//------------------------------------------------------
var leftText: Label?
var rightText: Label?
var leftTextLabel: Label?
var rightTextLabel: Label?
var leftTextWidth: NSLayoutConstraint?
var rightTextWidth: NSLayoutConstraint?
var leftTextLabelTrailing: NSLayoutConstraint?
//------------------------------------------------------
// MARK: - Initialization
@ -35,7 +41,7 @@ import Foundation
public convenience init(actionMap: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) {
self.init()
setWithJSON(actionMap: actionMap, delegateObject: delegateObject, additionalData: additionalData)
setWithJSON(actionMap, delegateObject: delegateObject, additionalData: additionalData)
}
override open func setupView() {
@ -51,11 +57,10 @@ import Foundation
override open func updateView(_ size: CGFloat) {
super.updateView(size)
leftText?.updateView(size)
rightText?.updateView(size)
leftTextLabel?.updateView(size)
rightTextLabel?.updateView(size)
layoutIfNeeded()
layoutSubviews()
}
//------------------------------------------------------
@ -64,69 +69,88 @@ import Foundation
func defaultState() {
if leftText == nil && rightText == nil {
if leftTextLabel == nil && rightTextLabel == nil {
let leftText = Label.commonLabelB1(true)
let rightText = Label.commonLabelB1(true)
let leftTextLabel = Label.commonLabelB1(true)
let rightTextLabel = Label.commonLabelB1(true)
self.leftText = leftText
self.rightText = rightText
self.leftTextLabel = leftTextLabel
self.rightTextLabel = rightTextLabel
addSubview(leftText)
addSubview(rightText)
addSubview(leftTextLabel)
addSubview(rightTextLabel)
leftText.textAlignment = .left
rightText.textAlignment = .right
leftTextLabel.textAlignment = .left
rightTextLabel.textAlignment = .right
// leftText.topAnchor.constraint(equalTo: topAnchor).isActive = true
// leftText.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
leftText.trailingAnchor.constraint(equalTo: rightText.leadingAnchor).isActive = true
NSLayoutConstraint.constraintPinSubview(leftText, pinTop: true, pinBottom: false, pinLeft: true, pinRight: false)
let leftTextBottom = leftText.bottomAnchor.constraint(equalTo: bottomAnchor)
leftTextBottom.priority = UILayoutPriority(249)
leftTextBottom.isActive = true
bottomAnchor.constraint(greaterThanOrEqualTo: leftText.bottomAnchor).isActive = true
// rightText.topAnchor.constraint(equalTo: topAnchor).isActive = true
// rightText.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
NSLayoutConstraint.constraintPinSubview(rightText, pinTop: true, pinBottom: false, pinLeft: false, pinRight: true)
let rightTextBottom = rightText.bottomAnchor.constraint(equalTo: bottomAnchor)
rightTextBottom.priority = UILayoutPriority(rawValue: 249)
rightTextBottom.isActive = true
bottomAnchor.constraint(greaterThanOrEqualTo: rightText.bottomAnchor).isActive = true
let leftTextWidth = leftText.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 0.7)
leftTextWidth.priority = UILayoutPriority(rawValue: 100)
leftTextWidth.isActive = true
// let rightTextWidth = rightText.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 0.3)
// rightTextWidth.priority = UILayoutPriority(rawValue: 100)
// rightTextWidth.isActive = true
rightText.firstBaselineAnchor.constraint(equalTo: leftText.firstBaselineAnchor).isActive = true
leftText.setContentHuggingPriority(UILayoutPriority(rawValue: 801), for: .horizontal)
// leftText.setContentHuggingPriority(.required, for: .vertical)
rightText.setContentHuggingPriority(UILayoutPriority(rawValue: 802), for: .horizontal)
// rightText.setContentHuggingPriority(.required, for: .vertical)
constrainBothLabels()
}
}
func setWithJSON(actionMap: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) {
private func constrainBothLabels() {
guard let dictionary = actionMap else { return }
leftTextLabel?.topAnchor.constraint(equalTo: topAnchor).isActive = true
leftTextLabel?.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
leftText?.setWithJSON(dictionary.optionalDictionaryForKey("leftText"), delegateObject: delegateObject as? MVMCoreUIDelegateObject, additionalData: additionalData)
rightText?.setWithJSON(dictionary.optionalDictionaryForKey("rightText"), delegateObject: delegateObject as? MVMCoreUIDelegateObject, additionalData: additionalData)
if let backgroundColorHex = dictionary[KeyBackgroundColor] as? String {
backgroundColor = UIColor.mfGet(forHex: backgroundColorHex)
if let rightLabelLeadingAnchor = rightTextLabel?.leadingAnchor {
leftTextLabelTrailing = leftTextLabel?.trailingAnchor.constraint(equalTo: rightLabelLeadingAnchor, constant: -PaddingOne)
leftTextLabelTrailing?.isActive = true
}
NSLayoutConstraint.constraintPinSubview(leftTextLabel, pinTop: true, pinBottom: false, pinLeft: true, pinRight: false)
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
}
rightTextLabel?.topAnchor.constraint(equalTo: topAnchor).isActive = true
rightTextLabel?.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
NSLayoutConstraint.constraintPinSubview(rightTextLabel, pinTop: true, pinBottom: false, pinLeft: false, pinRight: 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
}
leftTextWidth = leftTextLabel?.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 0.6)
leftTextWidth?.priority = UILayoutPriority(rawValue: 100)
leftTextWidth?.isActive = true
rightTextWidth = rightTextLabel?.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 0.4)
rightTextWidth?.priority = UILayoutPriority(rawValue: 101)
rightTextWidth?.isActive = true
if let leftLabelBaslineAnchor = leftTextLabel?.firstBaselineAnchor {
rightTextLabel?.firstBaselineAnchor.constraint(equalTo: leftLabelBaslineAnchor).isActive = true
}
leftTextLabel?.setContentHuggingPriority(UILayoutPriority(rawValue: 801), for: .horizontal)
rightTextLabel?.setContentHuggingPriority(UILayoutPriority(rawValue: 802), for: .horizontal)
}
private func constrainLeftLabel() {
leftTextLabelTrailing?.constant = 0
leftTextWidth?.isActive = false
leftTextWidth = leftTextLabel?.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 1.0)
leftTextWidth?.priority = UILayoutPriority(rawValue: 999)
leftTextWidth?.isActive = true
}
private func constrainRightLabel() {
leftTextLabelTrailing?.constant = 0
rightTextWidth?.isActive = false
rightTextWidth = rightTextLabel?.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 1.0)
rightTextWidth?.priority = UILayoutPriority(rawValue: 999)
rightTextWidth?.isActive = true
}
//------------------------------------------------------
@ -140,7 +164,23 @@ import Foundation
open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) {
super.setWithJSON(json, delegateObject: delegateObject as? MVMCoreUIDelegateObject, additionalData: additionalData)
setWithJSON(actionMap: json, delegateObject: delegateObject, 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)
if let backgroundColorHex = dictionary[KeyBackgroundColor] as? String {
backgroundColor = UIColor.mfGet(forHex: backgroundColorHex)
}
if let leftText = leftTextLabel?.text, leftText.isEmpty {
constrainRightLabel()
} else if let rightText = rightTextLabel?.text, rightText.isEmpty {
constrainLeftLabel()
} else {
constrainBothLabels()
}
}
open override func needsToBeConstrained() -> Bool {

View File

@ -37,8 +37,7 @@
@"checkbox": MVMCoreUICheckBox.class,
@"listItem": MoleculeTableViewCell.class,
@"switchLineItem": SwitchLineItem.class,
@"switch": Switch.class,
@"leftRightLabelView": LeftRightLabelView.class
@"switch": Switch.class
} mutableCopy];
});
return mapping;