2020-10-18 16:22:12 +00:00
|
|
|
//
|
|
|
|
// 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
|
2020-10-18 16:22:12 +00:00
|
|
|
attributedStrings[index] = attributedString
|
|
|
|
DispatchQueue.main.async { [weak self] in
|
|
|
|
guard let self = self, self.emojiIdentifier == identifier else { return }
|
|
|
|
recombine()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|