forked from shadowfacts/Tusker
Improve performance when displaying posts with many custom emojis
Closes #204
This commit is contained in:
parent
b9555cf7dd
commit
97a95c435e
|
@ -22,8 +22,7 @@ extension NSTextAttachment {
|
||||||
image.draw(in: CGRect(origin: .zero, size: imageSizeMatchingFontSize))
|
image.draw(in: CGRect(origin: .zero, size: imageSizeMatchingFontSize))
|
||||||
}
|
}
|
||||||
|
|
||||||
self.init()
|
self.init(image: attachmentImage)
|
||||||
self.image = attachmentImage
|
|
||||||
}
|
}
|
||||||
|
|
||||||
convenience init(emojiPlaceholderIn font: UIFont) {
|
convenience init(emojiPlaceholderIn font: UIFont) {
|
||||||
|
@ -31,7 +30,6 @@ extension NSTextAttachment {
|
||||||
// assumes emoji are mostly square
|
// assumes emoji are mostly square
|
||||||
let size = CGSize(width: adjustedCapHeight, height: adjustedCapHeight)
|
let size = CGSize(width: adjustedCapHeight, height: adjustedCapHeight)
|
||||||
let image = UIGraphicsImageRenderer(size: size).image { (_) in }
|
let image = UIGraphicsImageRenderer(size: size).image { (_) in }
|
||||||
self.init()
|
self.init(image: image)
|
||||||
self.image = image
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -86,20 +86,24 @@ extension BaseEmojiLabel {
|
||||||
func buildStringWithEmojisReplaced(usePlaceholders: Bool) -> NSAttributedString {
|
func buildStringWithEmojisReplaced(usePlaceholders: Bool) -> NSAttributedString {
|
||||||
let mutAttrString = NSMutableAttributedString(attributedString: attributedString)
|
let mutAttrString = NSMutableAttributedString(attributedString: attributedString)
|
||||||
|
|
||||||
// lock once for the entire loop, rather than lock/unlocking for each iteration to do the lookup
|
|
||||||
// OSAllocatedUnfairLock.withLock expects a @Sendable closure, so this warns about captures of non-sendable types (attribute dstrings, text checking results)
|
// OSAllocatedUnfairLock.withLock expects a @Sendable closure, so this warns about captures of non-sendable types (attribute dstrings, text checking results)
|
||||||
// even though the closures is invoked on the same thread that withLock is called, so it's unclear why it needs to be @Sendable (FB11494878)
|
// even though the closures is invoked on the same thread that withLock is called, so it's unclear why it needs to be @Sendable (FB11494878)
|
||||||
// so, just ignore the warnings
|
// so, just ignore the warnings
|
||||||
emojiImages.withLock { emojiImages in
|
let emojiAttachments = emojiImages.withLock {
|
||||||
|
$0.mapValues { image in
|
||||||
|
NSTextAttachment(emojiImage: image, in: self.emojiFont, with: self.emojiTextColor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let placeholder = usePlaceholders ? NSTextAttachment(emojiPlaceholderIn: self.emojiFont) : nil
|
||||||
|
|
||||||
// replaces the emojis starting from the end of the string as to not alter the indices of preceeding emojis
|
// replaces the emojis starting from the end of the string as to not alter the indices of preceeding emojis
|
||||||
for match in matches.reversed() {
|
for match in matches.reversed() {
|
||||||
let shortcode = (attributedString.string as NSString).substring(with: match.range(at: 1))
|
let shortcode = (attributedString.string as NSString).substring(with: match.range(at: 1))
|
||||||
let attachment: NSTextAttachment
|
let attachment: NSTextAttachment
|
||||||
|
if let emoji = emojiAttachments[shortcode] {
|
||||||
if let emojiImage = emojiImages[shortcode] {
|
attachment = emoji
|
||||||
attachment = NSTextAttachment(emojiImage: emojiImage, in: self.emojiFont, with: self.emojiTextColor)
|
|
||||||
} else if usePlaceholders {
|
} else if usePlaceholders {
|
||||||
attachment = NSTextAttachment(emojiPlaceholderIn: self.emojiFont)
|
attachment = placeholder!
|
||||||
} else {
|
} else {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -107,7 +111,6 @@ extension BaseEmojiLabel {
|
||||||
let attachmentStr = NSAttributedString(attachment: attachment)
|
let attachmentStr = NSAttributedString(attachment: attachment)
|
||||||
mutAttrString.replaceCharacters(in: match.range, with: attachmentStr)
|
mutAttrString.replaceCharacters(in: match.range, with: attachmentStr)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return mutAttrString
|
return mutAttrString
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue