Further swapping of didSet/willSet.

This commit is contained in:
Christiano, Kevin 2019-04-01 11:44:25 -04:00
parent f68c355527
commit c5233a680d

View File

@ -25,7 +25,7 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
public weak var label: MFLabel? public weak var label: MFLabel?
public var attributedText: NSAttributedString? { public var attributedText: NSAttributedString? {
didSet(newAttributedText) { willSet(newAttributedText) {
if let newAttribText = newAttributedText, !newAttribText.string.isEmpty { if let newAttribText = newAttributedText, !newAttribText.string.isEmpty {
let mutableAttributedText = newAttribText as? NSMutableAttributedString let mutableAttributedText = newAttribText as? NSMutableAttributedString
let paragraphStyle = NSMutableParagraphStyle() 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 // By default, it will follow most of the font standard in zepplin, and should need to be changed
public var normalTextFont: UIFont? = MFStyler.fontB2() { public var normalTextFont: UIFont? = MFStyler.fontB2() {
didSet(newNormalFont) { willSet(newNormalFont) {
setAlternateNormalTextAttributes([NSAttributedString.Key.font: newNormalFont as Any]) setAlternateNormalTextAttributes([NSAttributedString.Key.font: newNormalFont as Any])
} }
} }
public var actionTextFont: UIFont? = MFStyler.fontB2() { public var actionTextFont: UIFont? = MFStyler.fontB2() {
didSet(newActionFont) { willSet(newActionFont) {
setAlternateActionTextAttributes([NSAttributedString.Key.font: newActionFont as Any]) setAlternateActionTextAttributes([NSAttributedString.Key.font: newActionFont as Any])
} }
} }
public var normalTextColor: UIColor = .black { public var normalTextColor: UIColor = .black {
didSet(newNormalColor) { willSet(newNormalColor) {
setAlternateNormalTextAttributes([NSAttributedString.Key.foregroundColor: newNormalColor as Any]) setAlternateNormalTextAttributes([NSAttributedString.Key.foregroundColor: newNormalColor as Any])
} }
} }
public var actionTextColor: UIColor = .black { public var actionTextColor: UIColor = .black {
didSet(newActionColor) { willSet(newActionColor) {
setAlternateActionTextAttributes([NSAttributedString.Key.foregroundColor: newActionColor as Any]) setAlternateActionTextAttributes([NSAttributedString.Key.foregroundColor: newActionColor as Any])
} }
} }
@ -81,13 +81,11 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
public var text: String? { public var text: String? {
willSet(newText) { willSet(newText) {
// if newText != nil { attributedText = NSAttributedString(string: newText ?? "")
attributedText = NSAttributedString(string: newText ?? "") setAlternateNormalTextAttributes([NSAttributedString.Key.font: normalTextFont as Any])
setAlternateNormalTextAttributes([NSAttributedString.Key.font: normalTextFont as Any]) setAlternateActionTextAttributes([NSAttributedString.Key.font: actionTextFont as Any])
setAlternateActionTextAttributes([NSAttributedString.Key.font: actionTextFont as Any]) setAlternateNormalTextAttributes([NSAttributedString.Key.foregroundColor: normalTextColor as Any])
setAlternateNormalTextAttributes([NSAttributedString.Key.foregroundColor: normalTextColor as Any]) setAlternateActionTextAttributes([NSAttributedString.Key.foregroundColor: actionTextColor 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?) { 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: CGRect.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)
} }
// MARK: - legacy // MARK: - 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: CGRect.zero) super.init(frame: CGRect.zero)
self.frontText = frontText self.frontText = frontText
self.actionText = actionText self.actionText = actionText
self.backText = backText 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?) { 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) 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?) { 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) 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?) { 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) 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 {}. // 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?) { 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) 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?) { @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) 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 attributedText = mutableAttributedString
//added this line for underlining // Added this line for underlining
setAlternateActionTextAttributes([NSAttributedString.Key.underlineStyle: NSNumber(value: NSUnderlineStyle.single.rawValue)]) setAlternateActionTextAttributes([NSAttributedString.Key.underlineStyle: NSNumber(value: NSUnderlineStyle.single.rawValue)])
} }
@ -337,7 +330,7 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
result = true result = true
break break
} else if wordRect.origin.x == 0 && wordRect.origin.y == 0 && wordRect.size.height == 0 && wordRect.size.width == 0 { } 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 result = true
break break
} }
@ -424,7 +417,7 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
self.text = actionRange.revisedText self.text = actionRange.revisedText
setup() setup()
} }
private func rangeOfText(_ text: String?, startTag: String?, endTag: String?) -> ActionIndiciesTuple { private func rangeOfText(_ text: String?, startTag: String?, endTag: String?) -> ActionIndiciesTuple {
var fullText = text ?? "" var fullText = text ?? ""
@ -484,25 +477,25 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
@objc public func setAlternateActionTextAttributes(_ attributes: [AnyHashable: Any]?) { @objc public func setAlternateActionTextAttributes(_ attributes: [AnyHashable: Any]?) {
if let theseAttributes = attributes as? [NSAttributedString.Key: Any], let thisAttributedText = attributedText { guard let theseAttributes = attributes as? [NSAttributedString.Key: Any], let thisAttributedText = attributedText else { return }
let attributedString = NSMutableAttributedString(attributedString: thisAttributedText) let attributedString = NSMutableAttributedString(attributedString: thisAttributedText)
if !attributedString.string.isEmpty {
attributedString.addAttributes(theseAttributes, range: getActionRange()) if !attributedString.string.isEmpty {
} attributedString.addAttributes(theseAttributes, range: getActionRange())
attributedText = attributedString
} }
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] { 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 {
attributedString.addAttributes(_attributes, range: getFrontRange()) if !attributedString.string.isEmpty {
attributedString.addAttributes(_attributes, range: getBackRange()) attributedString.addAttributes(_attributes, range: getFrontRange())
} attributedString.addAttributes(_attributes, range: getBackRange())
attributedText = attributedString
} }
attributedText = attributedString
} }
private func getFrontRange() -> NSRange { private func getFrontRange() -> NSRange {
@ -565,8 +558,7 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
@objc public func accessibilityCustomAction() -> UIAccessibilityCustomAction? { @objc public func accessibilityCustomAction() -> UIAccessibilityCustomAction? {
if let actionTxt = actionText, !actionTxt.isEmpty { if let actionTxt = actionText, !actionTxt.isEmpty {
let name = actionTxt return UIAccessibilityCustomAction(name: actionTxt, target: self, selector: #selector(LabelWithInternalButton.accessibilityCustomActions))
return UIAccessibilityCustomAction(name: name, target: self, selector: #selector(LabelWithInternalButton.accessibilityCustomActions))
} }
return UIAccessibilityCustomAction() return UIAccessibilityCustomAction()
@ -624,27 +616,11 @@ extension LabelWithInternalButton: MVMCoreUIMoleculeViewProtocol {
self.actionText = actionText 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 { if let text = dictionary["text"] as? String {
self.text = text 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
}
*/