Added logic for added control over label constraints.
This commit is contained in:
parent
c884b97fff
commit
c4baffd679
@ -9,13 +9,19 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
|
|
||||||
|
@available(*, unavailable)
|
||||||
@objcMembers open class LeftRightLabelView: ViewConstrainingView {
|
@objcMembers open class LeftRightLabelView: ViewConstrainingView {
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
// MARK: - Properties
|
// MARK: - Properties
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
|
|
||||||
var leftText: Label?
|
var leftTextLabel: Label?
|
||||||
var rightText: Label?
|
var rightTextLabel: Label?
|
||||||
|
|
||||||
|
var leftTextWidth: NSLayoutConstraint?
|
||||||
|
var rightTextWidth: NSLayoutConstraint?
|
||||||
|
|
||||||
|
var leftTextLabelTrailing: NSLayoutConstraint?
|
||||||
|
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
// MARK: - Initialization
|
// MARK: - Initialization
|
||||||
@ -35,7 +41,7 @@ import Foundation
|
|||||||
|
|
||||||
public convenience init(actionMap: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) {
|
public convenience init(actionMap: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) {
|
||||||
self.init()
|
self.init()
|
||||||
setWithJSON(actionMap: actionMap, delegateObject: delegateObject, additionalData: additionalData)
|
setWithJSON(actionMap, delegateObject: delegateObject, additionalData: additionalData)
|
||||||
}
|
}
|
||||||
|
|
||||||
override open func setupView() {
|
override open func setupView() {
|
||||||
@ -51,11 +57,10 @@ import Foundation
|
|||||||
override open func updateView(_ size: CGFloat) {
|
override open func updateView(_ size: CGFloat) {
|
||||||
super.updateView(size)
|
super.updateView(size)
|
||||||
|
|
||||||
leftText?.updateView(size)
|
leftTextLabel?.updateView(size)
|
||||||
rightText?.updateView(size)
|
rightTextLabel?.updateView(size)
|
||||||
|
|
||||||
layoutIfNeeded()
|
layoutIfNeeded()
|
||||||
layoutSubviews()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
@ -64,69 +69,88 @@ import Foundation
|
|||||||
|
|
||||||
func defaultState() {
|
func defaultState() {
|
||||||
|
|
||||||
if leftText == nil && rightText == nil {
|
if leftTextLabel == nil && rightTextLabel == nil {
|
||||||
|
|
||||||
let leftText = Label.commonLabelB1(true)
|
let leftTextLabel = Label.commonLabelB1(true)
|
||||||
let rightText = Label.commonLabelB1(true)
|
let rightTextLabel = Label.commonLabelB1(true)
|
||||||
|
|
||||||
self.leftText = leftText
|
self.leftTextLabel = leftTextLabel
|
||||||
self.rightText = rightText
|
self.rightTextLabel = rightTextLabel
|
||||||
|
|
||||||
addSubview(leftText)
|
addSubview(leftTextLabel)
|
||||||
addSubview(rightText)
|
addSubview(rightTextLabel)
|
||||||
|
|
||||||
leftText.textAlignment = .left
|
leftTextLabel.textAlignment = .left
|
||||||
rightText.textAlignment = .right
|
rightTextLabel.textAlignment = .right
|
||||||
|
|
||||||
// leftText.topAnchor.constraint(equalTo: topAnchor).isActive = true
|
constrainBothLabels()
|
||||||
// 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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
if let rightLabelLeadingAnchor = rightTextLabel?.leadingAnchor {
|
||||||
rightText?.setWithJSON(dictionary.optionalDictionaryForKey("rightText"), delegateObject: delegateObject as? MVMCoreUIDelegateObject, additionalData: additionalData)
|
leftTextLabelTrailing = leftTextLabel?.trailingAnchor.constraint(equalTo: rightLabelLeadingAnchor, constant: -PaddingOne)
|
||||||
|
leftTextLabelTrailing?.isActive = true
|
||||||
if let backgroundColorHex = dictionary[KeyBackgroundColor] as? String {
|
|
||||||
backgroundColor = UIColor.mfGet(forHex: backgroundColorHex)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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]?) {
|
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)
|
||||||
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 {
|
open override func needsToBeConstrained() -> Bool {
|
||||||
|
|||||||
@ -37,8 +37,7 @@
|
|||||||
@"checkbox": MVMCoreUICheckBox.class,
|
@"checkbox": MVMCoreUICheckBox.class,
|
||||||
@"listItem": MoleculeTableViewCell.class,
|
@"listItem": MoleculeTableViewCell.class,
|
||||||
@"switchLineItem": SwitchLineItem.class,
|
@"switchLineItem": SwitchLineItem.class,
|
||||||
@"switch": Switch.class,
|
@"switch": Switch.class
|
||||||
@"leftRightLabelView": LeftRightLabelView.class
|
|
||||||
} mutableCopy];
|
} mutableCopy];
|
||||||
});
|
});
|
||||||
return mapping;
|
return mapping;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user