diff --git a/FontFace.xcodeproj/project.pbxproj b/FontFace.xcodeproj/project.pbxproj index aaea663..920ec7f 100644 --- a/FontFace.xcodeproj/project.pbxproj +++ b/FontFace.xcodeproj/project.pbxproj @@ -13,6 +13,9 @@ 5705571D262C5EA1004A1FE3 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 57055708262C5EA0004A1FE3 /* ContentView.swift */; }; 5705571E262C5EA1004A1FE3 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 57055709262C5EA1004A1FE3 /* Assets.xcassets */; }; 5705571F262C5EA1004A1FE3 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 57055709262C5EA1004A1FE3 /* Assets.xcassets */; }; + 5705572E262C5F8B004A1FE3 /* FontModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5705572D262C5F8B004A1FE3 /* FontModel.swift */; }; + 57055733262C602A004A1FE3 /* CharacterCellView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 57055732262C602A004A1FE3 /* CharacterCellView.swift */; }; + 5705573A262C61DF004A1FE3 /* NSCharacterSetExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 57055739262C61DF004A1FE3 /* NSCharacterSetExtension.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -24,6 +27,9 @@ 57055716262C5EA1004A1FE3 /* FontFace.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = FontFace.app; sourceTree = BUILT_PRODUCTS_DIR; }; 57055718262C5EA1004A1FE3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 57055719262C5EA1004A1FE3 /* macOS.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = macOS.entitlements; sourceTree = ""; }; + 5705572D262C5F8B004A1FE3 /* FontModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FontModel.swift; sourceTree = ""; }; + 57055732262C602A004A1FE3 /* CharacterCellView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CharacterCellView.swift; sourceTree = ""; }; + 57055739262C61DF004A1FE3 /* NSCharacterSetExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSCharacterSetExtension.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -57,6 +63,9 @@ 57055706262C5EA0004A1FE3 /* Shared */ = { isa = PBXGroup; children = ( + 57055738262C61CF004A1FE3 /* Utils */, + 57055731262C600D004A1FE3 /* View */, + 5705572C262C5F4D004A1FE3 /* Model */, 57055707262C5EA0004A1FE3 /* FontFaceApp.swift */, 57055708262C5EA0004A1FE3 /* ContentView.swift */, 57055709262C5EA1004A1FE3 /* Assets.xcassets */, @@ -90,6 +99,30 @@ path = macOS; sourceTree = ""; }; + 5705572C262C5F4D004A1FE3 /* Model */ = { + isa = PBXGroup; + children = ( + 5705572D262C5F8B004A1FE3 /* FontModel.swift */, + ); + path = Model; + sourceTree = ""; + }; + 57055731262C600D004A1FE3 /* View */ = { + isa = PBXGroup; + children = ( + 57055732262C602A004A1FE3 /* CharacterCellView.swift */, + ); + path = View; + sourceTree = ""; + }; + 57055738262C61CF004A1FE3 /* Utils */ = { + isa = PBXGroup; + children = ( + 57055739262C61DF004A1FE3 /* NSCharacterSetExtension.swift */, + ); + path = Utils; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -198,6 +231,9 @@ files = ( 5705571D262C5EA1004A1FE3 /* ContentView.swift in Sources */, 5705571B262C5EA1004A1FE3 /* FontFaceApp.swift in Sources */, + 5705572E262C5F8B004A1FE3 /* FontModel.swift in Sources */, + 5705573A262C61DF004A1FE3 /* NSCharacterSetExtension.swift in Sources */, + 57055733262C602A004A1FE3 /* CharacterCellView.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/FontFace.xcodeproj/project.xcworkspace/xcuserdata/lvjun.xcuserdatad/UserInterfaceState.xcuserstate b/FontFace.xcodeproj/project.xcworkspace/xcuserdata/lvjun.xcuserdatad/UserInterfaceState.xcuserstate index 0db8a71..959a580 100644 Binary files a/FontFace.xcodeproj/project.xcworkspace/xcuserdata/lvjun.xcuserdatad/UserInterfaceState.xcuserstate and b/FontFace.xcodeproj/project.xcworkspace/xcuserdata/lvjun.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/Shared/ContentView.swift b/Shared/ContentView.swift index 6dd4429..55c78b3 100644 --- a/Shared/ContentView.swift +++ b/Shared/ContentView.swift @@ -8,12 +8,56 @@ import SwiftUI struct ContentView: View { + + let columns = [ + GridItem(.flexible()), + GridItem(.flexible()), + GridItem(.flexible()), + GridItem(.flexible()), + GridItem(.flexible()), + GridItem(.flexible()), + ] + var body: some View { - Text("Hello, world!") - .padding() + ZStack(alignment: .top) { + ScrollView { + LazyVGrid(columns: columns) { + ForEach(fetchFontList(), id: \.self) { characterModel in + CharacterCellView(name: "PingFang", characterModel: characterModel) + } + } + } + } + .padding() + .frame(minWidth: 600, idealWidth: 700, minHeight: 400, idealHeight: 800, alignment: .center) + .toolbar { + ToolbarItem(placement: .primaryAction) { + Menu { + + } label: { + Image(systemName: "square.and.arrow.up.on.square.fill") + } + } + } } } +/// 获取字符集及其 Unicode +/// - Returns: StringCharacter +func fetchFontList() -> [CharacterModel] { + if let name = NSFontManager.shared.availableFontFamilies.first { + + if let font = NSFontManager.shared.font(withFamily: name, traits: .boldFontMask, weight: 1, size: 30) { + + let set = font.coveredCharacterSet as NSCharacterSet + + return set.characters + } + } + + return [] +} + struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() diff --git a/Shared/Model/FontModel.swift b/Shared/Model/FontModel.swift new file mode 100644 index 0000000..96183ec --- /dev/null +++ b/Shared/Model/FontModel.swift @@ -0,0 +1,22 @@ +// +// FontModel.swift +// FontFace (macOS) +// +// Created by 吕俊 on 2021/4/18. +// + +import Foundation + +struct FontModel: Hashable { + var name: String + var characters: [CharacterModel] +} + +struct CharacterModel: Hashable { + var character: String + var unicode: String +} + +let mockFontModel = FontModel(name: "PingFang", characters: [mockCharacterModel]) + +let mockCharacterModel = CharacterModel(character: "T", unicode: "\u{e638}") diff --git a/Shared/Utils/NSCharacterSetExtension.swift b/Shared/Utils/NSCharacterSetExtension.swift new file mode 100644 index 0000000..f10bc1d --- /dev/null +++ b/Shared/Utils/NSCharacterSetExtension.swift @@ -0,0 +1,33 @@ +// +// NSCharacterSetExtension.swift +// FontFace (macOS) +// +// Created by 吕俊 on 2021/4/18. +// + +import Foundation + +extension NSCharacterSet { + var characters:[CharacterModel] { + var results = [CharacterModel]() + for plane:UInt8 in 0...16 { + if self.hasMemberInPlane(plane) { + let p0 = UInt32(plane) << 16 + let p1 = (UInt32(plane) + 1) << 16 + for c:UTF32Char in p0..