Created
March 5, 2022 21:49
-
-
Save thejohnlima/33c2b3ff78dcb4dcf5ad8f932d5aef98 to your computer and use it in GitHub Desktop.
Extensions to help using custom fonts in SwiftUI and UIKit
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 | |
/// Custom fonts | |
enum CustomFont: String { | |
case primary = "Topsicle" | |
case secondary = "SpaceAndAstronomy" | |
case tertiary = "Velezodiac" | |
} | |
extension UIFont { | |
/// Get the Scaled version of custom UIFont. | |
/// | |
/// - Parameters: | |
/// - name: Name of the UIFont whose scaled version you wish to obtain. | |
/// - textStyle: The text style for your font, i.e Body, Title etc... | |
/// - size: The font's size. Default value is `nil` | |
/// - Returns: The scaled UIFont version with the given textStyle | |
static func scaledFont(forFont name: CustomFont, textStyle: UIFont.TextStyle, size: CGFloat? = nil) -> UIFont { | |
let userFont = UIFontDescriptor.preferredFontDescriptor(withTextStyle: textStyle) | |
let pointSize = size ?? userFont.pointSize | |
return UIFontMetrics.default.scaledFont(for: custom(font: name, size: pointSize)) | |
} | |
/// Custom font for UIKit | |
/// - Parameters: | |
/// - font: Custom font | |
/// - size: Font size | |
/// - Returns: Result font | |
static func custom(font: CustomFont, size: CGFloat) -> UIFont { | |
guard let customFont = UIFont(name: font.rawValue, size: size) else { | |
fatalError( | |
""" | |
\n❌ Failed to load the "\(font.rawValue)" font. | |
Make sure the font file is included in the project and the font name is spelled correctly.\n | |
""" | |
) | |
} | |
return customFont | |
} | |
} | |
extension Font { | |
/// Custom font for SwiftUI | |
/// - Parameters: | |
/// - font: Custom font | |
/// - size: Font size | |
/// - Returns: Result font | |
static func custom(font: CustomFont, size: CGFloat) -> Font { | |
Font.custom(font.rawValue, size: size) | |
} | |
} |
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 | |
struct InfoView: View { | |
var body: some View { | |
VStack(alignment: .leading, spacing: 24) { | |
VStack(alignment: .leading, spacing: 8) { | |
Text("Aries") | |
.fontWeight(.semibold) | |
.textCase(.uppercase) | |
.font(.custom(font: .secondary, size: 17)) | |
.lineLimit(1) | |
.frame(maxWidth: .infinity, alignment: .leading) | |
Text("Cautious") | |
.frame(maxWidth: .infinity, alignment: .leading) | |
.font(.custom(font: .primary, size: 14)) | |
.opacity(0.7) | |
} | |
Text("MAR 21 - APR 20") | |
.textCase(.uppercase) | |
.font(.custom(font: .primary, size: 12)) | |
.foregroundColor(.quickSand) | |
} | |
.padding(4) | |
} | |
} | |
struct InfoViewPreviews: PreviewProvider { | |
static var previews: some View { | |
SignInfoView() | |
.preferredColorScheme(.dark) | |
.previewLayout(.sizeThatFits) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment