Improved functioning of tap when adding icon before action. Appears to have separate behavior when done as a molecule than from source.

This commit is contained in:
Kevin G Christiano 2019-07-29 09:35:00 -04:00
parent a2d4984822
commit f6cd3aad18

View File

@ -238,9 +238,10 @@ public typealias ActionBlock = () -> Void
attributedString.removeAttribute(.foregroundColor, range: range) attributedString.removeAttribute(.foregroundColor, range: range)
attributedString.addAttribute(.foregroundColor, value: UIColor.mfGet(forHex: colorHex), 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 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 mutableString = NSMutableAttributedString()
let space = NSAttributedString(string: " ") let space = NSAttributedString(string: " ")
@ -286,9 +287,9 @@ public typealias ActionBlock = () -> Void
actionLabel.addActionAttributes(range: range, string: attributedString) actionLabel.addActionAttributes(range: range, string: attributedString)
actionLabel.clauses.append(ActionableClause(range: range, actionLabel.clauses.append(ActionableClause(range: range,
actionBlock: actionLabel.createActionBlockFrom(actionMap: json, actionBlock: actionLabel.createActionBlockFrom(actionMap: json,
additionalData: additionalData, additionalData: additionalData,
delegateObject: delegate))) delegateObject: delegate)))
default: default:
continue continue
} }
@ -297,8 +298,6 @@ public typealias ActionBlock = () -> Void
} }
} }
//------------------------------------------------------ //------------------------------------------------------
// MARK: - Methods // MARK: - Methods
//------------------------------------------------------ //------------------------------------------------------
@ -410,10 +409,9 @@ public typealias ActionBlock = () -> Void
/** /**
Insert external link icon anywhere within text of Label. 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. - 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. 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) { public func insertExternalLinkIcon(at index: Int) {
@ -426,12 +424,12 @@ public typealias ActionBlock = () -> Void
self.attributedText = mutableString 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 dimension = round(dimension * 0.8)
let imageAttachment = NSTextAttachment() let imageAttachment = NSTextAttachment()
imageAttachment.image = MVMCoreUIUtility.imageNamed("externalLink") imageAttachment.image = MVMCoreUIUtility.imageNamed(name)
imageAttachment.bounds = CGRect(x: 0, y: 0, width: dimension, height: dimension) imageAttachment.bounds = CGRect(x: 0, y: 0, width: dimension, height: dimension)
return imageAttachment return imageAttachment
@ -556,18 +554,18 @@ extension Label {
guard value is NSTextAttachment else { return } guard value is NSTextAttachment else { return }
attachmentLocations.append(range.location) attachmentLocations.append(range.location)
// if range.location == 0 { if range.location == 0 {
// offset = 2 offset = 2
// } }
} }
} }
for clause in clauses { for clause in clauses {
guard let range = clause.range else { return } 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 hasAttachmentImageInsideText {
if range.location > attachmentLocations[attachmentIndex] { if range.location >= attachmentLocations[attachmentIndex] - range.length {
offset += 2 offset += 2
attachmentIndex += 1 attachmentIndex += 1
} }