Skip to content

Commit 4ebe6de

Browse files
authored
Merge pull request #5 from onevcat/fix/optional-font
Allow font creation to fail and fallback to a safe font
2 parents ce7748a + 487e399 commit 4ebe6de

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

Sources/AttributedStringBuilder/Attributes.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ extension Attributes {
104104
if bold { traits.formUnion(.bold) }
105105
if italic { traits.formUnion(.italic )}
106106
if !traits.isEmpty { fontDescriptor = fontDescriptor.withSymbolicTraits(traits) }
107-
let font = NSFont(descriptor: fontDescriptor, size: size)!
107+
guard let font = NSFont(descriptor: fontDescriptor, size: size) else {
108+
print("Font creation with traits failed: \(traits). Fallback to named font.")
109+
return NSFont(name: family, size: size) ?? .systemFont(ofSize: size)
110+
}
108111
return font
109112
}
110113

0 commit comments

Comments
 (0)