Skip to content

Instantly share code, notes, and snippets.

@westerlund
Last active July 17, 2022 18:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westerlund/f715f6f7c394fef56dbceb0286755320 to your computer and use it in GitHub Desktop.
Save westerlund/f715f6f7c394fef56dbceb0286755320 to your computer and use it in GitHub Desktop.
I made a really simple helper for using Dynamic Type with your own fonts
struct FontSizesHelper {
static func dynamicSize(for originalSize: CGFloat, category: UIContentSizeCategory = UIApplication.shared.preferredContentSizeCategory) -> CGFloat {
let modifyBy: CGFloat = {
switch category {
case .extraSmall: return -3
case .small: return -2
case .medium: return -1
case .large: return 0
case .extraLarge: return 2
case .extraExtraLarge: return 4
case .extraExtraExtraLarge: return 6
default: return 10
}
}()
let minPossibleFontSize: CGFloat = 8
return max(minPossibleFontSize, originalSize + modifyBy)
}
static let sizes: [UIFontTextStyle: [UIContentSizeCategory: CGFloat]] = [
.headline: [
.extraSmall: 17,
.small: 18,
.medium: 19,
.large: 20,
.extraLarge: 21,
.extraExtraLarge: 22,
.extraExtraExtraLarge: 23,
],
.subheadline: [
.extraSmall: 15,
.small: 16,
.medium: 17,
.large: 18,
.extraLarge: 19,
.extraExtraLarge: 20,
.extraExtraExtraLarge: 21,
],
.body: [
.extraSmall: 12,
.small: 13,
.medium: 14,
.large: 15,
.extraLarge: 16,
.extraExtraLarge: 17,
.extraExtraExtraLarge: 18,
],
.caption1: [
.extraSmall: 12,
.small: 12,
.medium: 13,
.large: 14,
.extraLarge: 15,
.extraExtraLarge: 16,
.extraExtraExtraLarge: 16,
],
.caption2: [
.extraSmall: 10,
.small: 11,
.medium: 12,
.large: 12,
.extraLarge: 12,
.extraExtraLarge: 13,
.extraExtraExtraLarge: 14,
],
.footnote: [
.extraSmall: 10,
.small: 10,
.medium: 11,
.large: 11,
.extraLarge: 12,
.extraExtraLarge: 12,
.extraExtraExtraLarge: 13,
]
]
static func size(for style: UIFontTextStyle, with contentSizeCategory: UIContentSizeCategory) -> CGFloat {
return sizes[style]?[contentSizeCategory] ?? 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment