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.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
}