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
//------------------------------------------------------
/// H1 -> HeadlineLarge
@objc public static func commonLabelH1(_ scale: Bool) -> Label {
let label = Label.label()
label.styleH1(scale)
return label
}
/// H2 -> Headline
@objc public static func commonLabelH2(_ scale: Bool) -> Label {
let label = Label.label()
label.styleH2(scale)
return label
}
/// H3 -> SubHead
@objc public static func commonLabelH3(_ scale: Bool) -> Label {
let label = Label.label()
label.styleH3(scale)
@ -116,18 +119,21 @@ public typealias ActionBlock = () -> Void
return label
}
/// B1 -> SubTitle
@objc public static func commonLabelB1(_ scale: Bool) -> Label {
let label = Label.label()
label.styleB1(scale)
return label
}
/// B2 -> Body
@objc public static func commonLabelB2(_ scale: Bool) -> Label {
let label = Label.label()
label.styleB2(scale)
return label
}
/// B3 -> Legal
@objc public static func commonLabelB3(_ scale: Bool) -> Label {
let label = Label.label()
label.styleB3(scale)
@ -242,10 +248,10 @@ public typealias ActionBlock = () -> Void
case "action":
guard let actionLabel = label as? Label else { continue }
actionLabel.addActionableClause(range: range,
actionMap: json,
additionalData: additionalData,
delegateObject: delegate)
actionLabel.addTappableLinkAttribute(range: range,
actionMap: json,
additionalData: additionalData,
delegateObject: delegate)
default:
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
@ -401,6 +360,39 @@ extension Label {
// MARK: - Multi-Action Functionality
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.
@ -411,8 +403,8 @@ extension Label {
*/
@objc public func addTappableLinkAttribute(range: NSRange, actionBlock: @escaping ActionBlock) {
addActionableClause(range: range,
actionBlock: actionBlock)
setDefaultAttributes(range: range)
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?) {
addActionableClause(range: range,
actionMap: actionMap,
additionalData: additionalData,
delegateObject: delegateObject)
setDefaultAttributes(range: range)
clauses.append(ActionableClause(range: range,
actionBlock: createActionBlockFrom(actionMap: actionMap,
additionalData: additionalData,
delegateObject: delegateObject)))
}
@objc private func textLinkTapped(_ gesture: UITapGestureRecognizer) {
@ -449,6 +442,10 @@ 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()
@ -464,11 +461,7 @@ extension UITapGestureRecognizer {
textContainer.size = label.bounds.size
let indexOfCharacter = layoutManager.characterIndex(for: location(in: label), in: textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
if label.makeWholeViewClickable {
return true
}
return NSLocationInRange(indexOfCharacter, targetRange)
}
}

View File

@ -61,7 +61,7 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
public var actionBlock: ActionBlock? {
willSet(newActionBlock) {
if newActionBlock == nil {
label?.clauses = []
label?.clearActionableClauses()
} else {
label?.clauses = [Label.ActionableClause(range: actionRange, actionBlock: newActionBlock)]
}
@ -81,15 +81,11 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
}
public var makeWholeViewClickable = false {
willSet(newBool) {
label?.makeWholeViewClickable = newBool
}
willSet(newBool) { label?.makeWholeViewClickable = newBool }
}
override open var isEnabled: Bool {
didSet {
alpha = isEnabled ? 1 : DisableOppacity
}
didSet { alpha = isEnabled ? 1 : DisableOppacity }
}
public var frontText: String?
@ -133,7 +129,7 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
setLabelAttributes()
}
// legacy
// Legacy
public init(frontText: String?, actionText: String?, backText: String?, actionBlock block: ActionBlock?) {
super.init(frame: .zero)
@ -141,8 +137,8 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
self.frontText = frontText
self.actionText = actionText
self.backText = backText
actionBlock = block
text = getTextFromStringComponents()
actionBlock = block
setLabelAttributes()
}
@ -181,7 +177,6 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
if self.label == nil {
let label = Label(frame: .zero)
label.isUserInteractionEnabled = true
label.setContentCompressionResistancePriority(.required, for: .vertical)
addSubview(label)
@ -215,10 +210,10 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
createLabel()
self.frontText = frontText
setActionMap(actionMap, additionalData: additionalData, delegateObject: delegateObject)
self.actionText = actionText
self.backText = backText
text = getTextFromStringComponents()
setActionMap(actionMap, additionalData: additionalData, delegateObject: delegateObject)
setLabelAttributes()
}
@ -307,7 +302,6 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
}
}
}
self.text = getTextFromStringComponents()
setLabelAttributes()
}
@ -374,8 +368,8 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
frontText = actionMap?.optionalStringForKey(KeyTitlePrefix)
actionText = actionMap?.optionalStringForKey(KeyTitle)
backText = actionMap?.optionalStringForKey(KeyTitlePostfix)
actionBlock = label?.createActionBlockFrom(actionMap: actionMap, additionalData: additionalData, delegateObject: delegateObject)
text = getTextFromStringComponents()
actionBlock = label?.createActionBlockFrom(actionMap: actionMap, additionalData: additionalData, delegateObject: delegateObject)
setLabelAttributes()
}
@ -578,22 +572,21 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
@objc public func reset(withActionMap actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegate: ActionObjectDelegate?) {
createLabel()
frontText = actionMap?.optionalStringForKey(KeyTitlePrefix)
actionText = actionMap?.optionalStringForKey(KeyTitle)
backText = actionMap?.optionalStringForKey(KeyTitlePostfix)
text = getTextFromStringComponents()
actionBlock = { [weak delegate] in
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegate: delegate as? CoreObjectActionLoadPresentDelegate)
}
text = getTextFromStringComponents()
setLabelAttributes()
}
}
// MARK: - Atomization
extension LabelWithInternalButton: MVMCoreUIMoleculeViewProtocol {
// Default values for view.
@objc open func setAsMolecule() {