【发布时间】: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