some changes made. More to come.

This commit is contained in:
Kevin G Christiano 2019-08-29 16:21:30 -04:00
parent 5351c31b1c
commit 38912a4f85

View File

@ -254,16 +254,16 @@ public typealias ActionBlock = () -> ()
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 "icon": case "image":
let fontSize = attribute["size"] as? CGFloat ?? label.font.pointSize let fontSize = attribute["size"] as? CGFloat ?? label.font.pointSize
let iconName = attribute["iconName"] as? String ?? "externalLink" let imageName = attribute["name"] as? String ?? "externalLink"
let imageURL = attribute["image"] as? String let imageURL = attribute["URL"] as? String
let imageAttachment: NSTextAttachment let imageAttachment: NSTextAttachment
if let url = imageURL, let label = label as? Label { if let url = imageURL, let label = label as? Label {
imageAttachment = Label.getTextAttachmentFrom(url: url, dimension: fontSize, label: label) imageAttachment = Label.getTextAttachmentFrom(url: url, dimension: fontSize, label: label)
} else { } else {
imageAttachment = Label.getTextAttachmentImage(name: iconName, dimension: fontSize) imageAttachment = Label.getTextAttachmentImage(name: imageName, dimension: fontSize)
} }
let mutableString = NSMutableAttributedString() let mutableString = NSMutableAttributedString()
@ -382,7 +382,7 @@ public typealias ActionBlock = () -> ()
// Loop the original attributed string, resize the image attachments. // Loop the original attributed string, resize the image attachments.
originalAttributedString.enumerateAttribute(.attachment, in: NSRange(location: 0, length: originalAttributedString.length), options: []) { value, range, stop in originalAttributedString.enumerateAttribute(.attachment, in: NSRange(location: 0, length: originalAttributedString.length), options: []) { value, range, stop in
if value is NSTextAttachment, let attachment = value as? NSTextAttachment, if let attachment = value as? NSTextAttachment,
let stylerSize = MFStyler.sizeObjectGeneric(forCurrentDevice: attachment.bounds.width)?.getValueBased(onSize: size) { let stylerSize = MFStyler.sizeObjectGeneric(forCurrentDevice: attachment.bounds.width)?.getValueBased(onSize: size) {
let dimension = round(stylerSize) let dimension = round(stylerSize)
@ -471,14 +471,16 @@ public typealias ActionBlock = () -> ()
let imageAttachment = NSTextAttachment() let imageAttachment = NSTextAttachment()
imageAttachment.bounds = CGRect(x: 0, y: 0, width: dimension, height: dimension) imageAttachment.bounds = CGRect(x: 0, y: 0, width: dimension, height: dimension)
DispatchQueue.main.async { DispatchQueue.global(qos: .default).async {
guard let url = URL(string: url), guard let url = URL(string: url),
let data = try? Data(contentsOf: url) let data = try? Data(contentsOf: url)
else { return } else { return }
imageAttachment.image = UIImage(data: data) imageAttachment.image = UIImage(data: data)
label.setNeedsDisplay() DispatchQueue.main.sync {
label.setNeedsDisplay()
}
} }
return imageAttachment return imageAttachment
@ -595,10 +597,9 @@ extension Label {
@objc private func textLinkTapped(_ gesture: UITapGestureRecognizer) { @objc private func textLinkTapped(_ gesture: UITapGestureRecognizer) {
for clause in clauses { for clause in clauses {
guard let range = clause.range else { return }
// This determines if we tapped on the desired range of text. // This determines if we tapped on the desired range of text.
if gesture.didTapAttributedTextInLabel(self, inRange: range) { if let range = clause.range, gesture.didTapAttributedTextInLabel(self, inRange: range) {
clause.performAction() clause.performAction()
return return
} }