From ddb4ba39d0065d7354323c05487a34b718203ef8 Mon Sep 17 00:00:00 2001 From: Kevin G Christiano Date: Sat, 27 Jul 2019 17:00:27 -0400 Subject: [PATCH] Reworked mutable insert logic. Revised comments. --- MVMCoreUI/Atoms/Views/Label.swift | 43 ++++++++++--------------------- 1 file changed, 14 insertions(+), 29 deletions(-) diff --git a/MVMCoreUI/Atoms/Views/Label.swift b/MVMCoreUI/Atoms/Views/Label.swift index d21f2195..2cdd6d01 100644 --- a/MVMCoreUI/Atoms/Views/Label.swift +++ b/MVMCoreUI/Atoms/Views/Label.swift @@ -406,28 +406,18 @@ public typealias ActionBlock = () -> Void - Parameters: - index: Location within the associated text to insert an external Link Icon - - Note: You will need to increment your current index by 2 for any changes to the text after inserting an icon. - Each icon insertion adds 2 additional characters to the overall text length. - This implies that you must insert exertal icons in the order they would appear with tappable links. + - 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. */ public func insertExternalLinkIcon(at index: Int) { - guard let attributedText = attributedText, index <= attributedText.string.count && index > 0 else { return } - + guard let attributedText = attributedText, index <= attributedText.string.count && index >= 0 else { return } + let mutableString = NSMutableAttributedString(attributedString: attributedText) - - if index == 0 { - mutableString.insert(NSAttributedString(attachment: Label.getTextAttachmentImage(dimension: font.pointSize)), at: index) - } else { - mutableString.insert(NSAttributedString(string: " "), at: index) - mutableString.insert(NSAttributedString(attachment: Label.getTextAttachmentImage(dimension: font.pointSize)), at: index + 1) - } + mutableString.insert(NSAttributedString(string: " "), at: index) + mutableString.insert(NSAttributedString(attachment: Label.getTextAttachmentImage(dimension: font.pointSize)), at: index + (index == 0 ? 0 : 1)) self.attributedText = mutableString - -// if index < attributedText.length { -// hasAttachmentImageInsideText = true -// } } static func getTextAttachmentImage(dimension: CGFloat) -> NSTextAttachment { @@ -521,9 +511,8 @@ extension Label { Provides an actionable range of text. - Attention: This method expects text to be set first. Otherwise, it will do nothing. - - Parameters: - - range: The range of text to be tapped. - - actionBlock: The code triggered when tapping the range of text. + - parameter range: The range of text to be tapped. + - parameter actionBlock: The code triggered when tapping the range of text. */ @objc public func addTappableLinkAttribute(range: NSRange, actionBlock: @escaping ActionBlock) { @@ -535,11 +524,10 @@ extension Label { Provides an actionable range of text. - Attention: This method expects text to be set first. Otherwise, it will do nothing. - - Parameters: - - range: The range of text to be tapped. - - actionMap: - - delegate: - - additionalData: + - parameter range: The range of text to be tapped. + - parameter actionMap: + - parameter delegate: + - parameter additionalData: */ @objc public func addTappableLinkAttribute(range: NSRange, actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) { @@ -556,16 +544,12 @@ extension Label { var attachmentLocations: [Int] = [] var attachmentIndex = 0 - // Used to identify all images attached in the label. + // Used to identify all image attachments in the label. if hasAttachmentImageInsideText { attributedText?.enumerateAttribute(.attachment, in: NSRange(location: 0, length: attributedText!.length), options: []) { value, range, stop in guard value is NSTextAttachment else { return } attachmentLocations.append(range.location) - - if range.location == 0 { - offset = 1 - } } } @@ -615,6 +599,7 @@ extension UITapGestureRecognizer { let indexOfGlyph = layoutManager.glyphIndex(for: location(in: label), in: textContainer) + // If text attachment was inserted then extra indices must be accounted for. if label.hasAttachmentImageInsideText { let location = targetRange.location + offset return NSLocationInRange(indexOfGlyph, NSRange(location: location, length: targetRange.length))