Updated Label. Change LabelWithInternal for when nil is set for actionBlock.

This commit is contained in:
Christiano, Kevin 2019-05-14 16:26:35 -04:00
parent 42bc51373a
commit ec1682a632
2 changed files with 65 additions and 79 deletions

View File

@ -92,18 +92,21 @@ public typealias ActionBlock = () -> Void
// MARK: - Factory 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)
@ -116,18 +119,21 @@ public typealias ActionBlock = () -> Void
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)
@ -242,10 +248,10 @@ public typealias ActionBlock = () -> Void
case "action": case "action":
guard let actionLabel = label as? Label else { continue } guard let actionLabel = label as? Label else { continue }
actionLabel.addActionableClause(range: range, actionLabel.addTappableLinkAttribute(range: range,
actionMap: json, actionMap: json,
additionalData: additionalData, additionalData: additionalData,
delegateObject: delegate) delegateObject: delegate)
default: default:
continue continue
} }
@ -254,54 +260,7 @@ public typealias ActionBlock = () -> Void
} }
} }
/// 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 {
if (delegateObject as? MVMCoreUIDelegateObject)?.buttonDelegate?.button?(self, shouldPerformActionWithMap: actionMap, additionalData: additionalData) ?? true {
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegateObject: delegateObject)
}
}
}
private func addActionableClause(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)))
}
private func addActionableClause(range: NSRange, actionBlock: ActionBlock?) {
setDefaultAttributes(range: range)
clauses.append(ActionableClause(range: range, actionBlock: actionBlock))
}
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
}
//------------------------------------------------------ //------------------------------------------------------
// MARK: - Methods // MARK: - Methods
@ -401,6 +360,39 @@ extension Label {
// MARK: - Multi-Action Functionality // 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 {
if (delegateObject as? MVMCoreUIDelegateObject)?.buttonDelegate?.button?(self, 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. Provides an actionable range of text.
@ -411,8 +403,8 @@ extension Label {
*/ */
@objc public func addTappableLinkAttribute(range: NSRange, actionBlock: @escaping ActionBlock) { @objc public func addTappableLinkAttribute(range: NSRange, actionBlock: @escaping ActionBlock) {
addActionableClause(range: range, setDefaultAttributes(range: range)
actionBlock: actionBlock) clauses.append(ActionableClause(range: range, actionBlock: actionBlock))
} }
/** /**
@ -427,10 +419,11 @@ extension Label {
*/ */
@objc public func addTappableLinkAttribute(range: NSRange, actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) { @objc public func addTappableLinkAttribute(range: NSRange, actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) {
addActionableClause(range: range, setDefaultAttributes(range: range)
actionMap: actionMap, clauses.append(ActionableClause(range: range,
additionalData: additionalData, actionBlock: createActionBlockFrom(actionMap: actionMap,
delegateObject: delegateObject) additionalData: additionalData,
delegateObject: delegateObject)))
} }
@objc private func textLinkTapped(_ gesture: UITapGestureRecognizer) { @objc private func textLinkTapped(_ gesture: UITapGestureRecognizer) {
@ -449,6 +442,10 @@ extension UITapGestureRecognizer {
func didTapAttributedTextInLabel(_ label: Label, inRange targetRange: NSRange) -> Bool { func didTapAttributedTextInLabel(_ label: Label, inRange targetRange: NSRange) -> Bool {
if label.makeWholeViewClickable {
return true
}
guard let attributedText = label.attributedText else { return false } guard let attributedText = label.attributedText else { return false }
let layoutManager = NSLayoutManager() let layoutManager = NSLayoutManager()
@ -464,11 +461,7 @@ extension UITapGestureRecognizer {
textContainer.size = label.bounds.size textContainer.size = label.bounds.size
let indexOfCharacter = layoutManager.characterIndex(for: location(in: label), in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil) let indexOfCharacter = layoutManager.characterIndex(for: location(in: label), in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
if label.makeWholeViewClickable {
return true
}
return NSLocationInRange(indexOfCharacter, targetRange) return NSLocationInRange(indexOfCharacter, targetRange)
} }
} }

View File

@ -61,7 +61,7 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
public var actionBlock: ActionBlock? { public var actionBlock: ActionBlock? {
willSet(newActionBlock) { willSet(newActionBlock) {
if newActionBlock == nil { if newActionBlock == nil {
label?.clauses = [] label?.clearActionableClauses()
} else { } else {
label?.clauses = [Label.ActionableClause(range: actionRange, actionBlock: newActionBlock)] label?.clauses = [Label.ActionableClause(range: actionRange, actionBlock: newActionBlock)]
} }
@ -81,15 +81,11 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
} }
public var makeWholeViewClickable = false { public var makeWholeViewClickable = false {
willSet(newBool) { willSet(newBool) { label?.makeWholeViewClickable = 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?
@ -133,7 +129,7 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
setLabelAttributes() setLabelAttributes()
} }
// 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)
@ -141,8 +137,8 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
self.frontText = frontText self.frontText = frontText
self.actionText = actionText self.actionText = actionText
self.backText = backText self.backText = backText
actionBlock = block
text = getTextFromStringComponents() text = getTextFromStringComponents()
actionBlock = block
setLabelAttributes() setLabelAttributes()
} }
@ -181,7 +177,6 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
if self.label == nil { if self.label == nil {
let label = Label(frame: .zero) let label = Label(frame: .zero)
label.isUserInteractionEnabled = true label.isUserInteractionEnabled = true
label.setContentCompressionResistancePriority(.required, for: .vertical) label.setContentCompressionResistancePriority(.required, for: .vertical)
addSubview(label) addSubview(label)
@ -215,10 +210,10 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
createLabel() 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()
setActionMap(actionMap, additionalData: additionalData, delegateObject: delegateObject)
setLabelAttributes() setLabelAttributes()
} }
@ -307,7 +302,6 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
} }
} }
} }
self.text = getTextFromStringComponents() self.text = getTextFromStringComponents()
setLabelAttributes() setLabelAttributes()
} }
@ -374,8 +368,8 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
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 = label?.createActionBlockFrom(actionMap: actionMap, additionalData: additionalData, delegateObject: delegateObject)
text = getTextFromStringComponents() text = getTextFromStringComponents()
actionBlock = label?.createActionBlockFrom(actionMap: actionMap, additionalData: additionalData, delegateObject: delegateObject)
setLabelAttributes() setLabelAttributes()
} }
@ -578,22 +572,21 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
@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() 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)
text = getTextFromStringComponents()
actionBlock = { [weak delegate] in actionBlock = { [weak delegate] in
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegate: delegate as? CoreObjectActionLoadPresentDelegate) MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegate: delegate as? CoreObjectActionLoadPresentDelegate)
} }
text = getTextFromStringComponents()
setLabelAttributes() setLabelAttributes()
} }
} }
// MARK: - Atomization // MARK: - Atomization
extension LabelWithInternalButton: MVMCoreUIMoleculeViewProtocol { extension LabelWithInternalButton: MVMCoreUIMoleculeViewProtocol {
// Default values for view. // Default values for view.
@objc open func setAsMolecule() { @objc open func setAsMolecule() {