Skip to content

Instantly share code, notes, and snippets.

@kristopherjohnson
Created January 4, 2020 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kristopherjohnson/c825cb97b1ad1fe0bc13d709986d0763 to your computer and use it in GitHub Desktop.
Save kristopherjohnson/c825cb97b1ad1fe0bc13d709986d0763 to your computer and use it in GitHub Desktop.
SwiftUI view that displays all available fonts in a scrolling list
import SwiftUI
import UIKit
/// Displays all available fonts in a vertically scrolling view.
struct FontsView: View {
private static let fontNames: [String] = {
var names = [String]()
for familyName in UIFont.familyNames {
names.append(contentsOf: UIFont.fontNames(forFamilyName: familyName))
}
return names.sorted()
}()
var body: some View {
ScrollView {
VStack(spacing: 8) {
ForEach(FontsView.fontNames, id: \.self) { fontName in
Text(fontName)
.font(Font.custom(fontName, size: 17))
}
}
.padding()
}
}
}
struct FontsView_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
FontsView()
.navigationBarTitle("Fonts", displayMode: .inline)
}
}
}
@kristopherjohnson
Copy link
Author

SwiftUIFontsView

Add this view to a SwiftUI app to get a list of all available fonts. This is included in my KJSwiftUI sample app: https://github.com/kristopherjohnson/KJSwiftUI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment