Reputation: 1
I am using a Text which I want use SwiftUI custom fonts that can be offered as custom font from Apple, but I do not know, how can I see those offers? I also tried in canvas, and I just could do some padding or Color change, I am trying to see the List of Fonts that SwiftUI offer to us as custom one.
struct ContentView: View {
var body: some View {
Text("Hello, world!")
.font(Font.custom("?", size: 20)) // <<: How can I know, what I can choose?
}
}
Update:
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)
}
}
}
Upvotes: 2
Views: 2162
Reputation: 28539
One easy way to find all the fonts that are available is to list them in the console.
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)")
}
}
}
}
This will print out something like this (this is a shortened list, the actual list is much longer):
Family: Academy Engraved LET Font names: ["AcademyEngravedLetPlain"]
Family: Al Nile Font names: ["AlNile", "AlNile-Bold"]
Family: American Typewriter Font names: ["AmericanTypewriter", "AmericanTypewriter-Light", "AmericanTypewriter-Semibold", "AmericanTypewriter-Bold", "AmericanTypewriter-Condensed", "AmericanTypewriter-CondensedLight", "AmericanTypewriter-CondensedBold"]
Family: Apple Color Emoji Font names: ["AppleColorEmoji"]
Family: Apple SD Gothic Neo Font names: ["AppleSDGothicNeo-Regular", "AppleSDGothicNeo-Thin", "AppleSDGothicNeo-UltraLight", "AppleSDGothicNeo-Light", "AppleSDGothicNeo-Medium", "AppleSDGothicNeo-SemiBold", "AppleSDGothicNeo-Bold"]
Family: Apple Symbols Font names: ["AppleSymbols"]
The names inside the []
are the fonts that are available to you
Or you can do something like this to show the fonts in your view:
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))
}
}
}
}
}
Upvotes: 4
Reputation: 54641
If you want to list all available fonts programmatically, you can use UIFont.familyNames
.
Here is an example:
let fontFamilyNames = UIFont.familyNames
for familyName in fontFamilyNames {
print("Font Family Name = [\(familyName)]")
let names = UIFont.fontNames(forFamilyName: familyName)
print("Font Names = [\(names)]")
}
If you also want to see them, you can go to: http://iosfonts.com.
The ideal solution is to combine both these approaches: choose the font you like basing on its design etc, then list all available fonts to find its real name.
Upvotes: 1