1
0
mirror of https://github.com/metabolist/metatext synced 2024-12-19 13:02:49 +01:00
metatext-app-ios-iphone-ipad/Extensions/NSMutableAttributedString+Extensions.swift

29 lines
997 B
Swift
Raw Normal View History

2020-08-08 11:10:05 +02:00
// Copyright © 2020 Metabolist. All rights reserved.
import Kingfisher
2020-08-31 01:33:11 +02:00
import Mastodon
2020-09-05 04:31:43 +02:00
import UIKit
2020-08-08 11:10:05 +02:00
extension NSMutableAttributedString {
2020-08-27 07:10:31 +02:00
func insert(emoji: [Emoji], view: UIView) {
for emoji in emoji {
2020-08-08 11:10:05 +02:00
let token = ":\(emoji.shortcode):"
while let tokenRange = string.range(of: token) {
let attachment = NSTextAttachment()
2020-08-27 07:10:31 +02:00
attachment.kf.setImage(with: emoji.url, attributedView: view)
replaceCharacters(in: NSRange(tokenRange, in: string), with: NSAttributedString(attachment: attachment))
2020-08-08 11:10:05 +02:00
}
}
}
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)
}
}
}