【问题标题】:How can I see other options of Font that SwiftUI can offer as custom font?如何查看 SwiftUI 可以作为自定义字体提供的其他字体选项?
【发布时间】:2021-03-04 14:40:58
【问题描述】:

我正在使用我想要使用 SwiftUI 自定义字体的文本,该字体可以作为 Apple 的自定义字体提供,但我不知道,我如何才能看到这些优惠?我也在画布中尝试过,我只是可以做一些填充或颜色更改,我试图将 SwiftUI 提供给我们的字体列表作为自定义字体查看。

struct ContentView: View {
    var body: some View {
        Text("Hello, world!")
            .font(Font.custom("?", size: 20)) // <<: How can I know, what I can choose?
            
    }
}

更新:

import SwiftUI


let customFonts: (allKinds: [String], allFonts: [String]) = allCustomFontsFinder()

func allCustomFontsFinder() -> (allKinds: [String], allFonts: [String]) {
    
    let allKinds: [String] = UIFont.familyNames.sorted()
    var allFonts: [String] = [String]()
    
    allKinds.forEach { familyItem in
        
        UIFont.fontNames(forFamilyName: familyItem).forEach { item in allFonts.append(item) }
        
    }
    
    return (allKinds: allKinds, allFonts: allFonts)
    
}







struct ContentView: View {
    
    var body: some View {
        
        List {
            
            ForEach(customFonts.allFonts, id: \.self) { item in
                
                HStack {
                    
                    Text(item)
                        .font(Font.custom(item, size: 20))
                        .onTapGesture { print(item) }
                    
                    Spacer()
                    
                    Button(action: { let pasteboard = UIPasteboard.general; pasteboard.string = item }, label: {
                        Image(systemName: "doc.on.doc")
                    })
                    
                }
                
            }
            
        }
        .padding()
        .onAppear() {
            
            print("count Of CustomFontKinds:", customFonts.allKinds.count)
            print("count Of AllCustomFonts:", customFonts.allFonts.count)
            
        }
        
    }
}

【问题讨论】:

  • 您想(直观地)查看它们还是以编程方式访问它们?
  • 两者都很好,但我需要知道我们能做什么

标签: swiftui


【解决方案1】:

查找所有可用字体的一种简单方法是在控制台中列出它们。

struct ContentView: View {
    var body: some View {
        Text("Show the fonts")
            .onAppear {
                for family in UIFont.familyNames.sorted() {
                    let names = UIFont.fontNames(forFamilyName: family)
                    print("Family: \(family) Font names: \(names)")
                }
            }
    }
}

这将打印出类似这样的内容(这是一个缩短的列表,实际的列表要长得多):

Family:Academy Engraved LET 字体名称:["AcademyEngravedLetPlain"]

Family: Al Nile 字体名称:["AlNile", "AlNile-Bold"]

Family: American Typewriter 字体名称:["AmericanTypewriter", “AmericanTypewriter-Light”、“AmericanTypewriter-Semibold”、 “AmericanTypewriter-Bold”、“AmericanTypewriter-Condensed”、 “AmericanTypewriter-CondensedLight”, "AmericanTypewriter-CondensedBold"]

系列:Apple Color Emoji 字体名称:["AppleColorEmoji"]

系列:Apple SD Gothic Neo 字体名称:["AppleSDGothicNeo-Regular", “AppleSDGothicNeo-Thin”、“AppleSDGothicNeo-UltraLight”、 “AppleSDGothicNeo-Light”、“AppleSDGothicNeo-Medium”、 "AppleSDGothicNeo-SemiBold", "AppleSDGothicNeo-Bold"]

系列:Apple Symbols 字体名称:["AppleSymbols"]

[] 中的名称是您可以使用的字体

或者你可以做这样的事情来在你的视图中显示字体:

struct ContentView: View {
    var body: some View {
        ScrollView{
            ForEach(UIFont.familyNames.sorted(), id: \.self) { family in
                let names = UIFont.fontNames(forFamilyName: family)
                ForEach(names, id: \.self) { name in
                    Text(name).font(Font.custom(name, size: 20))
                }
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    以编程方式

    如果您想以编程方式列出所有可用字体,可以使用UIFont.familyNames

    Here 是一个例子:

    let fontFamilyNames = UIFont.familyNames
    
    for familyName in fontFamilyNames {
        print("Font Family Name = [\(familyName)]")
        let names = UIFont.fontNames(forFamilyName: familyName)
        print("Font Names = [\(names)]")
    }
    

    视觉上

    如果你也想看到他们,你可以去:http://iosfonts.com


    理想的解决方案是结合这两种方法:根据设计等选择您喜欢的字体,然后列出所有可用的字体以找到其真实名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-01
      • 1970-01-01
      • 2020-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多