Tusker/Tusker/Views/MultiSourceEmojiLabel.swift

51 lines
1.5 KiB
Swift
Raw Normal View History

//
// MultiSourceEmojiLabel.swift
// Tusker
//
// Created by Shadowfacts on 10/18/20.
// Copyright © 2020 Shadowfacts. All rights reserved.
//
import UIKit
import Pachyderm
private let emojiRegex = try! NSRegularExpression(pattern: ":(\\w+):", options: [])
class MultiSourceEmojiLabel: UILabel, BaseEmojiLabel {
var emojiIdentifier: String?
var emojiRequests = [ImageCache.Request]()
var emojiFont: UIFont { font }
var emojiTextColor: UIColor { textColor }
var combiner: (([NSAttributedString]) -> NSAttributedString)?
func setEmojis(pairs: [(String, [Emoji])], identifier: String) {
guard pairs.count > 0 else { return }
self.emojiIdentifier = identifier
emojiRequests.forEach { $0.cancel() }
emojiRequests = []
var attributedStrings = pairs.map { NSAttributedString(string: $0.0) }
func recombine() {
if let combiner = self.combiner {
self.attributedText = combiner(attributedStrings)
}
}
recombine()
for (index, (string, emojis)) in pairs.enumerated() {
2021-11-07 18:11:49 +00:00
self.replaceEmojis(in: string, emojis: emojis, identifier: identifier) { (attributedString, _) in
attributedStrings[index] = attributedString
DispatchQueue.main.async { [weak self] in
guard let self = self, self.emojiIdentifier == identifier else { return }
recombine()
}
}
}
}
}