mirror of
https://github.com/metabolist/metatext.git
synced 2024-11-22 16:21:00 +00:00
28 lines
997 B
Swift
28 lines
997 B
Swift
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
import Kingfisher
|
|
import Mastodon
|
|
import UIKit
|
|
|
|
extension NSMutableAttributedString {
|
|
func insert(emoji: [Emoji], view: UIView) {
|
|
for emoji in emoji {
|
|
let token = ":\(emoji.shortcode):"
|
|
|
|
while let tokenRange = string.range(of: token) {
|
|
let attachment = NSTextAttachment()
|
|
|
|
attachment.kf.setImage(with: emoji.url, attributedView: view)
|
|
replaceCharacters(in: NSRange(tokenRange, in: string), with: NSAttributedString(attachment: attachment))
|
|
}
|
|
}
|
|
}
|
|
|
|
func resizeAttachments(toLineHeight lineHeight: CGFloat) {
|
|
enumerateAttribute(.attachment, in: NSRange(location: 0, length: length), options: []) { attribute, _, _ in
|
|
guard let attachment = attribute as? NSTextAttachment else { return }
|
|
|
|
attachment.bounds = CGRect(x: 0, y: lineHeight * -0.25, width: lineHeight, height: lineHeight)
|
|
}
|
|
}
|
|
}
|