Tusker/Tusker/Views/EmojiLabel.swift

62 lines
1.8 KiB
Swift

//
// EmojiLabel.swift
// Tusker
//
// Created by Shadowfacts on 3/1/20.
// Copyright © 2020 Shadowfacts. All rights reserved.
//
import UIKit
import Pachyderm
class EmojiLabel: UILabel, BaseEmojiLabel {
var emojiIdentifier: String?
var emojiRequests: [ImageCache.Request] = []
var emojiFont: UIFont { font }
var emojiTextColor: UIColor { textColor }
func setEmojis(_ emojis: [Emoji], identifier: String) {
guard emojis.count > 0, let attributedText = attributedText else { return }
self.emojiIdentifier = identifier
emojiRequests.forEach { $0.cancel() }
emojiRequests = []
replaceEmojis(in: attributedText.string, emojis: emojis, identifier: identifier) { [weak self] (newAttributedText) in
guard let self = self, self.emojiIdentifier == identifier else { return }
self.attributedText = newAttributedText
self.setNeedsLayout()
self.setNeedsDisplay()
}
}
func removeEmojis() {
emojiIdentifier = nil
emojiRequests.forEach { $0.cancel() }
emojiRequests = []
}
}
extension EmojiLabel {
func updateForAccountDisplayName(account: Account) {
if Preferences.shared.hideCustomEmojiInUsernames {
self.text = account.displayName
self.removeEmojis()
} else {
self.text = account.displayName
self.setEmojis(account.emojis, identifier: account.id)
}
}
func updateForAccountDisplayName(account: AccountMO) {
if Preferences.shared.hideCustomEmojiInUsernames {
self.text = account.displayNameWithoutCustomEmoji
self.removeEmojis()
} else {
self.text = account.displayOrUserName
self.setEmojis(account.emojis, identifier: account.id)
}
}
}