Reworked mutable insert logic. Revised comments.

This commit is contained in:
Kevin G Christiano 2019-07-27 17:00:27 -04:00
parent 5a7acf6a33
commit ddb4ba39d0

View File

@ -406,28 +406,18 @@ public typealias ActionBlock = () -> Void
- Parameters: - Parameters:
- index: Location within the associated text to insert an external Link Icon - 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. - Note: Each icon insertion adds 2 additional characters to the overall text length.
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.
This implies that you must insert exertal icons in the order they would appear with tappable links.
*/ */
public func insertExternalLinkIcon(at index: Int) { 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) let mutableString = NSMutableAttributedString(attributedString: attributedText)
mutableString.insert(NSAttributedString(string: " "), at: index)
if index == 0 { mutableString.insert(NSAttributedString(attachment: Label.getTextAttachmentImage(dimension: font.pointSize)), at: index + (index == 0 ? 0 : 1))
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)
}
self.attributedText = mutableString self.attributedText = mutableString
// if index < attributedText.length {
// hasAttachmentImageInsideText = true
// }
} }
static func getTextAttachmentImage(dimension: CGFloat) -> NSTextAttachment { static func getTextAttachmentImage(dimension: CGFloat) -> NSTextAttachment {
@ -521,9 +511,8 @@ extension Label {
Provides an actionable range of text. Provides an actionable range of text.
- Attention: This method expects text to be set first. Otherwise, it will do nothing. - Attention: This method expects text to be set first. Otherwise, it will do nothing.
- Parameters: - parameter range: The range of text to be tapped.
- range: The range of text to be tapped. - parameter actionBlock: The code triggered when tapping the range of text.
- actionBlock: The code triggered when tapping the range of text.
*/ */
@objc public func addTappableLinkAttribute(range: NSRange, actionBlock: @escaping ActionBlock) { @objc public func addTappableLinkAttribute(range: NSRange, actionBlock: @escaping ActionBlock) {
@ -535,11 +524,10 @@ extension Label {
Provides an actionable range of text. Provides an actionable range of text.
- Attention: This method expects text to be set first. Otherwise, it will do nothing. - Attention: This method expects text to be set first. Otherwise, it will do nothing.
- Parameters: - parameter range: The range of text to be tapped.
- range: The range of text to be tapped. - parameter actionMap:
- actionMap: - parameter delegate:
- delegate: - parameter additionalData:
- additionalData:
*/ */
@objc public func addTappableLinkAttribute(range: NSRange, actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) { @objc public func addTappableLinkAttribute(range: NSRange, actionMap: [AnyHashable: Any]?, additionalData: [AnyHashable: Any]?, delegateObject: DelegateObject?) {
@ -556,16 +544,12 @@ extension Label {
var attachmentLocations: [Int] = [] var attachmentLocations: [Int] = []
var attachmentIndex = 0 var attachmentIndex = 0
// Used to identify all images attached in the label. // Used to identify all image attachments in the label.
if hasAttachmentImageInsideText { if hasAttachmentImageInsideText {
attributedText?.enumerateAttribute(.attachment, in: NSRange(location: 0, length: attributedText!.length), options: []) { value, range, stop in attributedText?.enumerateAttribute(.attachment, in: NSRange(location: 0, length: attributedText!.length), options: []) { value, range, stop in
guard value is NSTextAttachment else { return } guard value is NSTextAttachment else { return }
attachmentLocations.append(range.location) 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) 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 { if label.hasAttachmentImageInsideText {
let location = targetRange.location + offset let location = targetRange.location + offset
return NSLocationInRange(indexOfGlyph, NSRange(location: location, length: targetRange.length)) return NSLocationInRange(indexOfGlyph, NSRange(location: location, length: targetRange.length))