From c5233a680d8c5e97f6fcfab4a5c7657a7c31c5e0 Mon Sep 17 00:00:00 2001 From: "Christiano, Kevin" Date: Mon, 1 Apr 2019 11:44:25 -0400 Subject: [PATCH] Further swapping of didSet/willSet. --- .../Atoms/Views/LabelWithInternalButton.swift | 90 +++++++------------ 1 file changed, 33 insertions(+), 57 deletions(-) diff --git a/MVMCoreUI/Atoms/Views/LabelWithInternalButton.swift b/MVMCoreUI/Atoms/Views/LabelWithInternalButton.swift index 46f64280..ddbff108 100644 --- a/MVMCoreUI/Atoms/Views/LabelWithInternalButton.swift +++ b/MVMCoreUI/Atoms/Views/LabelWithInternalButton.swift @@ -25,7 +25,7 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt public weak var label: MFLabel? public var attributedText: NSAttributedString? { - didSet(newAttributedText) { + willSet(newAttributedText) { if let newAttribText = newAttributedText, !newAttribText.string.isEmpty { let mutableAttributedText = newAttribText as? NSMutableAttributedString let paragraphStyle = NSMutableParagraphStyle() @@ -44,25 +44,25 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt // By default, it will follow most of the font standard in zepplin, and should need to be changed public var normalTextFont: UIFont? = MFStyler.fontB2() { - didSet(newNormalFont) { + willSet(newNormalFont) { setAlternateNormalTextAttributes([NSAttributedString.Key.font: newNormalFont as Any]) } } public var actionTextFont: UIFont? = MFStyler.fontB2() { - didSet(newActionFont) { + willSet(newActionFont) { setAlternateActionTextAttributes([NSAttributedString.Key.font: newActionFont as Any]) } } public var normalTextColor: UIColor = .black { - didSet(newNormalColor) { + willSet(newNormalColor) { setAlternateNormalTextAttributes([NSAttributedString.Key.foregroundColor: newNormalColor as Any]) } } public var actionTextColor: UIColor = .black { - didSet(newActionColor) { + willSet(newActionColor) { setAlternateActionTextAttributes([NSAttributedString.Key.foregroundColor: newActionColor as Any]) } } @@ -81,13 +81,11 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt public var text: String? { willSet(newText) { -// if newText != nil { - attributedText = NSAttributedString(string: newText ?? "") - setAlternateNormalTextAttributes([NSAttributedString.Key.font: normalTextFont as Any]) - setAlternateActionTextAttributes([NSAttributedString.Key.font: actionTextFont as Any]) - setAlternateNormalTextAttributes([NSAttributedString.Key.foregroundColor: normalTextColor as Any]) - setAlternateActionTextAttributes([NSAttributedString.Key.foregroundColor: actionTextColor as Any]) -// } + attributedText = NSAttributedString(string: newText ?? "") + setAlternateNormalTextAttributes([NSAttributedString.Key.font: normalTextFont as Any]) + setAlternateActionTextAttributes([NSAttributedString.Key.font: actionTextFont as Any]) + setAlternateNormalTextAttributes([NSAttributedString.Key.foregroundColor: normalTextColor as Any]) + setAlternateActionTextAttributes([NSAttributedString.Key.foregroundColor: actionTextColor as Any]) } } @@ -111,15 +109,15 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt } public init(frontText: String?, actionText: String?, backText: String?, actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, actionDelegate delegate: ActionObjectDelegate?, buttonDelegate: ButtonObjectDelegate?) { - super.init(frame: CGRect.zero) + setFrontText(frontText, actionText: actionText, actionMap: actionMap, backText: backText, additionalData: additionalData, delegate: delegate, buttonDelegate: buttonDelegate) } // MARK: - legacy public init(frontText: String?, actionText: String?, backText: String?, actionBlock block: ActionBlock?) { - super.init(frame: CGRect.zero) + self.frontText = frontText self.actionText = actionText self.backText = backText @@ -139,23 +137,19 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt } public convenience init(frontText: String?, actionText: String?, backText: String?, actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, actionDelegate delegate: ActionObjectDelegate?) { - self.init(frontText: frontText, actionText: actionText, backText: backText, actionMap: actionMap, additionalData: additionalData, actionDelegate: delegate, buttonDelegate: nil) } public convenience init(actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, actionDelegate delegate: ActionObjectDelegate?) { - self.init(frontText: actionMap?.optionalStringForKey(KeyTitlePrefix), actionText: actionMap?.optionalStringForKey(KeyTitle), backText: actionMap?.optionalStringForKey(KeyTitlePostfix), actionMap: actionMap, additionalData: additionalData, actionDelegate: delegate) } public convenience init(frontText: String?, backText: String?, actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, actionDelegate delegate: ActionObjectDelegate?) { - self.init(frontText: frontText, actionText: actionMap?.optionalStringForKey(KeyTitle), backText: backText, actionMap: actionMap, additionalData: additionalData, actionDelegate: delegate) } // 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]?, actionDelegate delegate: ActionObjectDelegate?) { - self.init(text: fullText, startTag: "{", endTag: "}", actionMap: actionMap, additionalData: additionalData, actionDelegate: delegate) } @@ -226,7 +220,6 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt //------------------------------------------------------ @objc public func setFrontText(_ frontText: String?, actionMap: [AnyHashable: Any]?, backText: String?, additionalData: [AnyHashable: Any]?, delegate: ActionObjectDelegate?, buttonDelegate: ButtonObjectDelegate?) { - setFrontText(frontText, actionText: actionMap?.optionalStringForKey(KeyTitle), actionMap: actionMap, backText: backText, additionalData: additionalData, delegate: delegate, buttonDelegate: buttonDelegate) } @@ -289,7 +282,7 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt attributedText = mutableAttributedString - //added this line for underlining + // Added this line for underlining setAlternateActionTextAttributes([NSAttributedString.Key.underlineStyle: NSNumber(value: NSUnderlineStyle.single.rawValue)]) } @@ -337,7 +330,7 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt 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. + // 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 } @@ -424,7 +417,7 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt self.text = actionRange.revisedText setup() } - + private func rangeOfText(_ text: String?, startTag: String?, endTag: String?) -> ActionIndiciesTuple { var fullText = text ?? "" @@ -484,25 +477,25 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt @objc public func setAlternateActionTextAttributes(_ attributes: [AnyHashable: Any]?) { - if let theseAttributes = attributes as? [NSAttributedString.Key: Any], let thisAttributedText = attributedText { - let attributedString = NSMutableAttributedString(attributedString: thisAttributedText) - if !attributedString.string.isEmpty { - attributedString.addAttributes(theseAttributes, range: getActionRange()) - } - attributedText = attributedString + guard let theseAttributes = attributes as? [NSAttributedString.Key: Any], let thisAttributedText = attributedText else { return } + let attributedString = NSMutableAttributedString(attributedString: thisAttributedText) + + if !attributedString.string.isEmpty { + attributedString.addAttributes(theseAttributes, range: getActionRange()) } + attributedText = attributedString } - @objc public func setAlternateNormalTextAttributes(_ attributes: [AnyHashable: Any]?) { + public func setAlternateNormalTextAttributes(_ attributes: [AnyHashable: Any]?) { - if let _attributedText = attributedText, let _attributes = attributes as? [NSAttributedString.Key: Any] { - let attributedString = NSMutableAttributedString(attributedString: _attributedText) - if !attributedString.string.isEmpty { - attributedString.addAttributes(_attributes, range: getFrontRange()) - attributedString.addAttributes(_attributes, range: getBackRange()) - } - attributedText = attributedString + guard let _attributedText = attributedText, let _attributes = attributes as? [NSAttributedString.Key: Any] else { return } + let attributedString = NSMutableAttributedString(attributedString: _attributedText) + + if !attributedString.string.isEmpty { + attributedString.addAttributes(_attributes, range: getFrontRange()) + attributedString.addAttributes(_attributes, range: getBackRange()) } + attributedText = attributedString } private func getFrontRange() -> NSRange { @@ -565,8 +558,7 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt @objc public func accessibilityCustomAction() -> UIAccessibilityCustomAction? { if let actionTxt = actionText, !actionTxt.isEmpty { - let name = actionTxt - return UIAccessibilityCustomAction(name: name, target: self, selector: #selector(LabelWithInternalButton.accessibilityCustomActions)) + return UIAccessibilityCustomAction(name: actionTxt, target: self, selector: #selector(LabelWithInternalButton.accessibilityCustomActions)) } return UIAccessibilityCustomAction() @@ -624,27 +616,11 @@ extension LabelWithInternalButton: MVMCoreUIMoleculeViewProtocol { self.actionText = actionText } - // Want this to be last because it has a didSet feature. + //public var actionBlock: ActionBlock? + + // Want this to be last because it has a willSet feature. if let text = dictionary["text"] as? String { self.text = text } } } - - -/* - public var actionBlock: ActionBlock? - - public var alternateAttributeForActionText: String? - - let mutableAttributedText = newAttributedText as? NSMutableAttributedString - let paragraphStyle = NSMutableParagraphStyle() - paragraphStyle.lineSpacing = CGFloat(LabelWithInternalButtonLineSpace) - mutableAttributedText?.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: newAttributedText?.length ?? 0)) - - - override open var isEnabled: Bool { - didSet { - alpha = isEnabled ? 1 : DisableOppacity - } - */