From 6718e151338555a644d6f20f3b428c168a3c7707 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Wed, 14 Feb 2024 15:34:41 -0600 Subject: [PATCH] added extra methods used by MF Signed-off-by: Matt Bruce --- .../Atomic/Atoms/Views/Label/Label.swift | 60 ++++++++++++++++++- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/MVMCoreUI/Atomic/Atoms/Views/Label/Label.swift b/MVMCoreUI/Atomic/Atoms/Views/Label/Label.swift index b6ecfb95..6d33d8ce 100644 --- a/MVMCoreUI/Atomic/Atoms/Views/Label/Label.swift +++ b/MVMCoreUI/Atomic/Atoms/Views/Label/Label.swift @@ -278,14 +278,18 @@ public typealias ActionBlock = () -> () } } - public func updateView(_ size: CGFloat) { } + @objc open override func reset() { + super.reset() + } - @objc public func setFont(_ font: UIFont, scale: Bool) { + @objc open func updateView(_ size: CGFloat) { } + + @objc open func setFont(_ font: UIFont, scale: Bool) { self.font = font setScale(scale) } - @objc public func setScale(_ scale: Bool) { + @objc open func setScale(_ scale: Bool) { if scale { standardFontSize = font.pointSize if let floatScale = scaleSize?.floatValue { @@ -354,6 +358,52 @@ public typealias ActionBlock = () -> () // Mark: - Old Helpers extension Label { + /** + Appends an external link image to the end of the attributed string. + Will provide one whitespace to the left of the icon; adds 2 chars to the end of the string. + */ + @objc public func appendExternalLinkIcon() { + + guard let attributedText = attributedText else { return } + + let mutableString = NSMutableAttributedString(attributedString: attributedText) + + mutableString.append(NSAttributedString(string: " ")) + mutableString.append(NSAttributedString(attachment: Label.getTextAttachmentImage(dimension: font.pointSize))) + self.attributedText = mutableString + } + + /** + Insert external link icon anywhere within text of Label. + + - Note: Each icon insertion adds 1 additional characters to the overall text length. + Therefore, you **MUST** insert icons and links in the order they would appear. + - parameter index: Location within the associated text to insert an external Link Icon + */ + public func insertExternalLinkIcon(at index: Int) { + + guard let attributedText = attributedText, index <= attributedText.string.count && index >= 0 else { return } + + let mutableString = NSMutableAttributedString(attributedString: attributedText) + mutableString.insert(NSAttributedString(attachment: Label.getTextAttachmentImage(dimension: font.pointSize)), at: index) + + self.attributedText = mutableString + } + + /* + Retrieves an NSTextAttachment for NSAttributedString that is prepped to be inserted with the text. + + - parameter name: The Asset name of the image. DEFAULT: "externalLink" + - parameter dimension: length of the height and width of the image. Will be 80% the passed magnitude. + */ + static func getTextAttachmentImage(name: String = "externalLink", dimension: CGFloat) -> NSTextAttachment { + + let imageAttachment = NSTextAttachment() + imageAttachment.image = MVMCoreCache.shared()?.getImageFromRegisteredBundles(name) + imageAttachment.bounds = CGRect(x: 0, y: 0, width: dimension, height: dimension) + + return imageAttachment + } public static func boundingRect(forCharacterRange range: NSRange, in label: Label) -> CGRect { @@ -424,6 +474,10 @@ extension Label { attributes?.append(textLink) } + @objc public func clearActionableClauses() { + attributes = attributes?.filter { !($0 is (any ActionLabelAttributeModel)) } + } + public func createActionBlockFor(actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) -> ActionBlock? { return { [weak self] in guard let self = self else { return }