1
0
mirror of https://github.com/mastodon/mastodon-ios.git synced 2024-12-12 16:46:32 +01:00
mastodon-app-ufficiale-ipho.../MastodonSDK/Sources/CoreDataStack/Extension/UIFont.swift
2022-12-29 00:47:53 +09:00

36 lines
1.3 KiB
Swift

//
// UIFont.swift
// CoreDataStack
//
// Created by sxiaojian on 2021/1/28.
//
import UIKit
extension UIFont {
// refs: https://stackoverflow.com/questions/26371024/limit-supported-dynamic-type-font-sizes
static func preferredFont(withTextStyle textStyle: UIFont.TextStyle, maxSize: CGFloat) -> UIFont {
// Get the descriptor
let fontDescriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: textStyle)
// Return a font with the minimum size
return UIFont(descriptor: fontDescriptor, size: min(fontDescriptor.pointSize, maxSize))
}
public static func preferredMonospacedFont(withTextStyle textStyle: UIFont.TextStyle, compatibleWith traitCollection: UITraitCollection? = nil) -> UIFont {
let fontDescription = UIFontDescriptor.preferredFontDescriptor(withTextStyle: textStyle).addingAttributes([
UIFontDescriptor.AttributeName.featureSettings: [
[
UIFontDescriptor.FeatureKey.type:
kNumberSpacingType,
UIFontDescriptor.FeatureKey.selector:
kMonospacedNumbersSelector
]
]
])
return UIFontMetrics(forTextStyle: textStyle).scaledFont(for: UIFont(descriptor: fontDescription, size: 0), compatibleWith: traitCollection)
}
}