Skip to content

UIKit에서 TextStyle, FontWeight를 함께 주는 방법이 있을까요? #83

Discussion options

You must be logged in to vote
import UIKit

extension UIFont {
    
    static func preferredFont(for style: TextStyle, weight: Weight, italic: Bool = false) -> UIFont {

        // Get the style's default pointSize
        let traits = UITraitCollection(preferredContentSizeCategory: .large)
        let desc = UIFontDescriptor.preferredFontDescriptor(withTextStyle: style, compatibleWith: traits)

        // Get the font at the default size and preferred weight
        var font = UIFont.systemFont(ofSize: desc.pointSize, weight: weight)
        if italic == true {
            font = font.with([.traitItalic])
        }

        // Setup the font to be auto-scalable
        let metrics = UIFontMetrics(forTextStyle: style…

Replies: 1 comment

Comment options

M1zz
Sep 14, 2022
Maintainer Author

You must be logged in to vote
0 replies
Answer selected by M1zz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant