Created
January 4, 2020 22:12
-
-
Save kristopherjohnson/c825cb97b1ad1fe0bc13d709986d0763 to your computer and use it in GitHub Desktop.
SwiftUI view that displays all available fonts in a scrolling list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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