How to use a custom font with dynamic text sizes in iOS7

前端 未结 12 2013
长情又很酷
长情又很酷 2020-11-29 14:41

In iOS7 there are new API\'s for getting a font that is automatically adjusted to the text size the user has set in their preferences.

It looks something like this to

12条回答
  •  有刺的猬
    2020-11-29 15:30

    This is how I do it in Swift. I like this because it's more general, it only requires one table, and it should work well with any font. First I wrote a generalized multiplier (in a getter).

    var fontSizeMultiplier : CGFloat {
        get {
            switch UIApplication.sharedApplication().preferredContentSizeCategory {
            case UIContentSizeCategoryAccessibilityExtraExtraExtraLarge: return 23 / 16
            case UIContentSizeCategoryAccessibilityExtraExtraLarge: return 22 / 16
            case UIContentSizeCategoryAccessibilityExtraLarge: return 21 / 16
            case UIContentSizeCategoryAccessibilityLarge: return 20 / 16
            case UIContentSizeCategoryAccessibilityMedium: return 19 / 16
            case UIContentSizeCategoryExtraExtraExtraLarge: return 19 / 16
            case UIContentSizeCategoryExtraExtraLarge: return 18 / 16
            case UIContentSizeCategoryExtraLarge: return 17 / 16
            case UIContentSizeCategoryLarge: return 1.0
            case UIContentSizeCategoryMedium: return 15 / 16
            case UIContentSizeCategorySmall: return 14 / 16
            case UIContentSizeCategoryExtraSmall: return 13 / 16
            default: return 1.0
            }
        }
    }
    

    Then I update the font (e.g., in the observer) using a UIFontDescriptor like this:

    textView.font = UIFont(descriptor: fontDescriptor!, size: fontDescriptor!.pointSize * fontSizeMultiplier)
    

提交回复
热议问题