From 9a8653f39cfe2318ccfd113b2a21439c843a841f Mon Sep 17 00:00:00 2001 From: CMK Date: Wed, 4 Aug 2021 15:47:29 +0800 Subject: [PATCH] fix: auto completion title label not display custom emoji issue --- .../Section/Compose/AutoCompleteSection.swift | 22 +++++++++++++------ .../MastodonSDK/Mastodon+Entity+Account.swift | 8 +++++++ Mastodon/Extension/MetaLabel.swift | 4 ++++ .../Cell/AutoCompleteTableViewCell.swift | 8 +++---- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/Mastodon/Diffiable/Section/Compose/AutoCompleteSection.swift b/Mastodon/Diffiable/Section/Compose/AutoCompleteSection.swift index c5e8891a0..ed205b134 100644 --- a/Mastodon/Diffiable/Section/Compose/AutoCompleteSection.swift +++ b/Mastodon/Diffiable/Section/Compose/AutoCompleteSection.swift @@ -7,6 +7,7 @@ import UIKit import MastodonSDK +import MastodonMeta enum AutoCompleteSection: Equatable, Hashable { case main @@ -48,7 +49,8 @@ extension AutoCompleteSection { extension AutoCompleteSection { private static func configureHashtag(cell: AutoCompleteTableViewCell, hashtag: Mastodon.Entity.Tag) { - cell.titleLabel.text = "#" + hashtag.name + let metaContent = PlaintextMetaContent(string: "#" + hashtag.name) + cell.titleLabel.configure(content: metaContent) cell.subtitleLabel.text = { let count = (hashtag.history ?? []) .sorted(by: { $0.day > $1.day }) @@ -61,23 +63,29 @@ extension AutoCompleteSection { } private static func configureHashtag(cell: AutoCompleteTableViewCell, hashtagName: String) { - cell.titleLabel.text = "#" + hashtagName + let metaContent = PlaintextMetaContent(string: "#" + hashtagName) + cell.titleLabel.configure(content: metaContent) cell.subtitleLabel.text = " " cell.avatarImageView.isHidden = true } private static func configureAccount(cell: AutoCompleteTableViewCell, account: Mastodon.Entity.Account) { - cell.titleLabel.text = { - guard !account.displayName.isEmpty else { return account.username } - return account.displayName - }() + let mastodonContent = MastodonContent(content: account.displayNameWithFallback, emojis: account.emojiMeta) + do { + let metaContent = try MastodonMetaContent.convert(document: mastodonContent) + cell.titleLabel.configure(content: metaContent) + } catch { + let metaContent = PlaintextMetaContent(string: account.displayNameWithFallback) + cell.titleLabel.configure(content: metaContent) + } cell.subtitleLabel.text = "@" + account.acct cell.avatarImageView.isHidden = false cell.configure(with: AvatarConfigurableViewConfiguration(avatarImageURL: URL(string: account.avatar))) } private static func configureEmoji(cell: AutoCompleteTableViewCell, emoji: Mastodon.Entity.Emoji, isFirst: Bool) { - cell.titleLabel.text = ":" + emoji.shortcode + ":" + let metaContent = PlaintextMetaContent(string: ":" + emoji.shortcode + ":") + cell.titleLabel.configure(content: metaContent) // FIXME: handle spacer enter to complete emoji // cell.subtitleLabel.text = isFirst ? L10n.Scene.Compose.AutoComplete.spaceToAdd : " " cell.subtitleLabel.text = " " diff --git a/Mastodon/Extension/MastodonSDK/Mastodon+Entity+Account.swift b/Mastodon/Extension/MastodonSDK/Mastodon+Entity+Account.swift index 6aad350c3..09bbb3d8a 100644 --- a/Mastodon/Extension/MastodonSDK/Mastodon+Entity+Account.swift +++ b/Mastodon/Extension/MastodonSDK/Mastodon+Entity+Account.swift @@ -19,6 +19,14 @@ extension Mastodon.Entity.Account: Hashable { } } +extension Mastodon.Entity.Account { + + var displayNameWithFallback: String { + return !displayName.isEmpty ? displayName : username + } + +} + extension Mastodon.Entity.Account { public func avatarImageURL() -> URL? { let string = UserDefaults.shared.preferredStaticAvatar ? avatarStatic ?? avatar : avatar diff --git a/Mastodon/Extension/MetaLabel.swift b/Mastodon/Extension/MetaLabel.swift index baac93791..8163a10cb 100644 --- a/Mastodon/Extension/MetaLabel.swift +++ b/Mastodon/Extension/MetaLabel.swift @@ -19,6 +19,7 @@ extension MetaLabel { case recommendAccountName case titleView case settingTableFooter + case autoCompletion } convenience init(style: Style) { @@ -70,6 +71,9 @@ extension MetaLabel { numberOfLines = 0 textContainer.maximumNumberOfLines = 0 paragraphStyle.alignment = .center + case .autoCompletion: + font = UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 17, weight: .semibold), maximumPointSize: 22) + textColor = Asset.Colors.brandBlue.color } self.font = font diff --git a/Mastodon/Scene/Compose/AutoComplete/Cell/AutoCompleteTableViewCell.swift b/Mastodon/Scene/Compose/AutoComplete/Cell/AutoCompleteTableViewCell.swift index 8fa5d8644..7492753fe 100644 --- a/Mastodon/Scene/Compose/AutoComplete/Cell/AutoCompleteTableViewCell.swift +++ b/Mastodon/Scene/Compose/AutoComplete/Cell/AutoCompleteTableViewCell.swift @@ -7,6 +7,7 @@ import UIKit import FLAnimatedImage +import MetaTextKit final class AutoCompleteTableViewCell: UITableViewCell { @@ -30,11 +31,8 @@ final class AutoCompleteTableViewCell: UITableViewCell { let avatarImageView = FLAnimatedImageView() - let titleLabel: UILabel = { - let label = UILabel() - label.font = UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 17, weight: .semibold), maximumPointSize: 22) - label.textColor = Asset.Colors.brandBlue.color - label.text = "Title" + let titleLabel: MetaLabel = { + let label = MetaLabel(style: .autoCompletion) return label }()