English 中文(简体)
我如何利用快速通道在MacOS找到真象?
原标题:How do I get the font styles on MacOS using Swift?
  • 时间:2024-03-13 00:03:38
  •  标签:
  • swift
  • menu

我正试图为某个方面填满一个老体。 在下面的法典中,我曾尝试发表评论。 下面的法典正确地印刷了风格,但我可以让“ForEach()”工作。 GetFontStyles()海轮返回一系列阵列,内部阵列包含多个领域,包括字体名称、字体风格和一些身份识别号码。 注 成员3.1 用于拉动风格。

struct AddFontStyleItems: View {
    var dm: DrawModel
    init(_ dm: DrawModel ) { self.dm = dm }
    
    var body: some View {
    var fontStyles = getFontStyles(for: dm.curFont )
//      ForEach(fontStyles, id: .self) { curStyle in
//          Button( "xxx" ) {
//              print( "Style: (curStyle)")
//      //      print("Type of Member: (type(of: member)) - (member[1])")
//          }
//      }
        Button( "xxx" ) {
            let fontStyles = NSFontManager.shared.availableMembers(ofFontFamily: dm.curFont)
            print("-
Font: (dm.curFont)")
            
            if let members = fontStyles {
                for member in members {
                    print("Style for: (dm.curFont) - (member[1])")
                }
            }
        }
    }
}

func getFontStyles(for fontFamily: String) -> [String] {
     let fontManager = NSFontManager.shared

     guard let availableMembers = fontManager.availableMembers(ofFontFamily: fontFamily) else {
          return []
     }

     return availableMembers.compactMap { member in
         return member as? String
     }
}
问题回答

You could try this approach using a helper struct FontModel to hold the font results and make it easier to use in the ForEach loop.

struct ContentView: View {
    var body: some View {
        AddFontStyleItems(DrawModel(curFont: "Helvetica"))
    }
}

// for testing
struct DrawModel {
    var curFont: String
}

struct FontModel: Identifiable {  // <--- here
    let id = UUID()
    var fontType: String
    var fontName: String
    var fontWeight: Int
    var fontTrait: Int
    
    init(_ arr: [Any]) {
        if arr.count < 5 {
            self.fontType = arr[0] as? String ?? ""
            self.fontName = arr[1] as? String ?? ""
            self.fontWeight = arr[2] as? Int ?? 0
            self.fontTrait = arr[3] as? Int ?? 0
        } else {
            self.fontType = ""
            self.fontName = ""
            self.fontWeight = 0
            self.fontTrait = 0
        }
    }
}

struct AddFontStyleItems: View {
    var dm: DrawModel
    @State private var fontStyles: [FontModel] = [] // <--- here
    
    init(_ dm: DrawModel) { self.dm = dm }
    
    var body: some View {
        ForEach(fontStyles) { curStyle in  // <--- here
            Button(curStyle.fontName) {
                print("---> Style: (curStyle)")
            }
        }
        .onAppear {
            fontStyles = getFontStyles(for: dm.curFont) // <--- here
        }
    }
    
    func getFontStyles(for fontFamily: String) -> [FontModel] {
        var results: [FontModel] = []
        if let members = NSFontManager.shared.availableMembers(ofFontFamily: fontFamily) {
            for member in members {
                results.append(FontModel(member))
            }
        }
        return results
    }

}

EDIT-1:

如果只想一阵 of,

struct AddFontStyleItems: View {
    var dm: DrawModel
    @State private var fontStyles: [String] = [] // <--- here
    
    init(_ dm: DrawModel) { self.dm = dm }
    
    var body: some View {
        ForEach(fontStyles, id: .self) { curStyle in  // <--- here
            Button(curStyle) {
                print("---> Style: (curStyle)")
            }
        }
        .onAppear {
            fontStyles = getFontStyles(for: dm.curFont) // <--- here
        }
        .onChange(of: dm.curFont) { // <--- here
            fontStyles = getFontStyles(for: dm.curFont)
        }
    }
    
    func getFontStyles(for fontFamily: String) -> [String] {
        var results: [String] = []
        if let members = NSFontManager.shared.availableMembers(ofFontFamily: fontFamily) {
            for member in members {
                if member.count > 1 {
                    results.append(member[1] as? String ?? "")
                }
            }
        }
        return results
    }

}




相关问题
Delphi 2010 Action Manager & Main Menu Bar

I m trying to use the Action Manager and Action Main Menu Bar in Delphi 2010 an I have no idea how to make this work. I ve tried looking at the examples that come with Delphi 2010 and I can t seem to ...

asp.net (c#) menu/menuItem event/event-handling

Is it possible to use asp.net standard Menu (server-control) for tasks other than navigation? For example: I want to use asp.net menu to add a button control to the asp.net panel (server-control). ...

jQuery javascript menu?

Does anyone know how you create a menu with this rollover effect. Is it using jQuery? I want to create something similar using WordPress as my CMS. Would be great if there is a WordPress plugin? http:/...

jquery conditional logic for currentpage

I m using jQuery for a vertical site navigation menu, all links within site. I have the basic functionality working, but am at a loss as to the correct if-else to accomplish the following: As the ...

Links within and outside List elements not working

I m trying to create a unordered list, with each li element having it s own background image (no text, just image), but I m having trouble (in Firefox at least, it works in Safari) getting the link to ...

Horizontal menu with overflow-x:auto

Is it possible to have a horizontal menu that can scroll overflowed menuitems horizontally, but also display the vertical overflow when a submenu is to appear?

Bash Case menu - dynamic choices

I m not sure what the policy is here on asking followup questions. So please excuse me if i m breaking protocol. Earlier I was constructing a menu in bash ( Here ) And so far I ve got it working ...

热门标签