Skip to content

Commit

Permalink
Improve Default Placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
divadretlaw committed Jan 26, 2024
1 parent d29af75 commit 064c4f1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
9 changes: 7 additions & 2 deletions Sources/EmojiText/EmojiText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,18 @@ public struct EmojiText: View {
// MARK: - Load Emojis

func loadPlaceholders() -> [String: RenderedEmoji] {
let font = EmojiFont.preferredFont(from: font, for: dynamicTypeSize)
let baselineOffset = emojiBaselineOffset ?? -(font.pointSize - font.capHeight) / 2

var placeholders = [String: RenderedEmoji]()

for emoji in emojis {
switch emoji {
case let localEmoji as LocalEmoji:
placeholders[emoji.shortcode] = RenderedEmoji(
from: localEmoji,
targetHeight: targetHeight
targetHeight: targetHeight,
baselineOffset: baselineOffset
)
case let sfSymbolEmoji as SFSymbolEmoji:
placeholders[emoji.shortcode] = RenderedEmoji(
Expand All @@ -114,7 +118,8 @@ public struct EmojiText: View {
placeholders[emoji.shortcode] = RenderedEmoji(
from: emoji,
placeholder: emojiPlaceholder,
targetHeight: targetHeight
targetHeight: targetHeight,
baselineOffset: baselineOffset
)
}
}
Expand Down
12 changes: 11 additions & 1 deletion Sources/EmojiText/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,17 @@ struct EmojiImagePipelineKey: EnvironmentKey {

struct EmojiPlaceholderKey: EnvironmentKey {
static var defaultValue: any CustomEmoji {
SFSymbolEmoji.placeholder
#if os(iOS) || targetEnvironment(macCatalyst) || os(tvOS) || os(watchOS) || os(visionOS)
if let image = UIImage(systemName: "square.dashed") {
return LocalEmoji(shortcode: "square.dashed", image: image, renderingMode: .template)
}
#elseif os(macOS)
if let image = NSImage(systemName: "square.dashed") {
return LocalEmoji(shortcode: "square.dashed", image: image, renderingMode: .template)
}
#endif

return SFSymbolEmoji(shortcode: "square.dashed", symbolRenderingMode: .monochrome, renderingMode: .template)
}
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/EmojiText/Model/Emoji/RenderedEmoji.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ struct RenderedEmoji: Hashable, Equatable, Identifiable {
self.sourceHash = emoji.hashValue
}

init(from emoji: any CustomEmoji, placeholder: any CustomEmoji, animated: Bool = false, targetHeight: CGFloat) {
init(from emoji: any CustomEmoji, placeholder: any CustomEmoji, animated: Bool = false, targetHeight: CGFloat, baselineOffset: CGFloat? = nil) {
self.shortcode = "placeholder"
self.renderingMode = emoji.renderingMode
self.baselineOffset = emoji.baselineOffset
self.baselineOffset = emoji.baselineOffset ?? baselineOffset
self.symbolRenderingMode = emoji.symbolRenderingMode
self.placeholderId = UUID()
// The source hash is the cominbed value of the emoji & target height
Expand All @@ -76,7 +76,7 @@ struct RenderedEmoji: Hashable, Equatable, Identifiable {
case let sfSymbolEmoji as SFSymbolEmoji:
self.rawImage = RenderedImage(systemName: sfSymbolEmoji.shortcode)
default:
self.rawImage = RenderedImage(systemName: SFSymbolEmoji.placeholder.shortcode)
self.rawImage = RenderedImage(systemName: SFSymbolEmoji.fallback.shortcode)
Logger.emojiText.error("Unsupported CustomEmoji was used as placeholder. Only LocalEmoji and SFSymbolEmoji are supported. This is a bug. Please file a report at https://github.com/divadretlaw/EmojiText")
}
}
Expand Down
5 changes: 2 additions & 3 deletions Sources/EmojiText/Model/Emoji/SFSymbolEmoji.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public struct SFSymbolEmoji: CustomEmoji {
self.renderingMode = renderingMode
}

/// The default placeholder
static var placeholder: Self {
static var fallback: Self {
SFSymbolEmoji(shortcode: "square.dashed", symbolRenderingMode: .monochrome, renderingMode: .template)
}

Expand All @@ -45,7 +44,7 @@ public struct SFSymbolEmoji: CustomEmoji {
extension EmojiImage {
static func from(emoji: SFSymbolEmoji) -> EmojiImage {
EmojiImage(systemName: emoji.shortcode)
?? EmojiImage(systemName: SFSymbolEmoji.placeholder.shortcode)
?? EmojiImage(systemName: SFSymbolEmoji.fallback.shortcode)
?? EmojiImage()
}
}

0 comments on commit 064c4f1

Please sign in to comment.