Skip to content

Commit 1fddd83

Browse files
committed
Adjust Font Using Font Manager
1 parent 38a690c commit 1fddd83

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

Example/CodeEditSourceEditorExample/CodeEditSourceEditorExample/Views/ContentView.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct ContentView: View {
1616

1717
@State private var language: CodeLanguage = .default
1818
@State private var theme: EditorTheme = .standard
19-
@State private var font: NSFont = NSFont.monospacedSystemFont(ofSize: 12, weight: .regular)
19+
@State private var font: NSFont = NSFont.monospacedSystemFont(ofSize: 12, weight: .medium)
2020
@AppStorage("wrapLines") private var wrapLines: Bool = true
2121
@State private var cursorPositions: [CursorPosition] = []
2222
@AppStorage("systemCursor") private var useSystemCursor: Bool = false

Sources/CodeEditSourceEditor/Theme/EditorTheme.swift

+11-11
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@ public struct EditorTheme: Equatable {
2323
self.bold = bold
2424
self.italic = italic
2525
}
26-
27-
var fontDescriptorTraits: NSFontDescriptor.SymbolicTraits {
28-
switch (bold, italic) {
29-
case (true, true): return [.bold, .italic]
30-
case (true, false): return [.bold]
31-
case (false, true): return [.italic]
32-
case (false, false): return []
33-
}
34-
}
3526
}
3627

3728
public var text: Attribute
@@ -125,7 +116,16 @@ public struct EditorTheme: Equatable {
125116
return font
126117
}
127118

128-
let descriptor = font.fontDescriptor.withSymbolicTraits(attributes.fontDescriptorTraits)
129-
return NSFont(descriptor: descriptor, size: font.pointSize) ?? font
119+
var font = font
120+
121+
if attributes.bold {
122+
font = NSFontManager.shared.convert(font, toHaveTrait: .boldFontMask)
123+
}
124+
125+
if attributes.italic {
126+
font = NSFontManager.shared.convert(font, toHaveTrait: .italicFontMask)
127+
}
128+
129+
return font
130130
}
131131
}

0 commit comments

Comments
 (0)