LabelWithInternalButton is in a functional place. Further work required for Label.
This commit is contained in:
parent
f6f2cd2068
commit
2a05328f1a
@ -11,7 +11,7 @@ import MVMCore
|
|||||||
|
|
||||||
public typealias ActionBlock = () -> Void
|
public typealias ActionBlock = () -> Void
|
||||||
|
|
||||||
@objc open class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol {
|
@objcMembers open class Label: UILabel, MVMCoreViewProtocol, MVMCoreUIMoleculeViewProtocol {
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
// MARK: - Properties
|
// MARK: - Properties
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
@ -23,17 +23,17 @@ public typealias ActionBlock = () -> Void
|
|||||||
var makeWholeViewClickable = false
|
var makeWholeViewClickable = false
|
||||||
|
|
||||||
// Set this property if you want updateView to update the font based on this standard and the size passed in.
|
// Set this property if you want updateView to update the font based on this standard and the size passed in.
|
||||||
@objc public var standardFontSize: CGFloat = 0.0
|
public var standardFontSize: CGFloat = 0.0
|
||||||
|
|
||||||
// Set this to use a custom sizing object during updateView instead of the standard.
|
// Set this to use a custom sizing object during updateView instead of the standard.
|
||||||
@objc public var sizeObject: MFSizeObject?
|
public var sizeObject: MFSizeObject?
|
||||||
|
|
||||||
@objc public var scaleSize: NSNumber?
|
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
|
||||||
}
|
}
|
||||||
@ -151,7 +151,7 @@ public typealias ActionBlock = () -> Void
|
|||||||
guard let label = label else { return }
|
guard let label = label else { return }
|
||||||
|
|
||||||
label.text = json?.optionalStringForKey(KeyText)
|
label.text = json?.optionalStringForKey(KeyText)
|
||||||
|
|
||||||
setLabel(label, withHTML: json?.optionalStringForKey("html"))
|
setLabel(label, withHTML: json?.optionalStringForKey("html"))
|
||||||
|
|
||||||
if let textColorHex = json?.optionalStringForKey(KeyTextColor), !textColorHex.isEmpty {
|
if let textColorHex = json?.optionalStringForKey(KeyTextColor), !textColorHex.isEmpty {
|
||||||
@ -176,21 +176,21 @@ public typealias ActionBlock = () -> Void
|
|||||||
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
|
||||||
else { continue }
|
else { continue }
|
||||||
|
|
||||||
let range = NSRange(location: location, length: length)
|
let range = NSRange(location: location, length: length)
|
||||||
|
|
||||||
switch attributeType {
|
switch attributeType {
|
||||||
case "underline":
|
case "underline":
|
||||||
attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: range)
|
attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: range)
|
||||||
|
|
||||||
case "strikethrough":
|
case "strikethrough":
|
||||||
attributedString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.thick.rawValue, range: range)
|
attributedString.addAttribute(.strikethroughStyle, value: NSUnderlineStyle.thick.rawValue, range: range)
|
||||||
|
|
||||||
case "color":
|
case "color":
|
||||||
if let colorHex = attribute.optionalStringForKey(KeyTextColor), !colorHex.isEmpty {
|
if let colorHex = attribute.optionalStringForKey(KeyTextColor), !colorHex.isEmpty {
|
||||||
attributedString.removeAttribute(.foregroundColor, range: range)
|
attributedString.removeAttribute(.foregroundColor, range: range)
|
||||||
@ -199,13 +199,13 @@ public typealias ActionBlock = () -> Void
|
|||||||
case "font":
|
case "font":
|
||||||
let fontSize = attribute["size"] as? CGFloat
|
let fontSize = attribute["size"] as? CGFloat
|
||||||
var font: UIFont?
|
var font: UIFont?
|
||||||
|
|
||||||
if let fontName = attribute.optionalStringForKey("name") {
|
if let fontName = attribute.optionalStringForKey("name") {
|
||||||
font = MFFonts.mfFont(withName: fontName, size: fontSize ?? label.font.pointSize)
|
font = MFFonts.mfFont(withName: fontName, size: fontSize ?? label.font.pointSize)
|
||||||
} else if let fontSize = fontSize {
|
} else if let fontSize = fontSize {
|
||||||
font = label.font.withSize(fontSize)
|
font = label.font.withSize(fontSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
if let font = font {
|
if let font = font {
|
||||||
attributedString.removeAttribute(.font, range: range)
|
attributedString.removeAttribute(.font, range: range)
|
||||||
attributedString.addAttribute(.font, value: font, range: range)
|
attributedString.addAttribute(.font, value: font, range: range)
|
||||||
|
|||||||
@ -9,7 +9,6 @@
|
|||||||
|
|
||||||
import MVMCore
|
import MVMCore
|
||||||
|
|
||||||
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
|
||||||
@ -24,7 +23,7 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
|
|||||||
|
|
||||||
public var attributedText: NSAttributedString? {
|
public var attributedText: NSAttributedString? {
|
||||||
willSet(newAttributedText) {
|
willSet(newAttributedText) {
|
||||||
if let newAttribText = newAttributedText, !newAttribText.string.isEmpty {
|
if let newAttribText = newAttributedText {
|
||||||
|
|
||||||
let mutableAttributedText = NSMutableAttributedString(attributedString: newAttribText)
|
let mutableAttributedText = NSMutableAttributedString(attributedString: newAttribText)
|
||||||
let paragraphStyle = NSMutableParagraphStyle()
|
let paragraphStyle = NSMutableParagraphStyle()
|
||||||
@ -60,6 +59,12 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public var actionBlock: ActionBlock? {
|
||||||
|
willSet(newActionBlock) {
|
||||||
|
label?.actionBlock = newActionBlock
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
@ -81,6 +86,7 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
|
|||||||
label?.frontText = newFrontText
|
label?.frontText = newFrontText
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public var actionText: String? {
|
public var actionText: String? {
|
||||||
willSet(newActionText) {
|
willSet(newActionText) {
|
||||||
label?.actionText = newActionText
|
label?.actionText = newActionText
|
||||||
@ -89,15 +95,14 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
|
|||||||
|
|
||||||
public var backText: String?
|
public var backText: String?
|
||||||
|
|
||||||
public var actionBlock: ActionBlock? {
|
private var internalText: String = ""
|
||||||
willSet(newActionBlock) {
|
|
||||||
label?.actionBlock = newActionBlock
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private var text: String? {
|
public var text: String? {
|
||||||
willSet(newText) {
|
get { return internalText }
|
||||||
attributedText = NSAttributedString(string: newText ?? "")
|
set {
|
||||||
|
guard let text = newValue else { return }
|
||||||
|
internalText = text
|
||||||
|
attributedText = NSAttributedString(string: text)
|
||||||
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])
|
||||||
@ -111,29 +116,33 @@ 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
|
actionBlock = block
|
||||||
text = getTextFromStringComponents()
|
text = getTextFromStringComponents()
|
||||||
setup()
|
setLabelAttributes()
|
||||||
}
|
}
|
||||||
|
|
||||||
public convenience init(actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) {
|
public convenience init(actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) {
|
||||||
@ -145,32 +154,22 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
|
|||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
|
|
||||||
@ -183,7 +182,8 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
|
|||||||
// 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: .zero)
|
let label = Label(frame: .zero)
|
||||||
@ -191,12 +191,15 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
|
|||||||
label.isUserInteractionEnabled = true
|
label.isUserInteractionEnabled = true
|
||||||
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)])
|
||||||
@ -204,16 +207,15 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
|
|||||||
self.label?.attributedText = attributedText
|
self.label?.attributedText = attributedText
|
||||||
self.label?.accessibilityTraits = .button
|
self.label?.accessibilityTraits = .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 = { [weak self, weak delegateObject] in
|
||||||
weak var weakButtonDelegate: ButtonDelegateProtocol? = (delegateObject as? MVMCoreUIDelegateObject)?.buttonDelegate
|
|
||||||
|
|
||||||
actionBlock = {
|
|
||||||
var performAction = true
|
var performAction = true
|
||||||
|
|
||||||
if let wSelf = weakSelf, let wButtonDelegate = weakButtonDelegate, wButtonDelegate.responds(to: #selector(ButtonObjectDelegate.button(_:shouldPerformActionWithMap:additionalData:))) {
|
if let wSelf = self,
|
||||||
|
let wButtonDelegate = (delegateObject as? MVMCoreUIDelegateObject)?.buttonDelegate,
|
||||||
|
wButtonDelegate.responds(to: #selector(ButtonObjectDelegate.button(_:shouldPerformActionWithMap:additionalData:))) {
|
||||||
performAction = wButtonDelegate.button?(wSelf, shouldPerformActionWithMap: actionMap, additionalData: additionalData) ?? false
|
performAction = wButtonDelegate.button?(wSelf, shouldPerformActionWithMap: actionMap, additionalData: additionalData) ?? false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -229,12 +231,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)
|
setActionMap(actionMap, additionalData: additionalData, delegateObject: delegateObject)
|
||||||
self.actionText = actionText
|
self.actionText = actionText
|
||||||
self.backText = backText
|
self.backText = backText
|
||||||
text = getTextFromStringComponents()
|
text = getTextFromStringComponents()
|
||||||
setup()
|
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?) {
|
||||||
@ -283,14 +286,6 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
|
|||||||
label?.attributedText = attributedText
|
label?.attributedText = attributedText
|
||||||
}
|
}
|
||||||
|
|
||||||
//------------------------------------------------------
|
|
||||||
// MARK: - UIControl
|
|
||||||
//------------------------------------------------------
|
|
||||||
|
|
||||||
// override open func sendAction(_ action: Selector, to target: Any?, for event: UIEvent?) {
|
|
||||||
// <#code#>
|
|
||||||
// }
|
|
||||||
|
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
// MARK: - Helper
|
// MARK: - Helper
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
@ -315,45 +310,25 @@ 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, text.contains(leftTag),
|
var initialSegments = [String]()
|
||||||
let rightTag = endTag, text.contains(rightTag),
|
if let leftTag = startTag, text.contains(leftTag) {
|
||||||
let front = actionableTuple.front,
|
initialSegments = text.components(separatedBy: leftTag)
|
||||||
let middle = actionableTuple.action,
|
frontText = initialSegments[0].trimmingCharacters(in: .whitespaces)
|
||||||
let end = actionableTuple.end {
|
|
||||||
|
if let rightTag = endTag, text.contains(rightTag) {
|
||||||
frontText = front.trimmingCharacters(in: .whitespaces)
|
let secondPart = initialSegments[1].components(separatedBy: rightTag)
|
||||||
actionText = middle.trimmingCharacters(in: .whitespaces)
|
actionText = secondPart[0].trimmingCharacters(in: .whitespaces)
|
||||||
backText = end.trimmingCharacters(in: .whitespaces)
|
backText = secondPart[1].trimmingCharacters(in: .whitespaces)
|
||||||
} else {
|
}
|
||||||
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]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return actionableTuple
|
self.text = getTextFromStringComponents()
|
||||||
|
setLabelAttributes()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset the text and action map
|
// Reset the text and action map
|
||||||
@ -418,14 +393,16 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
|
|||||||
|
|
||||||
@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)
|
||||||
@ -435,7 +412,7 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
|
|||||||
}
|
}
|
||||||
|
|
||||||
text = getTextFromStringComponents()
|
text = getTextFromStringComponents()
|
||||||
setup()
|
setLabelAttributes()
|
||||||
}
|
}
|
||||||
|
|
||||||
//------------------------------------------------------
|
//------------------------------------------------------
|
||||||
@ -473,7 +450,7 @@ 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)
|
||||||
}
|
}
|
||||||
@ -517,29 +494,23 @@ public typealias CoreObjectActionLoadPresentDelegate = MVMCoreActionDelegateProt
|
|||||||
|
|
||||||
setText(fullText, startTag: startTag, endTag: endTag)
|
setText(fullText, startTag: startTag, endTag: endTag)
|
||||||
|
|
||||||
weak var weakDelegate: ActionObjectDelegate? = delegate
|
|
||||||
|
|
||||||
actionBlock = {
|
actionBlock = {
|
||||||
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegate: weakDelegate as? CoreObjectActionLoadPresentDelegate)
|
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegate: delegate 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] in
|
||||||
weak var weakDelegate: ActionObjectDelegate? = delegate
|
|
||||||
weak var weakButtonDelegate: ButtonObjectDelegate? = buttonDelegate
|
|
||||||
|
|
||||||
actionBlock = {
|
|
||||||
var performAction = true
|
var performAction = true
|
||||||
|
|
||||||
if let wSelf = weakSelf, let wButtonDelegate = weakButtonDelegate, wButtonDelegate.responds(to: #selector(ButtonObjectDelegate.button(_:shouldPerformActionWithMap:additionalData:))) {
|
if let wSelf = self, let wButtonDelegate = buttonDelegate, wButtonDelegate.responds(to: #selector(ButtonObjectDelegate.button(_:shouldPerformActionWithMap:additionalData:))) {
|
||||||
performAction = wButtonDelegate.button?(wSelf, shouldPerformActionWithMap: actionMap, additionalData: additionalData) ?? false
|
performAction = wButtonDelegate.button?(wSelf, shouldPerformActionWithMap: actionMap, additionalData: additionalData) ?? false
|
||||||
}
|
}
|
||||||
|
|
||||||
if let wDelegate = weakDelegate as? CoreObjectActionLoadPresentDelegate, performAction {
|
if performAction {
|
||||||
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegate: wDelegate)
|
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegate: delegate as? CoreObjectActionLoadPresentDelegate)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -552,12 +523,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)
|
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()
|
setLabelAttributes()
|
||||||
}
|
}
|
||||||
|
|
||||||
@available(*, deprecated)
|
@available(*, deprecated)
|
||||||
@ -578,10 +550,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
|
||||||
@ -646,18 +616,18 @@ 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 = {
|
actionBlock = {
|
||||||
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegate: weakDelegate as? CoreObjectActionLoadPresentDelegate)
|
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: additionalData, delegate: delegate as? CoreObjectActionLoadPresentDelegate)
|
||||||
}
|
}
|
||||||
|
|
||||||
text = getTextFromStringComponents()
|
text = getTextFromStringComponents()
|
||||||
setup()
|
setLabelAttributes()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user