From 232cbc15ce6c210dfe1367d97d2099742467a27c Mon Sep 17 00:00:00 2001 From: Austin Condiff Date: Fri, 14 Jun 2024 13:15:48 -0500 Subject: [PATCH 1/4] Default font weight is now medium. Added font weight pickers to text editing and terminal sections within settings. Small fix when using custom icons in editior tab. --- CodeEdit.xcodeproj/project.pbxproj | 4 ++ .../TabBar/Tabs/Tab/EditorTabView.swift | 4 +- .../Models/TerminalSettings.swift | 7 +- .../TerminalSettingsView.swift | 6 ++ .../Models/TextEditingSettings.swift | 64 +++++++++++++++++-- .../TextEditingSettingsView.swift | 5 ++ .../Settings/Views/FontWeightPicker.swift | 46 +++++++++++++ 7 files changed, 128 insertions(+), 8 deletions(-) create mode 100644 CodeEdit/Features/Settings/Views/FontWeightPicker.swift diff --git a/CodeEdit.xcodeproj/project.pbxproj b/CodeEdit.xcodeproj/project.pbxproj index c89dd6436d..cc3adc37a2 100644 --- a/CodeEdit.xcodeproj/project.pbxproj +++ b/CodeEdit.xcodeproj/project.pbxproj @@ -486,6 +486,7 @@ B6C6A42A297716A500A3D28F /* EditorTabCloseButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6C6A429297716A500A3D28F /* EditorTabCloseButton.swift */; }; B6C6A42E29771A8D00A3D28F /* EditorTabButtonStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6C6A42D29771A8D00A3D28F /* EditorTabButtonStyle.swift */; }; B6C6A43029771F7100A3D28F /* EditorTabBackground.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6C6A42F29771F7100A3D28F /* EditorTabBackground.swift */; }; + B6CFD80D2C1B9A8000E63F1A /* FontWeightPicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6CFD80C2C1B9A8000E63F1A /* FontWeightPicker.swift */; }; B6D7EA592971078500301FAC /* InspectorSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6D7EA582971078500301FAC /* InspectorSection.swift */; }; B6D7EA5C297107DD00301FAC /* InspectorField.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6D7EA5B297107DD00301FAC /* InspectorField.swift */; }; B6E41C7029DD157F0088F9F4 /* AccountsSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6E41C6F29DD157F0088F9F4 /* AccountsSettingsView.swift */; }; @@ -1056,6 +1057,7 @@ B6C6A429297716A500A3D28F /* EditorTabCloseButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditorTabCloseButton.swift; sourceTree = ""; }; B6C6A42D29771A8D00A3D28F /* EditorTabButtonStyle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EditorTabButtonStyle.swift; sourceTree = ""; }; B6C6A42F29771F7100A3D28F /* EditorTabBackground.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditorTabBackground.swift; sourceTree = ""; }; + B6CFD80C2C1B9A8000E63F1A /* FontWeightPicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FontWeightPicker.swift; sourceTree = ""; }; B6D7EA582971078500301FAC /* InspectorSection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InspectorSection.swift; sourceTree = ""; }; B6D7EA5B297107DD00301FAC /* InspectorField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InspectorField.swift; sourceTree = ""; }; B6E41C6F29DD157F0088F9F4 /* AccountsSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountsSettingsView.swift; sourceTree = ""; }; @@ -2988,6 +2990,7 @@ B640A9A029E2188F00715F20 /* View+NavigationBarBackButtonVisible.swift */, B6EA200129DB7F81001BF195 /* View+ConstrainHeightToWindow.swift */, B6E41C7329DD40010088F9F4 /* View+HideSidebarToggle.swift */, + B6CFD80C2C1B9A8000E63F1A /* FontWeightPicker.swift */, ); path = Views; sourceTree = ""; @@ -3547,6 +3550,7 @@ 5882252B292C280D00E83CDE /* StatusBarCursorPositionLabel.swift in Sources */, 5882252D292C280D00E83CDE /* UtilityAreaSplitTerminalButton.swift in Sources */, 58798238292E30B90085B254 /* FeedbackWindowController.swift in Sources */, + B6CFD80D2C1B9A8000E63F1A /* FontWeightPicker.swift in Sources */, 587B9E6C29301D8F00AC7927 /* GitLabNamespace.swift in Sources */, 30AB4EC22BF7253200ED4431 /* KeyValueTable.swift in Sources */, 6C48D8F22972DAFC00D6D205 /* Env+IsFullscreen.swift in Sources */, diff --git a/CodeEdit/Features/Editor/TabBar/Tabs/Tab/EditorTabView.swift b/CodeEdit/Features/Editor/TabBar/Tabs/Tab/EditorTabView.swift index 13c0306576..9e51ac3d59 100644 --- a/CodeEdit/Features/Editor/TabBar/Tabs/Tab/EditorTabView.swift +++ b/CodeEdit/Features/Editor/TabBar/Tabs/Tab/EditorTabView.swift @@ -134,15 +134,13 @@ struct EditorTabView: View { // Tab content (icon and text). HStack(alignment: .center, spacing: 3) { Image(nsImage: item.nsIcon) - .resizable() - .aspectRatio(contentMode: .fit) + .frame(width: 16, height: 16) .foregroundColor( fileIconStyle == .color && activeState != .inactive && isActiveEditor ? item.iconColor : .secondary ) - .frame(width: 16, height: 16) Text(item.name) .font( isTemporary diff --git a/CodeEdit/Features/Settings/Pages/TerminalSettings/Models/TerminalSettings.swift b/CodeEdit/Features/Settings/Pages/TerminalSettings/Models/TerminalSettings.swift index 9ffa415138..764e8f882f 100644 --- a/CodeEdit/Features/Settings/Pages/TerminalSettings/Models/TerminalSettings.swift +++ b/CodeEdit/Features/Settings/Pages/TerminalSettings/Models/TerminalSettings.swift @@ -103,6 +103,9 @@ extension SettingsData { /// The name of the custom font var name: String = "SF Mono" + + /// The weight of the custom font + var weight: NSFont.Weight = .medium /// Default initializer init() {} @@ -112,6 +115,7 @@ extension SettingsData { let container = try decoder.container(keyedBy: CodingKeys.self) self.size = try container.decodeIfPresent(Double.self, forKey: .size) ?? size self.name = try container.decodeIfPresent(String.self, forKey: .name) ?? name + self.weight = try container.decodeIfPresent(NSFont.Weight.self, forKey: .weight) ?? weight } /// Returns an NSFont representation of the current configuration. @@ -119,7 +123,8 @@ extension SettingsData { /// Returns the custom font, if enabled and able to be instantiated. /// Otherwise returns a default system font monospaced. var current: NSFont { - return NSFont(name: name, size: size) ?? NSFont.monospacedSystemFont(ofSize: size, weight: .medium) + let customFont = NSFont(name: name, size: size)?.withWeight(weight: weight) + return customFont ?? NSFont.monospacedSystemFont(ofSize: size, weight: .medium) } } } diff --git a/CodeEdit/Features/Settings/Pages/TerminalSettings/TerminalSettingsView.swift b/CodeEdit/Features/Settings/Pages/TerminalSettings/TerminalSettingsView.swift index 79d793c942..239af6c17a 100644 --- a/CodeEdit/Features/Settings/Pages/TerminalSettings/TerminalSettingsView.swift +++ b/CodeEdit/Features/Settings/Pages/TerminalSettings/TerminalSettingsView.swift @@ -22,6 +22,7 @@ struct TerminalSettingsView: View { if !settings.useTextEditorFont { fontSelector fontSizeSelector + fontWeightSelector } } Section { @@ -85,6 +86,11 @@ private extension TerminalSettingsView { format: .number ) } + + @ViewBuilder private var fontWeightSelector: some View { + FontWeightPicker(selection: $settings.font.weight) + } + @ViewBuilder private var injectionOptions: some View { VStack { diff --git a/CodeEdit/Features/Settings/Pages/TextEditingSettings/Models/TextEditingSettings.swift b/CodeEdit/Features/Settings/Pages/TextEditingSettings/Models/TextEditingSettings.swift index ab1112423e..5df879c7b4 100644 --- a/CodeEdit/Features/Settings/Pages/TextEditingSettings/Models/TextEditingSettings.swift +++ b/CodeEdit/Features/Settings/Pages/TextEditingSettings/Models/TextEditingSettings.swift @@ -20,6 +20,7 @@ extension SettingsData { "Wrap lines to editor width", "Font", "Font Size", + "Font Weight", "Line Height", "Letter Spacing", "Autocomplete braces", @@ -48,8 +49,8 @@ extension SettingsData { /// A flag indicating whether to wrap lines to editor width var wrapLinesToEditorWidth: Bool = true - /// A multiplier for setting the line height. Defaults to `1.45` - var lineHeightMultiple: Double = 1.45 + /// A multiplier for setting the line height. Defaults to `1.2` + var lineHeightMultiple: Double = 1.2 /// A multiplier for setting the letter spacing, `1` being no spacing and /// `2` is one character of spacing between letters, defaults to `1`. @@ -87,7 +88,7 @@ extension SettingsData { self.lineHeightMultiple = try container.decodeIfPresent( Double.self, forKey: .lineHeightMultiple - ) ?? 1.45 + ) ?? 1.2 self.letterSpacing = try container.decodeIfPresent( Double.self, forKey: .letterSpacing @@ -167,6 +168,9 @@ extension SettingsData { /// The name of the custom font var name: String = "SF Mono" + /// The weight of the custom font + var weight: NSFont.Weight = .medium + /// Default initializer init() {} @@ -175,6 +179,7 @@ extension SettingsData { let container = try decoder.container(keyedBy: CodingKeys.self) self.size = try container.decodeIfPresent(Double.self, forKey: .size) ?? size self.name = try container.decodeIfPresent(String.self, forKey: .name) ?? name + self.weight = try container.decodeIfPresent(NSFont.Weight.self, forKey: .weight) ?? weight } /// Returns an NSFont representation of the current configuration. @@ -182,7 +187,58 @@ extension SettingsData { /// Returns the custom font, if enabled and able to be instantiated. /// Otherwise returns a default system font monospaced. var current: NSFont { - return NSFont(name: name, size: size) ?? NSFont.monospacedSystemFont(ofSize: size, weight: .medium) + let customFont = NSFont(name: name, size: size)?.withWeight(weight: weight) + return customFont ?? NSFont.monospacedSystemFont(ofSize: size, weight: .medium) + } + } +} + +extension NSFont { + /// Rough mapping from behavior of .systemFont(…weight:) + /// to NSFontManager's Int-based weight, as of 13.4 Ventura + func withWeight(weight: NSFont.Weight) -> NSFont? { + let fontManager = NSFontManager.shared + var intWeight: Int + + switch weight { + case .ultraLight: + intWeight=0 + case .light: + intWeight=2 + case .thin: + intWeight=3 + case .medium: + intWeight=6 + case .semibold: + intWeight=8 + case .bold: + intWeight=9 + case .heavy: + intWeight=10 + case .black: + intWeight=15 + default: + intWeight=5 } + + return fontManager.font( + withFamily: self.familyName ?? "", + traits: [], + weight: intWeight, + size: self.pointSize + ) + } +} + +extension NSFont.Weight: Codable { + public func encode(to encoder: Encoder) throws { + var container = encoder.singleValueContainer() + try container.encode(self.rawValue) + } + + public init(from decoder: Decoder) throws { + let container = try decoder.singleValueContainer() + let rawValue = try container.decode(CGFloat.self) + self = NSFont.Weight(rawValue: rawValue) } } diff --git a/CodeEdit/Features/Settings/Pages/TextEditingSettings/TextEditingSettingsView.swift b/CodeEdit/Features/Settings/Pages/TextEditingSettings/TextEditingSettingsView.swift index fc9b377fa4..186e929d7b 100644 --- a/CodeEdit/Features/Settings/Pages/TextEditingSettings/TextEditingSettingsView.swift +++ b/CodeEdit/Features/Settings/Pages/TextEditingSettings/TextEditingSettingsView.swift @@ -22,6 +22,7 @@ struct TextEditingSettingsView: View { Section { fontSelector fontSizeSelector + fontWeightSelector lineHeight letterSpacing } @@ -51,6 +52,10 @@ private extension TextEditingSettingsView { ) } + @ViewBuilder private var fontWeightSelector: some View { + FontWeightPicker(selection: $textEditing.font.weight) + } + @ViewBuilder private var autocompleteBraces: some View { Toggle(isOn: $textEditing.autocompleteBraces) { Text("Autocomplete braces") diff --git a/CodeEdit/Features/Settings/Views/FontWeightPicker.swift b/CodeEdit/Features/Settings/Views/FontWeightPicker.swift new file mode 100644 index 0000000000..92b50fccfb --- /dev/null +++ b/CodeEdit/Features/Settings/Views/FontWeightPicker.swift @@ -0,0 +1,46 @@ +// +// FontWeightPicker.swift +// CodeEdit +// +// Created by Austin Condiff on 6/13/24. +// + +import SwiftUI + +struct FontWeightPicker: View { + @Binding var selection: NSFont.Weight + var label: String? + + let fontWeights: [NSFont.Weight] = [ + .ultraLight, + .thin, + .light, + .regular, + .medium, + .semibold, + .bold, + .heavy, + .black + ] + + var weightNames: [NSFont.Weight: String] = [ + .ultraLight: "Ultra Light", + .thin: "Thin", + .light: "Light", + .regular: "Regular", + .medium: "Medium", + .semibold: "Semi Bold", + .bold: "Bold", + .heavy: "Heavy", + .black: "Black" + ] + + var body: some View { + Picker(label ?? "Font Weight", selection: $selection) { + ForEach(fontWeights, id: \.self) { weight in + Text(weightNames[weight] ?? "Unknown") + .tag(weight) + } + } + } +} From 58f6dd7d3221ae8891015b628ac7166702703e9f Mon Sep 17 00:00:00 2001 From: Austin Condiff Date: Fri, 14 Jun 2024 13:24:59 -0500 Subject: [PATCH 2/4] Fixed SwiftLint errors --- .../Settings/Pages/TerminalSettings/TerminalSettingsView.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CodeEdit/Features/Settings/Pages/TerminalSettings/TerminalSettingsView.swift b/CodeEdit/Features/Settings/Pages/TerminalSettings/TerminalSettingsView.swift index 239af6c17a..99c90fdd74 100644 --- a/CodeEdit/Features/Settings/Pages/TerminalSettings/TerminalSettingsView.swift +++ b/CodeEdit/Features/Settings/Pages/TerminalSettings/TerminalSettingsView.swift @@ -86,12 +86,11 @@ private extension TerminalSettingsView { format: .number ) } - + @ViewBuilder private var fontWeightSelector: some View { FontWeightPicker(selection: $settings.font.weight) } - @ViewBuilder private var injectionOptions: some View { VStack { Toggle("Shell Integration", isOn: $settings.useShellIntegration) From 48c226732f188d6ae699a2ed131e73cef5f91394 Mon Sep 17 00:00:00 2001 From: Austin Condiff Date: Fri, 14 Jun 2024 13:26:56 -0500 Subject: [PATCH 3/4] Fixed SwiftLint errors --- .../Pages/TerminalSettings/Models/TerminalSettings.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CodeEdit/Features/Settings/Pages/TerminalSettings/Models/TerminalSettings.swift b/CodeEdit/Features/Settings/Pages/TerminalSettings/Models/TerminalSettings.swift index 764e8f882f..38f18bb316 100644 --- a/CodeEdit/Features/Settings/Pages/TerminalSettings/Models/TerminalSettings.swift +++ b/CodeEdit/Features/Settings/Pages/TerminalSettings/Models/TerminalSettings.swift @@ -103,7 +103,7 @@ extension SettingsData { /// The name of the custom font var name: String = "SF Mono" - + /// The weight of the custom font var weight: NSFont.Weight = .medium From 67724cadac2bdf1ded4655dba1d5e0620ece852d Mon Sep 17 00:00:00 2001 From: Austin Condiff Date: Mon, 17 Jun 2024 12:29:50 -0500 Subject: [PATCH 4/4] Moved NSFont extension to its own file --- CodeEdit.xcodeproj/project.pbxproj | 4 ++ .../Models/NSFont+WithWeight.swift | 58 +++++++++++++++++++ .../Models/TextEditingSettings.swift | 50 ---------------- 3 files changed, 62 insertions(+), 50 deletions(-) create mode 100644 CodeEdit/Features/Settings/Pages/TextEditingSettings/Models/NSFont+WithWeight.swift diff --git a/CodeEdit.xcodeproj/project.pbxproj b/CodeEdit.xcodeproj/project.pbxproj index cc3adc37a2..44d4efee13 100644 --- a/CodeEdit.xcodeproj/project.pbxproj +++ b/CodeEdit.xcodeproj/project.pbxproj @@ -487,6 +487,7 @@ B6C6A42E29771A8D00A3D28F /* EditorTabButtonStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6C6A42D29771A8D00A3D28F /* EditorTabButtonStyle.swift */; }; B6C6A43029771F7100A3D28F /* EditorTabBackground.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6C6A42F29771F7100A3D28F /* EditorTabBackground.swift */; }; B6CFD80D2C1B9A8000E63F1A /* FontWeightPicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6CFD80C2C1B9A8000E63F1A /* FontWeightPicker.swift */; }; + B6CFD8112C20A8EE00E63F1A /* NSFont+WithWeight.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6CFD8102C20A8EE00E63F1A /* NSFont+WithWeight.swift */; }; B6D7EA592971078500301FAC /* InspectorSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6D7EA582971078500301FAC /* InspectorSection.swift */; }; B6D7EA5C297107DD00301FAC /* InspectorField.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6D7EA5B297107DD00301FAC /* InspectorField.swift */; }; B6E41C7029DD157F0088F9F4 /* AccountsSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6E41C6F29DD157F0088F9F4 /* AccountsSettingsView.swift */; }; @@ -1058,6 +1059,7 @@ B6C6A42D29771A8D00A3D28F /* EditorTabButtonStyle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EditorTabButtonStyle.swift; sourceTree = ""; }; B6C6A42F29771F7100A3D28F /* EditorTabBackground.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditorTabBackground.swift; sourceTree = ""; }; B6CFD80C2C1B9A8000E63F1A /* FontWeightPicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FontWeightPicker.swift; sourceTree = ""; }; + B6CFD8102C20A8EE00E63F1A /* NSFont+WithWeight.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSFont+WithWeight.swift"; sourceTree = ""; }; B6D7EA582971078500301FAC /* InspectorSection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InspectorSection.swift; sourceTree = ""; }; B6D7EA5B297107DD00301FAC /* InspectorField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InspectorField.swift; sourceTree = ""; }; B6E41C6F29DD157F0088F9F4 /* AccountsSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountsSettingsView.swift; sourceTree = ""; }; @@ -2975,6 +2977,7 @@ isa = PBXGroup; children = ( 58F2EAD8292FB2B0004A9BDE /* TextEditingSettings.swift */, + B6CFD8102C20A8EE00E63F1A /* NSFont+WithWeight.swift */, ); path = Models; sourceTree = ""; @@ -3495,6 +3498,7 @@ 58798284292ED0FB0085B254 /* TerminalEmulatorView.swift in Sources */, B6C4F2AC2B3CC4D000B2B140 /* CommitChangedFileListItemView.swift in Sources */, 6C82D6B329BFD88700495C54 /* NavigateCommands.swift in Sources */, + B6CFD8112C20A8EE00E63F1A /* NSFont+WithWeight.swift in Sources */, B66A4E4C29C9179B004573B4 /* CodeEditApp.swift in Sources */, 661EF7B82BEE215300C3E577 /* ImageFileView.swift in Sources */, 4E7F066629602E7B00BB3C12 /* CodeEditSplitViewController.swift in Sources */, diff --git a/CodeEdit/Features/Settings/Pages/TextEditingSettings/Models/NSFont+WithWeight.swift b/CodeEdit/Features/Settings/Pages/TextEditingSettings/Models/NSFont+WithWeight.swift new file mode 100644 index 0000000000..561ea4f712 --- /dev/null +++ b/CodeEdit/Features/Settings/Pages/TextEditingSettings/Models/NSFont+WithWeight.swift @@ -0,0 +1,58 @@ +// +// NSFont+WithWeight.swift +// CodeEdit +// +// Created by Austin Condiff on 6/17/24. +// + +import SwiftUI + +extension NSFont { + /// Rough mapping from behavior of .systemFont(…weight:) + /// to NSFontManager's Int-based weight, as of 13.4 Ventura + func withWeight(weight: NSFont.Weight) -> NSFont? { + let fontManager = NSFontManager.shared + var intWeight: Int + + switch weight { + case .ultraLight: + intWeight=0 + case .light: + intWeight=2 + case .thin: + intWeight=3 + case .medium: + intWeight=6 + case .semibold: + intWeight=8 + case .bold: + intWeight=9 + case .heavy: + intWeight=10 + case .black: + intWeight=15 + default: + intWeight=5 + } + + return fontManager.font( + withFamily: self.familyName ?? "", + traits: [], + weight: intWeight, + size: self.pointSize + ) + } +} + +extension NSFont.Weight: Codable { + public func encode(to encoder: Encoder) throws { + var container = encoder.singleValueContainer() + try container.encode(self.rawValue) + } + + public init(from decoder: Decoder) throws { + let container = try decoder.singleValueContainer() + let rawValue = try container.decode(CGFloat.self) + self = NSFont.Weight(rawValue: rawValue) + } +} diff --git a/CodeEdit/Features/Settings/Pages/TextEditingSettings/Models/TextEditingSettings.swift b/CodeEdit/Features/Settings/Pages/TextEditingSettings/Models/TextEditingSettings.swift index 5df879c7b4..019f1aeb23 100644 --- a/CodeEdit/Features/Settings/Pages/TextEditingSettings/Models/TextEditingSettings.swift +++ b/CodeEdit/Features/Settings/Pages/TextEditingSettings/Models/TextEditingSettings.swift @@ -192,53 +192,3 @@ extension SettingsData { } } } - -extension NSFont { - /// Rough mapping from behavior of .systemFont(…weight:) - /// to NSFontManager's Int-based weight, as of 13.4 Ventura - func withWeight(weight: NSFont.Weight) -> NSFont? { - let fontManager = NSFontManager.shared - var intWeight: Int - - switch weight { - case .ultraLight: - intWeight=0 - case .light: - intWeight=2 - case .thin: - intWeight=3 - case .medium: - intWeight=6 - case .semibold: - intWeight=8 - case .bold: - intWeight=9 - case .heavy: - intWeight=10 - case .black: - intWeight=15 - default: - intWeight=5 - } - - return fontManager.font( - withFamily: self.familyName ?? "", - traits: [], - weight: intWeight, - size: self.pointSize - ) - } -} - -extension NSFont.Weight: Codable { - public func encode(to encoder: Encoder) throws { - var container = encoder.singleValueContainer() - try container.encode(self.rawValue) - } - - public init(from decoder: Decoder) throws { - let container = try decoder.singleValueContainer() - let rawValue = try container.decode(CGFloat.self) - self = NSFont.Weight(rawValue: rawValue) - } -}