FontKit is a Swift utility designed to bridge the gap between SwiftUI and UIKit font management in iOS development. This tool provides a unified approach to handling font styling, including dynamic type and font designs like rounded, serif, and monospaced, focusing on the SF Pro system font. It ensures accessibility, consistency, and ease of use in typography across your entire app.
- Unified Font Management: Manage fonts in SwiftUI and UIKit with a single, versatile utility class. It is specifically optimized for the SF Pro system font.
- Dynamic Type Support: Fully supports Dynamic Type, allowing font sizes to adjust automatically based on the user's accessibility settings.
- Multiple Font Designs: Offers the ability to use various font designs, including
.rounded
,.serif
, and.monospaced
, enhancing the visual appeal of your app, especially with the SF Pro font family. - Customizable Font Weights: Provides flexibility in choosing font weights, from
.ultraLight
to.black
, to match different design needs. - Ease of Use: Simplifies the font selection and usage process in both SwiftUI and UIKit environments.
Text("Hello, SwiftUI!")
.font(FontKit.font(forTextStyle: .body, weight: .semibold, design: .rounded))
let label = UILabel()
label.font = FontKit.font(forTextStyle: .body, weight: .semibold, design: .rounded)
FontKit includes a set of methods and mappings that facilitate the use of fonts in both SwiftUI and UIKit. It abstracts away the complexities of handling different font types, text styles, and designs, providing a straightforward and consistent API.
font(forTextStyle:weight:design:)
: The primary method to retrieve fonts, accepting parameters for text style, weight, and design.TextStyle
: A custom enum mapping to SwiftUI's and UIKit's text styles.uiFontWeight(from:)
anduiFontDesign(from:)
: Helper methods for mapping SwiftUI types to their UIKit equivalents.
- iOS 13.0 or later for the full range of design features.
- Swift 5.1 or later.
- Xcode 11 or later.
-
Large Title with Heavy Weight in Rounded Style:
Text("Large Title") .font(FontKit.font(forTextStyle: .largeTitle, weight: .heavy, design: .rounded))
-
Body Text in Monospaced Design:
Text("Body Text") .font(FontKit.font(forTextStyle: .body, weight: .regular, design: .monospaced))
-
Caption Text in Serif Design:
Text("Caption Text") .font(FontKit.font(forTextStyle: .caption, weight: .light, design: .serif))
-
Title Label with Semibold Weight in Rounded Style:
let titleLabel = UILabel() titleLabel.font = FontKit.font(forTextStyle: .title1, weight: .semibold, design: .rounded) titleLabel.text = "Title Label"
-
Subheadline Label in Monospaced Design:
let subheadlineLabel = UILabel() subheadlineLabel.font = FontKit.font(forTextStyle: .subheadline, weight: .medium, design: .monospaced) subheadlineLabel.text = "Subheadline Label"
-
Footnote Label in Default System Design:
let footnoteLabel = UILabel() footnoteLabel.font = FontKit.font(forTextStyle: .footnote, weight: .regular) footnoteLabel.text = "Footnote Label"
- UltraLight:
.ultraLight
- Thin:
.thin
- Light:
.light
- Regular:
.regular
- Medium:
.medium
- Semibold:
.semibold
- Bold:
.bold
- Heavy:
.heavy
- Black:
.black
- Large Title:
.largeTitle
- Title:
.title
- Title2:
.title2
- Title3:
.title3
- Headline:
.headline
- Subheadline:
.subheadline
- Body:
.body
- Callout:
.callout
- Footnote:
.footnote
- Caption:
.caption
- Caption2:
.caption2
Designs applicable for iOS 13.0 and later:
- Default:
.default
- Rounded:
.rounded
- Serif:
.serif
- Monospaced:
.monospaced