Merge branch 'feature/kevin' into 'develop'
Label Multi-Action See merge request BPHV_MIPS/mvm_core_ui!62
This commit is contained in:
commit
cd577288c3
@ -167,6 +167,7 @@
|
|||||||
D2E1FADD2268B25E00AEFD8C /* MoleculeTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2E1FADC2268B25E00AEFD8C /* MoleculeTableViewCell.swift */; };
|
D2E1FADD2268B25E00AEFD8C /* MoleculeTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2E1FADC2268B25E00AEFD8C /* MoleculeTableViewCell.swift */; };
|
||||||
D2E1FADF2268B8E700AEFD8C /* ThreeLayerTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2E1FADE2268B8E700AEFD8C /* ThreeLayerTableViewController.swift */; };
|
D2E1FADF2268B8E700AEFD8C /* ThreeLayerTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2E1FADE2268B8E700AEFD8C /* ThreeLayerTableViewController.swift */; };
|
||||||
D2E1FAE12268E81D00AEFD8C /* MoleculeListTemplate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2E1FAE02268E81D00AEFD8C /* MoleculeListTemplate.swift */; };
|
D2E1FAE12268E81D00AEFD8C /* MoleculeListTemplate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2E1FAE02268E81D00AEFD8C /* MoleculeListTemplate.swift */; };
|
||||||
|
DB06250B2293456500B72DD3 /* LeftRightLabelView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DB06250A2293456500B72DD3 /* LeftRightLabelView.swift */; };
|
||||||
DBC4391822442197001AB423 /* CaretView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBC4391622442196001AB423 /* CaretView.swift */; };
|
DBC4391822442197001AB423 /* CaretView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBC4391622442196001AB423 /* CaretView.swift */; };
|
||||||
DBC4391922442197001AB423 /* DashLine.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBC4391722442197001AB423 /* DashLine.swift */; };
|
DBC4391922442197001AB423 /* DashLine.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBC4391722442197001AB423 /* DashLine.swift */; };
|
||||||
DBC4391B224421A0001AB423 /* CaretButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBC4391A224421A0001AB423 /* CaretButton.swift */; };
|
DBC4391B224421A0001AB423 /* CaretButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = DBC4391A224421A0001AB423 /* CaretButton.swift */; };
|
||||||
@ -338,6 +339,7 @@
|
|||||||
D2E1FADC2268B25E00AEFD8C /* MoleculeTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MoleculeTableViewCell.swift; sourceTree = "<group>"; };
|
D2E1FADC2268B25E00AEFD8C /* MoleculeTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MoleculeTableViewCell.swift; sourceTree = "<group>"; };
|
||||||
D2E1FADE2268B8E700AEFD8C /* ThreeLayerTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThreeLayerTableViewController.swift; sourceTree = "<group>"; };
|
D2E1FADE2268B8E700AEFD8C /* ThreeLayerTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThreeLayerTableViewController.swift; sourceTree = "<group>"; };
|
||||||
D2E1FAE02268E81D00AEFD8C /* MoleculeListTemplate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MoleculeListTemplate.swift; sourceTree = "<group>"; };
|
D2E1FAE02268E81D00AEFD8C /* MoleculeListTemplate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MoleculeListTemplate.swift; sourceTree = "<group>"; };
|
||||||
|
DB06250A2293456500B72DD3 /* LeftRightLabelView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LeftRightLabelView.swift; sourceTree = "<group>"; };
|
||||||
DB891E822253FA8500022516 /* Label.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Label.swift; sourceTree = "<group>"; };
|
DB891E822253FA8500022516 /* Label.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Label.swift; sourceTree = "<group>"; };
|
||||||
DBC4391622442196001AB423 /* CaretView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CaretView.swift; sourceTree = "<group>"; };
|
DBC4391622442196001AB423 /* CaretView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CaretView.swift; sourceTree = "<group>"; };
|
||||||
DBC4391722442197001AB423 /* DashLine.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DashLine.swift; sourceTree = "<group>"; };
|
DBC4391722442197001AB423 /* DashLine.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DashLine.swift; sourceTree = "<group>"; };
|
||||||
@ -590,6 +592,7 @@
|
|||||||
children = (
|
children = (
|
||||||
DBC4391622442196001AB423 /* CaretView.swift */,
|
DBC4391622442196001AB423 /* CaretView.swift */,
|
||||||
DBC4391722442197001AB423 /* DashLine.swift */,
|
DBC4391722442197001AB423 /* DashLine.swift */,
|
||||||
|
DB06250A2293456500B72DD3 /* LeftRightLabelView.swift */,
|
||||||
D29DF17E21E69E2E003B2FB9 /* MFView.h */,
|
D29DF17E21E69E2E003B2FB9 /* MFView.h */,
|
||||||
D29DF17F21E69E2E003B2FB9 /* MFView.m */,
|
D29DF17F21E69E2E003B2FB9 /* MFView.m */,
|
||||||
D29DF31E21ED0CBA003B2FB9 /* LabelView.h */,
|
D29DF31E21ED0CBA003B2FB9 /* LabelView.h */,
|
||||||
@ -950,6 +953,7 @@
|
|||||||
D29DF29521E7ADB8003B2FB9 /* ProgrammaticScrollViewController.m in Sources */,
|
D29DF29521E7ADB8003B2FB9 /* ProgrammaticScrollViewController.m in Sources */,
|
||||||
D29DF16121E69996003B2FB9 /* MFViewController.m in Sources */,
|
D29DF16121E69996003B2FB9 /* MFViewController.m in Sources */,
|
||||||
D2E1FAE12268E81D00AEFD8C /* MoleculeListTemplate.swift in Sources */,
|
D2E1FAE12268E81D00AEFD8C /* MoleculeListTemplate.swift in Sources */,
|
||||||
|
DB06250B2293456500B72DD3 /* LeftRightLabelView.swift in Sources */,
|
||||||
D22D1F47220496A30077CEC0 /* MVMCoreUISwitch.m in Sources */,
|
D22D1F47220496A30077CEC0 /* MVMCoreUISwitch.m in Sources */,
|
||||||
D29DF28C21E7AC2B003B2FB9 /* ViewConstrainingView.m in Sources */,
|
D29DF28C21E7AC2B003B2FB9 /* ViewConstrainingView.m in Sources */,
|
||||||
D29DF17B21E69E1F003B2FB9 /* PrimaryButton.m in Sources */,
|
D29DF17B21E69E1F003B2FB9 /* PrimaryButton.m in Sources */,
|
||||||
|
|||||||
@ -9,28 +9,49 @@
|
|||||||
|
|
||||||
import MVMCore
|
import MVMCore
|
||||||
|
|
||||||
|
public typealias ActionBlock = () -> Void
|
||||||
|
|
||||||
@objc open class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol, MVMCoreUIViewConstrainingProtocol {
|
|
||||||
|
@objcMembers open class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol, MVMCoreUIViewConstrainingProtocol, MFButtonProtocol {
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
// MARK: - Properties
|
// MARK: - General Properties
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
|
|
||||||
// Set this property if you want updateView to update the font based on this standard and the size passed in.
|
public var makeWholeViewClickable = false
|
||||||
@objc public var standardFontSize: CGFloat = 0.0
|
|
||||||
|
|
||||||
// Set this to use a custom sizing object during updateView instead of the standard.
|
/// Set this property if you want updateView to update the font based on this standard and the size passed in.
|
||||||
@objc public var sizeObject: MFSizeObject?
|
public var standardFontSize: CGFloat = 0.0
|
||||||
|
|
||||||
@objc public var scaleSize: NSNumber?
|
/// Set this to use a custom sizing object during updateView instead of the standard.
|
||||||
|
public var sizeObject: MFSizeObject?
|
||||||
|
public var scaleSize: NSNumber?
|
||||||
|
|
||||||
// Used for scaling the font in updateView.
|
// Used for scaling the font in updateView.
|
||||||
private var originalAttributedString: NSAttributedString?
|
private var originalAttributedString: NSAttributedString?
|
||||||
|
|
||||||
@objc public var hasText: Bool {
|
public var hasText: Bool {
|
||||||
guard let text = text, let attributedText = attributedText else { return false }
|
guard let text = text, let attributedText = attributedText else { return false }
|
||||||
return !text.isEmpty || !attributedText.string.isEmpty
|
return !text.isEmpty || !attributedText.string.isEmpty
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------
|
||||||
|
// MARK: - Multi-Action Text
|
||||||
|
//------------------------------------------------------
|
||||||
|
|
||||||
|
public var clauses: [ActionableClause] = [] {
|
||||||
|
didSet { isUserInteractionEnabled = !clauses.isEmpty }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Used for tappable links in the text.
|
||||||
|
public struct ActionableClause {
|
||||||
|
var range: NSRange?
|
||||||
|
var actionBlock: ActionBlock?
|
||||||
|
|
||||||
|
func performAction() {
|
||||||
|
actionBlock?()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
// MARK: - Initialization
|
// MARK: - Initialization
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
@ -41,6 +62,10 @@ import MVMCore
|
|||||||
numberOfLines = 0
|
numberOfLines = 0
|
||||||
lineBreakMode = .byWordWrapping
|
lineBreakMode = .byWordWrapping
|
||||||
translatesAutoresizingMaskIntoConstraints = false
|
translatesAutoresizingMaskIntoConstraints = false
|
||||||
|
|
||||||
|
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(textLinkTapped(_:)))
|
||||||
|
tapGesture.numberOfTapsRequired = 1
|
||||||
|
addGestureRecognizer(tapGesture)
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc public init() {
|
@objc public init() {
|
||||||
@ -64,21 +89,24 @@ import MVMCore
|
|||||||
}
|
}
|
||||||
|
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
// MARK: - Functions
|
// MARK: - Factory Functions
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
|
|
||||||
|
/// H1 -> HeadlineLarge
|
||||||
@objc public static func commonLabelH1(_ scale: Bool) -> Label {
|
@objc public static func commonLabelH1(_ scale: Bool) -> Label {
|
||||||
let label = Label.label()
|
let label = Label.label()
|
||||||
label.styleH1(scale)
|
label.styleH1(scale)
|
||||||
return label
|
return label
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// H2 -> Headline
|
||||||
@objc public static func commonLabelH2(_ scale: Bool) -> Label {
|
@objc public static func commonLabelH2(_ scale: Bool) -> Label {
|
||||||
let label = Label.label()
|
let label = Label.label()
|
||||||
label.styleH2(scale)
|
label.styleH2(scale)
|
||||||
return label
|
return label
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// H3 -> SubHead
|
||||||
@objc public static func commonLabelH3(_ scale: Bool) -> Label {
|
@objc public static func commonLabelH3(_ scale: Bool) -> Label {
|
||||||
let label = Label.label()
|
let label = Label.label()
|
||||||
label.styleH3(scale)
|
label.styleH3(scale)
|
||||||
@ -91,18 +119,21 @@ import MVMCore
|
|||||||
return label
|
return label
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// B1 -> SubTitle
|
||||||
@objc public static func commonLabelB1(_ scale: Bool) -> Label {
|
@objc public static func commonLabelB1(_ scale: Bool) -> Label {
|
||||||
let label = Label.label()
|
let label = Label.label()
|
||||||
label.styleB1(scale)
|
label.styleB1(scale)
|
||||||
return label
|
return label
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// B2 -> Body
|
||||||
@objc public static func commonLabelB2(_ scale: Bool) -> Label {
|
@objc public static func commonLabelB2(_ scale: Bool) -> Label {
|
||||||
let label = Label.label()
|
let label = Label.label()
|
||||||
label.styleB2(scale)
|
label.styleB2(scale)
|
||||||
return label
|
return label
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// B3 -> Legal
|
||||||
@objc public static func commonLabelB3(_ scale: Bool) -> Label {
|
@objc public static func commonLabelB3(_ scale: Bool) -> Label {
|
||||||
let label = Label.label()
|
let label = Label.label()
|
||||||
label.styleB3(scale)
|
label.styleB3(scale)
|
||||||
@ -142,7 +173,7 @@ import MVMCore
|
|||||||
@objc public static func setUILabel(_ label: UILabel?, withJSON json: [AnyHashable: Any]?, delegate: DelegateObject?, additionalData: [AnyHashable: Any]?) {
|
@objc public static func setUILabel(_ label: UILabel?, withJSON json: [AnyHashable: Any]?, delegate: DelegateObject?, additionalData: [AnyHashable: Any]?) {
|
||||||
|
|
||||||
guard let label = label else { return }
|
guard let label = label else { return }
|
||||||
|
label.attributedText = nil
|
||||||
label.text = json?.optionalStringForKey(KeyText)
|
label.text = json?.optionalStringForKey(KeyText)
|
||||||
|
|
||||||
setLabel(label, withHTML: json?.optionalStringForKey("html"))
|
setLabel(label, withHTML: json?.optionalStringForKey("html"))
|
||||||
@ -173,7 +204,6 @@ import MVMCore
|
|||||||
let attributedString = NSMutableAttributedString(string: labelText, attributes: [NSAttributedString.Key.font: label.font as UIFont,
|
let attributedString = NSMutableAttributedString(string: labelText, attributes: [NSAttributedString.Key.font: label.font as UIFont,
|
||||||
NSAttributedString.Key.foregroundColor: label.textColor as UIColor])
|
NSAttributedString.Key.foregroundColor: label.textColor as UIColor])
|
||||||
for case let attribute as [String: Any] in attributes {
|
for case let attribute as [String: Any] in attributes {
|
||||||
|
|
||||||
guard let attributeType = attribute.optionalStringForKey(KeyType),
|
guard let attributeType = attribute.optionalStringForKey(KeyType),
|
||||||
let location = attribute["location"] as? Int,
|
let location = attribute["location"] as? Int,
|
||||||
let length = attribute["length"] as? Int
|
let length = attribute["length"] as? Int
|
||||||
@ -214,6 +244,13 @@ import MVMCore
|
|||||||
attributedString.addAttribute(.font, value: font, range: range)
|
attributedString.addAttribute(.font, value: font, range: range)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
case "action":
|
||||||
|
guard let actionLabel = label as? Label else { continue }
|
||||||
|
|
||||||
|
actionLabel.addTappableLinkAttribute(range: range,
|
||||||
|
actionMap: json,
|
||||||
|
additionalData: additionalData,
|
||||||
|
delegateObject: delegate)
|
||||||
default:
|
default:
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -222,6 +259,8 @@ import MVMCore
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
// MARK: - Methods
|
// MARK: - Methods
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
@ -301,10 +340,10 @@ import MVMCore
|
|||||||
standardFontSize = 0
|
standardFontSize = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//------------------------------------------------------
|
// MARK: - Atomization
|
||||||
// MARK: - Atomization
|
extension Label {
|
||||||
//------------------------------------------------------
|
|
||||||
|
|
||||||
public func reset() {
|
public func reset() {
|
||||||
text = nil
|
text = nil
|
||||||
@ -314,6 +353,7 @@ import MVMCore
|
|||||||
}
|
}
|
||||||
|
|
||||||
@objc public func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
|
@objc public func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
|
||||||
|
clauses = []
|
||||||
Label.setUILabel(self, withJSON: json, delegate: delegateObject, additionalData: additionalData)
|
Label.setUILabel(self, withJSON: json, delegate: delegateObject, additionalData: additionalData)
|
||||||
originalAttributedString = attributedText
|
originalAttributedString = attributedText
|
||||||
}
|
}
|
||||||
@ -323,7 +363,7 @@ import MVMCore
|
|||||||
}
|
}
|
||||||
|
|
||||||
public func needsToBeConstrained() -> Bool {
|
public func needsToBeConstrained() -> Bool {
|
||||||
return true;
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
public func alignment() -> UIStackView.Alignment {
|
public func alignment() -> UIStackView.Alignment {
|
||||||
@ -331,6 +371,111 @@ import MVMCore
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: - Multi-Action Functionality
|
||||||
extension Label {
|
extension Label {
|
||||||
|
|
||||||
|
/// Reseting to default Label values.
|
||||||
|
@objc public func clearActionableClauses() {
|
||||||
|
|
||||||
|
guard let attributedText = attributedText else { return }
|
||||||
|
let mutableAttributedString = NSMutableAttributedString(attributedString: attributedText)
|
||||||
|
|
||||||
|
clauses.forEach { clause in
|
||||||
|
guard let range = clause.range else { return }
|
||||||
|
mutableAttributedString.removeAttribute(NSAttributedString.Key.underlineStyle, range: range)
|
||||||
|
}
|
||||||
|
|
||||||
|
self.attributedText = mutableAttributedString
|
||||||
|
clauses = []
|
||||||
|
}
|
||||||
|
|
||||||
|
public func createActionBlockFrom(actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) -> ActionBlock {
|
||||||
|
return { [weak self] in
|
||||||
|
if let wSelf = self, (delegateObject as? MVMCoreUIDelegateObject)?.buttonDelegate?.button?(wSelf, shouldPerformActionWithMap: actionMap, additionalData: additionalData) ?? true {
|
||||||
|
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegateObject: delegateObject)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fileprivate func setDefaultAttributes(range: NSRange) {
|
||||||
|
|
||||||
|
guard let attributedText = attributedText else { return }
|
||||||
|
|
||||||
|
let mutableAttributedString = NSMutableAttributedString(attributedString: attributedText)
|
||||||
|
mutableAttributedString.addAttributes([NSAttributedString.Key.underlineStyle: NSUnderlineStyle.single.rawValue], range: range)
|
||||||
|
|
||||||
|
self.attributedText = mutableAttributedString
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Provides an actionable range of text.
|
||||||
|
|
||||||
|
- Attention: This method expects text to be set first. Otherwise, it will do nothing.
|
||||||
|
- Parameters:
|
||||||
|
- range: The range of text to be tapped.
|
||||||
|
- actionBlock: The code triggered when tapping the range of text.
|
||||||
|
*/
|
||||||
|
@objc public func addTappableLinkAttribute(range: NSRange, actionBlock: @escaping ActionBlock) {
|
||||||
|
|
||||||
|
setDefaultAttributes(range: range)
|
||||||
|
clauses.append(ActionableClause(range: range, actionBlock: actionBlock))
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Provides an actionable range of text.
|
||||||
|
|
||||||
|
- Attention: This method expects text to be set first. Otherwise, it will do nothing.
|
||||||
|
- Parameters:
|
||||||
|
- range: The range of text to be tapped.
|
||||||
|
- actionMap:
|
||||||
|
- delegate:
|
||||||
|
- additionalData:
|
||||||
|
*/
|
||||||
|
@objc public func addTappableLinkAttribute(range: NSRange, actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) {
|
||||||
|
|
||||||
|
setDefaultAttributes(range: range)
|
||||||
|
clauses.append(ActionableClause(range: range,
|
||||||
|
actionBlock: createActionBlockFrom(actionMap: actionMap,
|
||||||
|
additionalData: additionalData,
|
||||||
|
delegateObject: delegateObject)))
|
||||||
|
}
|
||||||
|
|
||||||
|
@objc private func textLinkTapped(_ gesture: UITapGestureRecognizer) {
|
||||||
|
|
||||||
|
for clause in clauses {
|
||||||
|
if let range = clause.range, gesture.didTapAttributedTextInLabel(self, inRange: range) {
|
||||||
|
clause.performAction()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: -
|
||||||
|
extension UITapGestureRecognizer {
|
||||||
|
|
||||||
|
func didTapAttributedTextInLabel(_ label: Label, inRange targetRange: NSRange) -> Bool {
|
||||||
|
|
||||||
|
if label.makeWholeViewClickable {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let attributedText = label.attributedText else { return false }
|
||||||
|
|
||||||
|
let layoutManager = NSLayoutManager()
|
||||||
|
let textContainer = NSTextContainer(size: .zero)
|
||||||
|
let textStorage = NSTextStorage(attributedString: attributedText)
|
||||||
|
|
||||||
|
layoutManager.addTextContainer(textContainer)
|
||||||
|
textStorage.addLayoutManager(layoutManager)
|
||||||
|
|
||||||
|
textContainer.lineFragmentPadding = 0.0
|
||||||
|
textContainer.lineBreakMode = label.lineBreakMode
|
||||||
|
textContainer.maximumNumberOfLines = label.numberOfLines
|
||||||
|
textContainer.size = label.bounds.size
|
||||||
|
|
||||||
|
let indexOfCharacter = layoutManager.characterIndex(for: location(in: label), in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
|
||||||
|
|
||||||
|
return NSLocationInRange(indexOfCharacter, targetRange)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,25 +9,22 @@
|
|||||||
|
|
||||||
import MVMCore
|
import MVMCore
|
||||||
|
|
||||||
public typealias ActionBlock = () -> Void
|
|
||||||
private typealias ActionableStringTuple = (front: String?, action: String?, end: String?)
|
|
||||||
public typealias ActionObjectDelegate = NSObjectProtocol & MVMCoreActionDelegateProtocol
|
public typealias ActionObjectDelegate = NSObjectProtocol & MVMCoreActionDelegateProtocol
|
||||||
public typealias ButtonObjectDelegate = NSObjectProtocol & ButtonDelegateProtocol
|
public typealias ButtonObjectDelegate = NSObjectProtocol & ButtonDelegateProtocol
|
||||||
public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProtocol & MVMCoreLoadDelegateProtocol & MVMCorePresentationDelegateProtocol & NSObjectProtocol
|
public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProtocol & MVMCoreLoadDelegateProtocol & MVMCorePresentationDelegateProtocol & NSObjectProtocol
|
||||||
|
|
||||||
|
|
||||||
@objcMembers open class LabelWithInternalButton: UIControl, MVMCoreViewProtocol, MFButtonProtocol {
|
@available(*, deprecated, message: "Use Label instead.")
|
||||||
|
@objcMembers open class LabelWithInternalButton: UIControl, MVMCoreViewProtocol, MFButtonProtocol, MVMCoreUIMoleculeViewProtocol {
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
// MARK: - Properties
|
// MARK: - Properties
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
|
|
||||||
public var actionBlock: ActionBlock?
|
|
||||||
public weak var label: Label?
|
public weak var label: Label?
|
||||||
|
|
||||||
public var attributedText: NSAttributedString? {
|
public var attributedText: NSAttributedString? {
|
||||||
willSet(newAttributedText) {
|
willSet(newAttributedText) {
|
||||||
if let newAttribText = newAttributedText {
|
if let newAttribText = newAttributedText {
|
||||||
|
|
||||||
let mutableAttributedText = NSMutableAttributedString(attributedString: newAttribText)
|
let mutableAttributedText = NSMutableAttributedString(attributedString: newAttribText)
|
||||||
let paragraphStyle = NSMutableParagraphStyle()
|
let paragraphStyle = NSMutableParagraphStyle()
|
||||||
paragraphStyle.lineSpacing = CGFloat(LabelWithInternalButtonLineSpace)
|
paragraphStyle.lineSpacing = CGFloat(LabelWithInternalButtonLineSpace)
|
||||||
@ -62,16 +59,34 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public var actionBlock: ActionBlock? {
|
||||||
|
willSet(newActionBlock) {
|
||||||
|
if newActionBlock == nil {
|
||||||
|
label?.clearActionableClauses()
|
||||||
|
} else {
|
||||||
|
label?.clauses = [Label.ActionableClause(range: actionRange, actionBlock: newActionBlock)]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private var frontRange: NSRange {
|
||||||
|
return NSRange(location: 0, length: frontText?.count ?? 0)
|
||||||
|
}
|
||||||
|
|
||||||
private var actionRange: NSRange {
|
private var actionRange: NSRange {
|
||||||
return NSRange(location: frontText?.count ?? 0, length: actionText?.count ?? 0)
|
return NSRange(location: frontText?.count ?? 0, length: actionText?.count ?? 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
public var makeWholeViewClickable = false
|
private var backRange: NSRange {
|
||||||
|
return NSRange(location: (frontText?.count ?? 0) + (actionText?.count ?? 0), length: backText?.count ?? 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
public var makeWholeViewClickable = false {
|
||||||
|
willSet(newBool) { label?.makeWholeViewClickable = newBool }
|
||||||
|
}
|
||||||
|
|
||||||
override open var isEnabled: Bool {
|
override open var isEnabled: Bool {
|
||||||
didSet {
|
didSet { alpha = isEnabled ? 1 : DisableOppacity }
|
||||||
alpha = isEnabled ? 1 : DisableOppacity
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public var frontText: String?
|
public var frontText: String?
|
||||||
@ -81,9 +96,7 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
|
|||||||
private var internalText: String = ""
|
private var internalText: String = ""
|
||||||
|
|
||||||
public var text: String? {
|
public var text: String? {
|
||||||
get {
|
get { return internalText }
|
||||||
return internalText
|
|
||||||
}
|
|
||||||
set {
|
set {
|
||||||
guard let text = newValue else { return }
|
guard let text = newValue else { return }
|
||||||
internalText = text
|
internalText = text
|
||||||
@ -101,117 +114,92 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
|
|||||||
|
|
||||||
public init() {
|
public init() {
|
||||||
super.init(frame: .zero)
|
super.init(frame: .zero)
|
||||||
setup()
|
createLabel()
|
||||||
|
setLabelAttributes()
|
||||||
}
|
}
|
||||||
|
|
||||||
required public init?(coder: NSCoder) {
|
required public init?(coder: NSCoder) {
|
||||||
super.init(coder: coder)
|
super.init(coder: coder)
|
||||||
setup()
|
createLabel()
|
||||||
|
setLabelAttributes()
|
||||||
}
|
}
|
||||||
|
|
||||||
override public init(frame: CGRect) {
|
override public init(frame: CGRect) {
|
||||||
super.init(frame: frame)
|
super.init(frame: frame)
|
||||||
setup()
|
createLabel()
|
||||||
|
setLabelAttributes()
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - legacy
|
// Legacy
|
||||||
public init(frontText: String?, actionText: String?, backText: String?, actionBlock block: ActionBlock?) {
|
public init(frontText: String?, actionText: String?, backText: String?, actionBlock block: ActionBlock?) {
|
||||||
super.init(frame: .zero)
|
super.init(frame: .zero)
|
||||||
|
|
||||||
|
createLabel()
|
||||||
self.frontText = frontText
|
self.frontText = frontText
|
||||||
self.actionText = actionText
|
self.actionText = actionText
|
||||||
self.backText = backText
|
self.backText = backText
|
||||||
actionBlock = block
|
|
||||||
text = getTextFromStringComponents()
|
text = getTextFromStringComponents()
|
||||||
setup()
|
actionBlock = block
|
||||||
|
setLabelAttributes()
|
||||||
}
|
}
|
||||||
|
|
||||||
public convenience init(actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) {
|
public convenience init(actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) {
|
||||||
self.init(frontText: actionMap?.optionalStringForKey(KeyTitlePrefix),
|
self.init(frontText: actionMap?.optionalStringForKey(KeyTitlePrefix), actionText: actionMap?.optionalStringForKey(KeyTitle), backText: actionMap?.optionalStringForKey(KeyTitlePostfix), actionMap: actionMap, additionalData: additionalData, delegateObject: delegateObject)
|
||||||
actionText: actionMap?.optionalStringForKey(KeyTitle),
|
|
||||||
backText: actionMap?.optionalStringForKey(KeyTitlePostfix),
|
|
||||||
actionMap: actionMap, additionalData: additionalData,
|
|
||||||
delegateObject: delegateObject)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public convenience init(frontText: String?, backText: String?, actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) {
|
public convenience init(frontText: String?, backText: String?, actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) {
|
||||||
self.init(frontText: frontText,
|
self.init(frontText: frontText, actionText: actionMap?.optionalStringForKey(KeyTitle), backText: backText, actionMap: actionMap, additionalData: additionalData, delegateObject: delegateObject)
|
||||||
actionText: actionMap?.optionalStringForKey(KeyTitle),
|
|
||||||
backText: backText,
|
|
||||||
actionMap: actionMap,
|
|
||||||
additionalData: additionalData,
|
|
||||||
delegateObject: delegateObject)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public init(frontText: String?, actionText: String?, backText: String?, actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) {
|
public init(frontText: String?, actionText: String?, backText: String?, actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) {
|
||||||
super.init(frame: CGRect.zero)
|
super.init(frame: .zero)
|
||||||
|
|
||||||
setFrontText(frontText, actionText: actionText, actionMap: actionMap, backText: backText, additionalData: additionalData, delegateObject: delegateObject)
|
setFrontText(frontText, actionText: actionText, actionMap: actionMap, backText: backText, additionalData: additionalData, delegateObject: delegateObject)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convenience Initializer which assumes that the clickable text will be embedded in curly braces {}.
|
// Convenience Initializer which assumes that the clickable text will be embedded in curly braces {}.
|
||||||
public convenience init(clickableTextEmbeddedInCurlyBraces fullText: String?, actionMapForClickableText actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) {
|
public convenience init(clickableTextEmbeddedInCurlyBraces fullText: String?, actionMapForClickableText actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) {
|
||||||
self.init(text: fullText,
|
self.init(text: fullText, startTag: "{", endTag: "}", actionMap: actionMap, additionalData: additionalData, delegateObject: delegateObject)
|
||||||
startTag: "{",
|
|
||||||
endTag: "}",
|
|
||||||
actionMap: actionMap,
|
|
||||||
additionalData: additionalData,
|
|
||||||
delegateObject: delegateObject)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public init(text fullText: String?, startTag: String?, endTag: String?, actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) {
|
public init(text fullText: String?, startTag: String?, endTag: String?, actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) {
|
||||||
super.init(frame: CGRect.zero)
|
super.init(frame: .zero)
|
||||||
|
|
||||||
setText(fullText, startTag: startTag, endTag: endTag)
|
setText(fullText, startTag: startTag, endTag: endTag)
|
||||||
|
actionBlock = label?.createActionBlockFrom(actionMap: actionMap, additionalData: additionalData, delegateObject: delegateObject)
|
||||||
actionBlock = {
|
|
||||||
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegateObject: delegateObject)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
// MARK: - Configuration
|
// MARK: - Configuration
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
|
|
||||||
private func setup() {
|
/// Creates the Label that will be interacted with.
|
||||||
|
private func createLabel() {
|
||||||
|
|
||||||
if self.label == nil {
|
if self.label == nil {
|
||||||
let label = Label(frame: CGRect.zero)
|
let label = Label(frame: .zero)
|
||||||
|
|
||||||
backgroundColor = .clear
|
|
||||||
label.isUserInteractionEnabled = false
|
|
||||||
label.setContentCompressionResistancePriority(.required, for: .vertical)
|
label.setContentCompressionResistancePriority(.required, for: .vertical)
|
||||||
addSubview(label)
|
addSubview(label)
|
||||||
NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[label]-0-|", options: .directionLeadingToTrailing, metrics: nil, views: ["label": label]))
|
NSLayoutConstraint.constraintPinSubview(label, pinTop: true, pinBottom: true, pinLeft: true, pinRight: true)
|
||||||
NSLayoutConstraint.activate(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[label]-0-|", options: .directionLeadingToTrailing, metrics: nil, views: ["label": label]))
|
|
||||||
|
|
||||||
self.label = label
|
self.label = label
|
||||||
label.sizeToFit()
|
label.sizeToFit()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets up label. Best to call sometime after setting the text.
|
||||||
|
private func setLabelAttributes() {
|
||||||
|
|
||||||
// Adding the underline
|
// Adding the underline
|
||||||
setAlternateActionTextAttributes([NSAttributedString.Key.underlineStyle: NSNumber(value: NSUnderlineStyle.single.rawValue)])
|
setAlternateActionTextAttributes([NSAttributedString.Key.underlineStyle: NSNumber(value: NSUnderlineStyle.single.rawValue)])
|
||||||
|
|
||||||
self.label?.attributedText = attributedText
|
label?.attributedText = attributedText
|
||||||
self.label?.accessibilityTraits = .button
|
label?.accessibilityTraits = actionText?.isEmpty ?? true ? .staticText : .button
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc public func setActionMap(_ actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) {
|
@objc public func setActionMap(_ actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) {
|
||||||
|
|
||||||
weak var weakSelf: LabelWithInternalButton? = self
|
actionBlock = label?.createActionBlockFrom(actionMap: actionMap, additionalData: additionalData, delegateObject: delegateObject)
|
||||||
weak var weakButtonDelegate: ButtonDelegateProtocol? = (delegateObject as? MVMCoreUIDelegateObject)?.buttonDelegate
|
|
||||||
|
|
||||||
actionBlock = {
|
|
||||||
var performAction = true
|
|
||||||
|
|
||||||
if let wSelf = weakSelf, let wButtonDelegate = weakButtonDelegate, wButtonDelegate.responds(to: #selector(ButtonObjectDelegate.button(_:shouldPerformActionWithMap:additionalData:))) {
|
|
||||||
performAction = wButtonDelegate.button?(wSelf, shouldPerformActionWithMap: actionMap, additionalData: additionalData) ?? false
|
|
||||||
}
|
|
||||||
|
|
||||||
if performAction {
|
|
||||||
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegateObject: delegateObject)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
@ -220,12 +208,13 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
|
|||||||
|
|
||||||
@objc public func setFrontText(_ frontText: String?, actionText: String?, actionMap: [AnyHashable: Any]?, backText: String?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) {
|
@objc public func setFrontText(_ frontText: String?, actionText: String?, actionMap: [AnyHashable: Any]?, backText: String?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) {
|
||||||
|
|
||||||
|
createLabel()
|
||||||
self.frontText = frontText
|
self.frontText = frontText
|
||||||
setActionMap(actionMap, additionalData: additionalData, delegateObject: delegateObject)
|
|
||||||
self.actionText = actionText
|
self.actionText = actionText
|
||||||
self.backText = backText
|
self.backText = backText
|
||||||
text = getTextFromStringComponents()
|
text = getTextFromStringComponents()
|
||||||
setup()
|
setActionMap(actionMap, additionalData: additionalData, delegateObject: delegateObject)
|
||||||
|
setLabelAttributes()
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc public func setFrontText(_ frontText: String?, actionMap: [AnyHashable: Any]?, backText: String?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) {
|
@objc public func setFrontText(_ frontText: String?, actionMap: [AnyHashable: Any]?, backText: String?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) {
|
||||||
@ -244,10 +233,8 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let b2Font = MFStyler.fontB2(),
|
if let b2Font = MFStyler.fontB2(),
|
||||||
let actions = actionMap,
|
let actions = actionMap, actions.keys.count > 0,
|
||||||
actions.keys.count > 0,
|
let actionString = actions.optionalStringForKey(KeyTitle), !actionString.isEmpty {
|
||||||
let actionString = actions.optionalStringForKey(KeyTitle),
|
|
||||||
!actionString.isEmpty {
|
|
||||||
|
|
||||||
let actionStringOnLine = actionString + (addNewLine ? "\n" : " ")
|
let actionStringOnLine = actionString + (addNewLine ? "\n" : " ")
|
||||||
actionText = actionStringOnLine
|
actionText = actionStringOnLine
|
||||||
@ -264,7 +251,6 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
|
|||||||
}
|
}
|
||||||
|
|
||||||
attributedText = mutableAttributedString
|
attributedText = mutableAttributedString
|
||||||
// Added this line for underlining
|
|
||||||
setAlternateActionTextAttributes([NSAttributedString.Key.underlineStyle: NSNumber(value: NSUnderlineStyle.single.rawValue)])
|
setAlternateActionTextAttributes([NSAttributedString.Key.underlineStyle: NSNumber(value: NSUnderlineStyle.single.rawValue)])
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -276,51 +262,6 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
|
|||||||
label?.attributedText = attributedText
|
label?.attributedText = attributedText
|
||||||
}
|
}
|
||||||
|
|
||||||
//------------------------------------------------------
|
|
||||||
// MARK: - UIControl Override
|
|
||||||
//------------------------------------------------------
|
|
||||||
|
|
||||||
override open func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
|
|
||||||
|
|
||||||
if areTouches(inActionString: touches) {
|
|
||||||
sendActions(for: .touchUpInside)
|
|
||||||
if let action = actionBlock {
|
|
||||||
action()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
sendActions(for: .touchUpOutside)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private func areTouches(inActionString touches: Set<UITouch>?) -> Bool {
|
|
||||||
|
|
||||||
if UIAccessibility.isVoiceOverRunning || makeWholeViewClickable {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
let location: CGPoint? = touches?.first?.location(in: label)
|
|
||||||
let actionString = actionText
|
|
||||||
let index: Int = actionRange.location
|
|
||||||
let rangeArray = getRangeArrayOfWords(in: actionString, withInitalIndex: index)
|
|
||||||
let rectArray = getRectArray(fromRangeArray: rangeArray)
|
|
||||||
var result = false
|
|
||||||
|
|
||||||
for aValueOfRect in rectArray as? [NSValue] ?? [] {
|
|
||||||
let wordRect: CGRect = aValueOfRect.cgRectValue
|
|
||||||
|
|
||||||
if let position = location, wordRect.contains(position) {
|
|
||||||
result = true
|
|
||||||
break
|
|
||||||
} else if wordRect.origin.x == 0 && wordRect.origin.y == 0 && wordRect.size.height == 0 && wordRect.size.width == 0 {
|
|
||||||
// Incase word rect is not found for any reason, make the whole label to be clicable to avoid non functioning link in production.
|
|
||||||
result = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
// MARK: - Helper
|
// MARK: - Helper
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
@ -338,40 +279,6 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
|
|||||||
return "\(frontText ?? "")\(actionText ?? "")\(backText ?? "")"
|
return "\(frontText ?? "")\(actionText ?? "")\(backText ?? "")"
|
||||||
}
|
}
|
||||||
|
|
||||||
private func getRangeArrayOfWords(in string: String?, withInitalIndex index: Int) -> [Any]? {
|
|
||||||
|
|
||||||
var index = index
|
|
||||||
let words = string?.components(separatedBy: " ")
|
|
||||||
var rangeArray = [AnyHashable]()
|
|
||||||
|
|
||||||
for subString in words ?? [] {
|
|
||||||
let finalSubString = subString + " "
|
|
||||||
let wordIndex: Int = index
|
|
||||||
let length: Int = finalSubString.count
|
|
||||||
let subStringRange = NSRange(location: wordIndex, length: length)
|
|
||||||
let rangeValue = NSValue(range: subStringRange)
|
|
||||||
rangeArray.append(rangeValue)
|
|
||||||
index += length
|
|
||||||
}
|
|
||||||
|
|
||||||
return rangeArray
|
|
||||||
}
|
|
||||||
|
|
||||||
private func getRectArray(fromRangeArray rangeArray: [Any]?) -> [Any]? {
|
|
||||||
|
|
||||||
var rectArray = [AnyHashable]()
|
|
||||||
|
|
||||||
for aValueOfRange in rangeArray as? [NSValue] ?? [] {
|
|
||||||
let wordRange: NSRange = aValueOfRange.rangeValue
|
|
||||||
if let rect: CGRect = label?.boundingRect(forCharacterRange: wordRange) {
|
|
||||||
let rectValue = NSValue(cgRect: rect)
|
|
||||||
rectArray.append(rectValue)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return rectArray
|
|
||||||
}
|
|
||||||
|
|
||||||
@objc public func setCurlyBracedText(_ text: String) {
|
@objc public func setCurlyBracedText(_ text: String) {
|
||||||
|
|
||||||
setText(text, startTag: "{", endTag: "}")
|
setText(text, startTag: "{", endTag: "}")
|
||||||
@ -379,47 +286,24 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
|
|||||||
|
|
||||||
private func setText(_ text: String?, startTag: String?, endTag: String?) {
|
private func setText(_ text: String?, startTag: String?, endTag: String?) {
|
||||||
|
|
||||||
let actionableTuple: ActionableStringTuple = rangeOfText(text, startTag: startTag, endTag: endTag)
|
createLabel()
|
||||||
|
frontText = text
|
||||||
|
|
||||||
if let text = text,
|
if let text = text {
|
||||||
let leftTag = startTag,
|
var initialSegments = [String]()
|
||||||
text.contains(leftTag),
|
if let leftTag = startTag, text.contains(leftTag) {
|
||||||
let rightTag = endTag,
|
initialSegments = text.components(separatedBy: leftTag)
|
||||||
text.contains(rightTag),
|
frontText = initialSegments[0].trimmingCharacters(in: .whitespaces)
|
||||||
let front = actionableTuple.front,
|
|
||||||
let middle = actionableTuple.action,
|
|
||||||
let end = actionableTuple.end {
|
|
||||||
|
|
||||||
frontText = front.trimmingCharacters(in: .whitespaces)
|
if let rightTag = endTag, text.contains(rightTag) {
|
||||||
actionText = middle.trimmingCharacters(in: .whitespaces)
|
let secondPart = initialSegments[1].components(separatedBy: rightTag)
|
||||||
backText = end.trimmingCharacters(in: .whitespaces)
|
actionText = secondPart[0].trimmingCharacters(in: .whitespaces)
|
||||||
} else {
|
backText = secondPart[1].trimmingCharacters(in: .whitespaces)
|
||||||
frontText = text
|
}
|
||||||
}
|
|
||||||
|
|
||||||
self.text = getTextFromStringComponents()
|
|
||||||
setup()
|
|
||||||
}
|
|
||||||
|
|
||||||
private func rangeOfText(_ text: String?, startTag: String?, endTag: String?) -> ActionableStringTuple {
|
|
||||||
|
|
||||||
var actionableTuple: ActionableStringTuple = (front: nil, action: nil, end: nil)
|
|
||||||
|
|
||||||
guard let text = text else { return actionableTuple }
|
|
||||||
|
|
||||||
if let leftTag = startTag, text.contains(leftTag) {
|
|
||||||
|
|
||||||
let firstHalf = text.components(separatedBy: leftTag)
|
|
||||||
actionableTuple.front = firstHalf.first
|
|
||||||
|
|
||||||
if let rightTag = endTag, text.contains(rightTag) {
|
|
||||||
let secondHalf = firstHalf[1].components(separatedBy: rightTag)
|
|
||||||
actionableTuple.action = secondHalf[0]
|
|
||||||
actionableTuple.end = secondHalf[1]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
self.text = getTextFromStringComponents()
|
||||||
return actionableTuple
|
setLabelAttributes()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset the text and action map
|
// Reset the text and action map
|
||||||
@ -459,49 +343,34 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
|
|||||||
|
|
||||||
public func setAlternateNormalTextAttributes(_ attributes: [AnyHashable: Any]?) {
|
public func setAlternateNormalTextAttributes(_ attributes: [AnyHashable: Any]?) {
|
||||||
|
|
||||||
guard let _attributedText = attributedText, let _attributes = attributes as? [NSAttributedString.Key: Any] else { return }
|
guard let attributedText = attributedText, let attributes = attributes as? [NSAttributedString.Key: Any] else { return }
|
||||||
let attributedString = NSMutableAttributedString(attributedString: _attributedText)
|
let attributedString = NSMutableAttributedString(attributedString: attributedText)
|
||||||
|
|
||||||
if !attributedString.string.isEmpty {
|
if !attributedString.string.isEmpty {
|
||||||
attributedString.addAttributes(_attributes, range: getFrontRange())
|
attributedString.addAttributes(attributes, range: frontRange)
|
||||||
attributedString.addAttributes(_attributes, range: getBackRange())
|
attributedString.addAttributes(attributes, range: backRange)
|
||||||
}
|
}
|
||||||
attributedText = attributedString
|
self.attributedText = attributedString
|
||||||
}
|
|
||||||
|
|
||||||
private func getFrontRange() -> NSRange {
|
|
||||||
|
|
||||||
return NSRange(location: 0, length: frontText?.count ?? 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
private func getBackRange() -> NSRange {
|
|
||||||
|
|
||||||
let textLocation: Int = (frontText?.count ?? 0) + (actionText?.count ?? 0)
|
|
||||||
let rangeLength: Int = backText?.count ?? 0
|
|
||||||
|
|
||||||
return NSRange(location: textLocation, length: rangeLength)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc public func setActionTextString(_ actionText: String?) {
|
@objc public func setActionTextString(_ actionText: String?) {
|
||||||
|
|
||||||
|
createLabel()
|
||||||
self.actionText = actionText
|
self.actionText = actionText
|
||||||
text = getTextFromStringComponents()
|
text = getTextFromStringComponents()
|
||||||
setup()
|
setLabelAttributes()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Used to just reset the texts and actions if already initialized
|
/// Used to just reset the texts and actions if already initialized
|
||||||
@objc public func reset(withActionMap actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) {
|
@objc public func reset(withActionMap actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) {
|
||||||
|
|
||||||
|
createLabel()
|
||||||
frontText = actionMap?.optionalStringForKey(KeyTitlePrefix)
|
frontText = actionMap?.optionalStringForKey(KeyTitlePrefix)
|
||||||
actionText = actionMap?.optionalStringForKey(KeyTitle)
|
actionText = actionMap?.optionalStringForKey(KeyTitle)
|
||||||
backText = actionMap?.optionalStringForKey(KeyTitlePostfix)
|
backText = actionMap?.optionalStringForKey(KeyTitlePostfix)
|
||||||
|
|
||||||
actionBlock = {
|
|
||||||
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegateObject: delegateObject)
|
|
||||||
}
|
|
||||||
|
|
||||||
text = getTextFromStringComponents()
|
text = getTextFromStringComponents()
|
||||||
setup()
|
actionBlock = label?.createActionBlockFrom(actionMap: actionMap, additionalData: additionalData, delegateObject: delegateObject)
|
||||||
|
setLabelAttributes()
|
||||||
}
|
}
|
||||||
|
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
@ -539,20 +408,18 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
|
|||||||
|
|
||||||
@available(*, deprecated)
|
@available(*, deprecated)
|
||||||
public init(frontText: String?, actionText: String?, backText: String?, actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, actionDelegate delegate: ActionObjectDelegate?, buttonDelegate: ButtonObjectDelegate?) {
|
public init(frontText: String?, actionText: String?, backText: String?, actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, actionDelegate delegate: ActionObjectDelegate?, buttonDelegate: ButtonObjectDelegate?) {
|
||||||
super.init(frame: CGRect.zero)
|
super.init(frame: .zero)
|
||||||
|
|
||||||
setFrontText(frontText, actionText: actionText, actionMap: actionMap, backText: backText, additionalData: additionalData, delegate: delegate, buttonDelegate: buttonDelegate)
|
setFrontText(frontText, actionText: actionText, actionMap: actionMap, backText: backText, additionalData: additionalData, delegate: delegate, buttonDelegate: buttonDelegate)
|
||||||
}
|
}
|
||||||
|
|
||||||
@available(*, deprecated)
|
@available(*, deprecated)
|
||||||
public convenience init(actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, actionDelegate delegate: ActionObjectDelegate?, buttonDelegate: ButtonObjectDelegate?) {
|
public convenience init(actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, actionDelegate delegate: ActionObjectDelegate?, buttonDelegate: ButtonObjectDelegate?) {
|
||||||
|
|
||||||
self.init(frontText: actionMap?.optionalStringForKey(KeyTitlePrefix), actionText: actionMap?.optionalStringForKey(KeyTitle), backText: actionMap?.optionalStringForKey(KeyTitlePostfix), actionMap: actionMap, additionalData: additionalData, actionDelegate: delegate, buttonDelegate: buttonDelegate)
|
self.init(frontText: actionMap?.optionalStringForKey(KeyTitlePrefix), actionText: actionMap?.optionalStringForKey(KeyTitle), backText: actionMap?.optionalStringForKey(KeyTitlePostfix), actionMap: actionMap, additionalData: additionalData, actionDelegate: delegate, buttonDelegate: buttonDelegate)
|
||||||
}
|
}
|
||||||
|
|
||||||
@available(*, deprecated)
|
@available(*, deprecated)
|
||||||
public convenience init(frontText: String?, backText: String?, actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, actionDelegate delegate: ActionObjectDelegate?, buttonDelegate: ButtonObjectDelegate?) {
|
public convenience init(frontText: String?, backText: String?, actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, actionDelegate delegate: ActionObjectDelegate?, buttonDelegate: ButtonObjectDelegate?) {
|
||||||
|
|
||||||
self.init(frontText: frontText, actionText: actionMap?.optionalStringForKey(KeyTitle), backText: backText, actionMap: actionMap, additionalData: additionalData, actionDelegate: delegate, buttonDelegate: buttonDelegate)
|
self.init(frontText: frontText, actionText: actionMap?.optionalStringForKey(KeyTitle), backText: backText, actionMap: actionMap, additionalData: additionalData, actionDelegate: delegate, buttonDelegate: buttonDelegate)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -583,29 +450,18 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
|
|||||||
|
|
||||||
setText(fullText, startTag: startTag, endTag: endTag)
|
setText(fullText, startTag: startTag, endTag: endTag)
|
||||||
|
|
||||||
weak var weakDelegate: ActionObjectDelegate? = delegate
|
actionBlock = { [weak delegate] in
|
||||||
|
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegate: delegate as? CoreObjectActionLoadPresentDelegate)
|
||||||
actionBlock = {
|
|
||||||
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegate: weakDelegate as? CoreObjectActionLoadPresentDelegate)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@available(*, deprecated)
|
@available(*, deprecated)
|
||||||
private func setActionMap(_ actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, actionDelegate delegate: ActionObjectDelegate?, buttonDelegate: ButtonObjectDelegate?) {
|
private func setActionMap(_ actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, actionDelegate delegate: ActionObjectDelegate?, buttonDelegate: ButtonObjectDelegate?) {
|
||||||
|
|
||||||
weak var weakSelf: LabelWithInternalButton? = self
|
actionBlock = { [weak self, weak delegate, weak buttonDelegate] in
|
||||||
weak var weakDelegate: ActionObjectDelegate? = delegate
|
|
||||||
weak var weakButtonDelegate: ButtonObjectDelegate? = buttonDelegate
|
|
||||||
|
|
||||||
actionBlock = {
|
if let wSelf = self, buttonDelegate?.button?(wSelf, shouldPerformActionWithMap: actionMap, additionalData: additionalData) ?? true {
|
||||||
var performAction = true
|
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegate: delegate as? CoreObjectActionLoadPresentDelegate)
|
||||||
|
|
||||||
if let wSelf = weakSelf, let wButtonDelegate = weakButtonDelegate, wButtonDelegate.responds(to: #selector(ButtonObjectDelegate.button(_:shouldPerformActionWithMap:additionalData:))) {
|
|
||||||
performAction = wButtonDelegate.button?(wSelf, shouldPerformActionWithMap: actionMap, additionalData: additionalData) ?? false
|
|
||||||
}
|
|
||||||
|
|
||||||
if performAction {
|
|
||||||
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegate: weakDelegate as? CoreObjectActionLoadPresentDelegate)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -618,12 +474,13 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
|
|||||||
@available(*, deprecated)
|
@available(*, deprecated)
|
||||||
@objc public func setFrontText(_ frontText: String?, actionText: String?, actionMap: [AnyHashable: Any]?, backText: String?, additionalData: [AnyHashable: Any]?, delegate: ActionObjectDelegate?, buttonDelegate: ButtonObjectDelegate?) {
|
@objc public func setFrontText(_ frontText: String?, actionText: String?, actionMap: [AnyHashable: Any]?, backText: String?, additionalData: [AnyHashable: Any]?, delegate: ActionObjectDelegate?, buttonDelegate: ButtonObjectDelegate?) {
|
||||||
|
|
||||||
|
createLabel()
|
||||||
self.frontText = frontText
|
self.frontText = frontText
|
||||||
setActionMap(actionMap, additionalData: additionalData, actionDelegate: delegate, buttonDelegate: buttonDelegate)
|
|
||||||
self.actionText = actionText
|
self.actionText = actionText
|
||||||
self.backText = backText
|
self.backText = backText
|
||||||
text = getTextFromStringComponents()
|
text = getTextFromStringComponents()
|
||||||
setup()
|
setActionMap(actionMap, additionalData: additionalData, actionDelegate: delegate, buttonDelegate: buttonDelegate)
|
||||||
|
setLabelAttributes()
|
||||||
}
|
}
|
||||||
|
|
||||||
@available(*, deprecated)
|
@available(*, deprecated)
|
||||||
@ -644,10 +501,8 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let b2Font = MFStyler.fontB2(),
|
if let b2Font = MFStyler.fontB2(),
|
||||||
let actions = actionMap,
|
let actions = actionMap, actions.keys.count > 0,
|
||||||
actions.keys.count > 0,
|
let actionString = actions.optionalStringForKey(KeyTitle), !actionString.isEmpty {
|
||||||
let actionString = actions.optionalStringForKey(KeyTitle),
|
|
||||||
!actionString.isEmpty {
|
|
||||||
|
|
||||||
let actionStringOnLine = actionString + (addNewLine ? "\n" : " ")
|
let actionStringOnLine = actionString + (addNewLine ? "\n" : " ")
|
||||||
actionText = actionStringOnLine
|
actionText = actionStringOnLine
|
||||||
@ -664,7 +519,6 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
|
|||||||
}
|
}
|
||||||
|
|
||||||
attributedText = mutableAttributedString
|
attributedText = mutableAttributedString
|
||||||
// Added this line for underlining
|
|
||||||
setAlternateActionTextAttributes([NSAttributedString.Key.underlineStyle: NSNumber(value: NSUnderlineStyle.single.rawValue)])
|
setAlternateActionTextAttributes([NSAttributedString.Key.underlineStyle: NSNumber(value: NSUnderlineStyle.single.rawValue)])
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -712,23 +566,22 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
|
|||||||
@available(*, deprecated)
|
@available(*, deprecated)
|
||||||
@objc public func reset(withActionMap actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegate: ActionObjectDelegate?) {
|
@objc public func reset(withActionMap actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegate: ActionObjectDelegate?) {
|
||||||
|
|
||||||
|
createLabel()
|
||||||
frontText = actionMap?.optionalStringForKey(KeyTitlePrefix)
|
frontText = actionMap?.optionalStringForKey(KeyTitlePrefix)
|
||||||
actionText = actionMap?.optionalStringForKey(KeyTitle)
|
actionText = actionMap?.optionalStringForKey(KeyTitle)
|
||||||
backText = actionMap?.optionalStringForKey(KeyTitlePostfix)
|
backText = actionMap?.optionalStringForKey(KeyTitlePostfix)
|
||||||
|
|
||||||
weak var weakDelegate: ActionObjectDelegate? = delegate
|
|
||||||
|
|
||||||
actionBlock = {
|
|
||||||
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegate: weakDelegate as? CoreObjectActionLoadPresentDelegate)
|
|
||||||
}
|
|
||||||
|
|
||||||
text = getTextFromStringComponents()
|
text = getTextFromStringComponents()
|
||||||
setup()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// MARK: - Atomization
|
actionBlock = { [weak delegate] in
|
||||||
extension LabelWithInternalButton: MVMCoreUIMoleculeViewProtocol {
|
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegate: delegate as? CoreObjectActionLoadPresentDelegate)
|
||||||
|
}
|
||||||
|
setLabelAttributes()
|
||||||
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------
|
||||||
|
// MARK: - Atomization
|
||||||
|
//------------------------------------------------------
|
||||||
|
|
||||||
// Default values for view.
|
// Default values for view.
|
||||||
@objc open func setAsMolecule() {
|
@objc open func setAsMolecule() {
|
||||||
|
|
||||||
|
|||||||
183
MVMCoreUI/Atoms/Views/LeftRightLabelView.swift
Normal file
183
MVMCoreUI/Atoms/Views/LeftRightLabelView.swift
Normal file
@ -0,0 +1,183 @@
|
|||||||
|
//
|
||||||
|
// LeftRightLabelView.swift
|
||||||
|
// MVMCoreUI
|
||||||
|
//
|
||||||
|
// Created by Christiano, Kevin on 5/20/19.
|
||||||
|
// Copyright © 2019 Verizon Wireless. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
|
||||||
|
@objcMembers open class LeftRightLabelView: ViewConstrainingView {
|
||||||
|
//------------------------------------------------------
|
||||||
|
// MARK: - Outlets
|
||||||
|
//------------------------------------------------------
|
||||||
|
|
||||||
|
let leftTextLabel = Label.commonLabelB1(true)
|
||||||
|
let rightTextLabel = Label.commonLabelB1(true)
|
||||||
|
|
||||||
|
//------------------------------------------------------
|
||||||
|
// MARK: - Constraints
|
||||||
|
//------------------------------------------------------
|
||||||
|
|
||||||
|
var rightTextLabelLeading: NSLayoutConstraint?
|
||||||
|
var leftTextLabelTrailing: NSLayoutConstraint?
|
||||||
|
|
||||||
|
//------------------------------------------------------
|
||||||
|
// MARK: - Initialization
|
||||||
|
//------------------------------------------------------
|
||||||
|
|
||||||
|
public init() {
|
||||||
|
super.init(frame: .zero)
|
||||||
|
}
|
||||||
|
|
||||||
|
public override init(frame: CGRect) {
|
||||||
|
super.init(frame: frame)
|
||||||
|
}
|
||||||
|
|
||||||
|
required public init?(coder aDecoder: NSCoder) {
|
||||||
|
super.init(coder: aDecoder)
|
||||||
|
}
|
||||||
|
|
||||||
|
public convenience init(json: [AnyHashable: Any]?, delegateObject: DelegateObject?, additionalData: [AnyHashable: Any]?) {
|
||||||
|
self.init()
|
||||||
|
setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData)
|
||||||
|
}
|
||||||
|
|
||||||
|
override open func setupView() {
|
||||||
|
super.setupView()
|
||||||
|
|
||||||
|
guard subviews.isEmpty else { return }
|
||||||
|
|
||||||
|
addSubview(leftTextLabel)
|
||||||
|
addSubview(rightTextLabel)
|
||||||
|
|
||||||
|
leftTextLabel.textAlignment = .left
|
||||||
|
rightTextLabel.textAlignment = .right
|
||||||
|
|
||||||
|
constrainBothLabels()
|
||||||
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------
|
||||||
|
// MARK: - View Lifecycle
|
||||||
|
//------------------------------------------------------
|
||||||
|
|
||||||
|
override open func updateView(_ size: CGFloat) {
|
||||||
|
super.updateView(size)
|
||||||
|
|
||||||
|
leftTextLabel.updateView(size)
|
||||||
|
rightTextLabel.updateView(size)
|
||||||
|
|
||||||
|
// Resolves text layout issues found between both dynamically sized labels, number is not exact but performs as required.
|
||||||
|
if leftTextLabel.hasText && rightTextLabel.hasText {
|
||||||
|
let padding = MFStyler.defaultHorizontalPadding(forSize: size) * 2
|
||||||
|
let maximumTextWidth = (size - (padding + 16)) * 0.4
|
||||||
|
// Subtracting 10 resolves issues of SE and iPad
|
||||||
|
rightTextLabel.preferredMaxLayoutWidth = round(maximumTextWidth) - 10
|
||||||
|
} else {
|
||||||
|
rightTextLabel.preferredMaxLayoutWidth = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------
|
||||||
|
// MARK: - Setup
|
||||||
|
//------------------------------------------------------
|
||||||
|
|
||||||
|
private func constrainBothLabels() {
|
||||||
|
|
||||||
|
leftTextLabel.topAnchor.constraint(equalTo: topAnchor).isActive = true
|
||||||
|
leftTextLabel.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor).isActive = true
|
||||||
|
|
||||||
|
let leftTextBottom = leftTextLabel.bottomAnchor.constraint(equalTo: bottomAnchor)
|
||||||
|
leftTextBottom.priority = UILayoutPriority(249)
|
||||||
|
leftTextBottom.isActive = true
|
||||||
|
|
||||||
|
bottomAnchor.constraint(greaterThanOrEqualTo: leftTextLabel.bottomAnchor).isActive = true
|
||||||
|
|
||||||
|
rightTextLabelLeading = rightTextLabel.leadingAnchor.constraint(equalTo: leftTextLabel.trailingAnchor, constant: 16)
|
||||||
|
rightTextLabelLeading?.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
|
||||||
|
|
||||||
|
bottomAnchor.constraint(greaterThanOrEqualTo: rightTextLabel.bottomAnchor).isActive = true
|
||||||
|
|
||||||
|
let leftTextWidth = leftTextLabel.widthAnchor.constraint(greaterThanOrEqualTo: layoutMarginsGuide.widthAnchor, multiplier: 0.6)
|
||||||
|
leftTextWidth.priority = UILayoutPriority(rawValue: 995)
|
||||||
|
leftTextWidth.isActive = true
|
||||||
|
|
||||||
|
let rightTextWidth = rightTextLabel.widthAnchor.constraint(lessThanOrEqualTo: layoutMarginsGuide.widthAnchor, multiplier: 0.4)
|
||||||
|
rightTextWidth.priority = UILayoutPriority(rawValue: 906)
|
||||||
|
rightTextWidth.isActive = true
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
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() {
|
||||||
|
super.reset()
|
||||||
|
|
||||||
|
deactivateMiddleConstraint()
|
||||||
|
constrainBothLabels()
|
||||||
|
leftTextLabel.text = ""
|
||||||
|
rightTextLabel.text = ""
|
||||||
|
backgroundColor = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
private func deactivateMiddleConstraint() {
|
||||||
|
|
||||||
|
leftTextLabelTrailing?.isActive = false
|
||||||
|
rightTextLabelLeading?.isActive = false
|
||||||
|
}
|
||||||
|
|
||||||
|
//------------------------------------------------------
|
||||||
|
// MARK: - Atomization
|
||||||
|
//------------------------------------------------------
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
if let backgroundColorHex = dictionary[KeyBackgroundColor] as? String {
|
||||||
|
backgroundColor = UIColor.mfGet(forHex: backgroundColorHex)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !leftTextLabel.hasText {
|
||||||
|
constrainRightLabel()
|
||||||
|
} else if !rightTextLabel.hasText {
|
||||||
|
constrainLeftLabel()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -274,6 +274,11 @@
|
|||||||
[super setupView];
|
[super setupView];
|
||||||
self.translatesAutoresizingMaskIntoConstraints = NO;
|
self.translatesAutoresizingMaskIntoConstraints = NO;
|
||||||
self.backgroundColor = [UIColor clearColor];
|
self.backgroundColor = [UIColor clearColor];
|
||||||
|
if (@available(iOS 11.0, *)) {
|
||||||
|
self.directionalLayoutMargins = NSDirectionalEdgeInsetsZero;
|
||||||
|
} else {
|
||||||
|
self.layoutMargins = UIEdgeInsetsZero;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)updateView:(CGFloat)size {
|
- (void)updateView:(CGFloat)size {
|
||||||
|
|||||||
@ -44,8 +44,8 @@ import UIKit
|
|||||||
if let backgroundColorString = json?.optionalStringForKey(KeyBackgroundColor) {
|
if let backgroundColorString = json?.optionalStringForKey(KeyBackgroundColor) {
|
||||||
backgroundColor = UIColor.mfGet(forHex: backgroundColorString)
|
backgroundColor = UIColor.mfGet(forHex: backgroundColorString)
|
||||||
}
|
}
|
||||||
let primaryButtonMap = json?.optionalDictionaryForKey("primaryButton")
|
let primaryButtonMap = json?.optionalDictionaryForKey(KeyPrimaryButton)
|
||||||
let secondaryButtonMap = json?.optionalDictionaryForKey("secondaryButton")
|
let secondaryButtonMap = json?.optionalDictionaryForKey(KeySecondaryButton)
|
||||||
set(primaryButtonJSON: primaryButtonMap, secondaryButtonJSON: secondaryButtonMap, delegateObject: delegateObject, additionalData: additionalData)
|
set(primaryButtonJSON: primaryButtonMap, secondaryButtonJSON: secondaryButtonMap, delegateObject: delegateObject, additionalData: additionalData)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -42,7 +42,8 @@
|
|||||||
@"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