diff --git a/MVMCoreUI/Atoms/Views/Label.swift b/MVMCoreUI/Atoms/Views/Label.swift index b1e8c4ed..186c24c8 100644 --- a/MVMCoreUI/Atoms/Views/Label.swift +++ b/MVMCoreUI/Atoms/Views/Label.swift @@ -18,6 +18,7 @@ public typealias ActionBlock = () -> Void //------------------------------------------------------ public var makeWholeViewClickable = false + fileprivate var hasAttachmentImage = false /// Set this property if you want updateView to update the font based on this standard and the size passed in. public var standardFontSize: CGFloat = 0.0 @@ -26,8 +27,6 @@ public typealias ActionBlock = () -> Void public var sizeObject: MFSizeObject? public var scaleSize: NSNumber? - fileprivate var hasAttachmentImage = false - // Used for scaling the font in updateView. private var originalAttributedString: NSAttributedString? @@ -226,18 +225,14 @@ public typealias ActionBlock = () -> Void attributedString.addAttribute(.foregroundColor, value: UIColor.mfGet(forHex: colorHex), range: range) } case "externalLink": - let fontSize = (attribute["size"] as? CGFloat ?? label.font.pointSize) * 0.8 + let fontSize = attribute["size"] as? CGFloat ?? label.font.pointSize + let imageAttachment = Label.getTextAttachmentImage(dimension: fontSize) - let imageAttachment = NSTextAttachment() - imageAttachment.image = MVMCoreUIUtility.imageNamed("externalLink") - imageAttachment.bounds = CGRect(x: 0, y: 0, width: fontSize, height: fontSize) - - let mutableAttributedString = NSMutableAttributedString() - mutableAttributedString.append(NSAttributedString(string: " ")) - mutableAttributedString.append(NSAttributedString(attachment: imageAttachment)) - - attributedString.insert(mutableAttributedString, at: location) + let mutableString = NSMutableAttributedString() + mutableString.append(NSAttributedString(string: " ")) + mutableString.append(NSAttributedString(attachment: imageAttachment)) + attributedString.insert(mutableString, at: location) (label as? Label)?.hasAttachmentImage = true case "font": @@ -359,24 +354,55 @@ public typealias ActionBlock = () -> Void } } - ///Appends an external link image to the end of the attributed string. - public func addExternalLinkIcon() { - - let size = round(font.pointSize * 0.8) + /** + Appends an external link image to the end of the attributed string. + Will provide one whitespace to the left of the icon + */ + public func appendExternalLinkIcon() { guard let attributedText = attributedText else { return } - let fullString = NSMutableAttributedString(attributedString: attributedText) + let mutableString = NSMutableAttributedString(attributedString: attributedText) + + mutableString.append(NSAttributedString(string: " ")) + mutableString.append(NSAttributedString(attachment: Label.getTextAttachmentImage(dimension: font.pointSize))) + self.attributedText = mutableString + + hasAttachmentImage = true + } + + /** + Insert external link icon anywhere within text of Label. + + - 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. + */ + public func insertExternalLinkIcon(at index: Int) { + + guard let attributedText = attributedText, index <= attributedText.string.count && index >= 0 else { return } + + let mutableString = NSMutableAttributedString(attributedString: attributedText) + + if index != 0 { + mutableString.insert(NSAttributedString(string: " "), at: index - 1) + } + mutableString.insert(NSAttributedString(attachment: Label.getTextAttachmentImage(dimension: font.pointSize)), at: index) + self.attributedText = mutableString + + hasAttachmentImage = true + } + + static func getTextAttachmentImage(dimension: CGFloat) -> NSTextAttachment { + + let dimension = round(dimension * 0.8) let imageAttachment = NSTextAttachment() imageAttachment.image = MVMCoreUIUtility.imageNamed("externalLink") - imageAttachment.bounds = CGRect(x: 0, y: 0, width: size, height: size) + imageAttachment.bounds = CGRect(x: 0, y: 0, width: dimension, height: dimension) - fullString.append(NSAttributedString(string: " ")) - fullString.append(NSAttributedString(attachment: imageAttachment)) - self.attributedText = fullString - - hasAttachmentImage = true + return imageAttachment } }