From f6cd3aad18a5658197eca449d7d123891bb46e91 Mon Sep 17 00:00:00 2001 From: Kevin G Christiano Date: Mon, 29 Jul 2019 09:35:00 -0400 Subject: [PATCH] Improved functioning of tap when adding icon before action. Appears to have separate behavior when done as a molecule than from source. --- MVMCoreUI/Atoms/Views/Label.swift | 36 +++++++++++++++---------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/MVMCoreUI/Atoms/Views/Label.swift b/MVMCoreUI/Atoms/Views/Label.swift index 69d37804..49ae467f 100644 --- a/MVMCoreUI/Atoms/Views/Label.swift +++ b/MVMCoreUI/Atoms/Views/Label.swift @@ -238,9 +238,10 @@ public typealias ActionBlock = () -> Void attributedString.removeAttribute(.foregroundColor, range: range) attributedString.addAttribute(.foregroundColor, value: UIColor.mfGet(forHex: colorHex), range: range) } - case "externalLink": + case "icon": let fontSize = attribute["size"] as? CGFloat ?? label.font.pointSize - let imageAttachment = Label.getTextAttachmentImage(dimension: fontSize) + let iconName = attribute["iconName"] as? String ?? "externalLink" + let imageAttachment = Label.getTextAttachmentImage(name: iconName, dimension: fontSize) let mutableString = NSMutableAttributedString() let space = NSAttributedString(string: " ") @@ -253,7 +254,7 @@ public typealias ActionBlock = () -> Void mutableString.append(space) mutableString.append(attachment) } - + attributedString.insert(mutableString, at: location) if location < attributedString.length { @@ -269,13 +270,13 @@ public typealias ActionBlock = () -> Void } else { let fontSize = attribute["size"] as? CGFloat var font: UIFont? - + if let fontName = attribute.optionalStringForKey("name") { font = MFFonts.mfFont(withName: fontName, size: fontSize ?? label.font.pointSize) } else if let fontSize = fontSize { font = label.font.withSize(fontSize) } - + if let font = font { attributedString.removeAttribute(.font, range: range) attributedString.addAttribute(.font, value: font, range: range) @@ -286,9 +287,9 @@ public typealias ActionBlock = () -> Void actionLabel.addActionAttributes(range: range, string: attributedString) actionLabel.clauses.append(ActionableClause(range: range, - actionBlock: actionLabel.createActionBlockFrom(actionMap: json, - additionalData: additionalData, - delegateObject: delegate))) + actionBlock: actionLabel.createActionBlockFrom(actionMap: json, + additionalData: additionalData, + delegateObject: delegate))) default: continue } @@ -297,8 +298,6 @@ public typealias ActionBlock = () -> Void } } - - //------------------------------------------------------ // MARK: - Methods //------------------------------------------------------ @@ -410,10 +409,9 @@ public typealias ActionBlock = () -> Void /** Insert external link icon anywhere within text of Label. - - Parameters: - - index: Location within the associated text to insert an external Link Icon - Note: Each icon insertion adds 2 additional characters to the overall text length. This means that 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) { @@ -426,12 +424,12 @@ public typealias ActionBlock = () -> Void self.attributedText = mutableString } - static func getTextAttachmentImage(dimension: CGFloat) -> NSTextAttachment { + static func getTextAttachmentImage(name: String = "externalLink", dimension: CGFloat) -> NSTextAttachment { let dimension = round(dimension * 0.8) let imageAttachment = NSTextAttachment() - imageAttachment.image = MVMCoreUIUtility.imageNamed("externalLink") + imageAttachment.image = MVMCoreUIUtility.imageNamed(name) imageAttachment.bounds = CGRect(x: 0, y: 0, width: dimension, height: dimension) return imageAttachment @@ -556,18 +554,18 @@ extension Label { guard value is NSTextAttachment else { return } attachmentLocations.append(range.location) -// if range.location == 0 { -// offset = 2 -// } + if range.location == 0 { + offset = 2 + } } } for clause in clauses { guard let range = clause.range else { return } - // Must increment an offset becuase every image added to text interior increases text length by 1 or 2 depending on location. + // Must increment an offset becuase every image added to text interior increases text length by 2. if hasAttachmentImageInsideText { - if range.location > attachmentLocations[attachmentIndex] { + if range.location >= attachmentLocations[attachmentIndex] - range.length { offset += 2 attachmentIndex += 1 }