Search code examples
iosfonts

API to detect iOS bold text is turned on?


My app pushes the limits of what can be displayed on the screen, such that turning on bold text breaks the display. I can imagine asking for the width of a character, and then using CGAffineTransform to slightly shrink the display. Is there a combination of iOS APIs that I can use to detect bold turned on?


Solution

  • Accessibility settings are one of the things that are stored in UITraitCollection. So, you can detect it via this method:

    if UITraitCollection.current.legibilityWeight == .bold {
        // Do your stuff here
    }
    

    Alternatively, if you're going to ignore the user's preferences (I don't know if smaller bolder text would be better than larger, less bold text, it depends entirely on the content you're using) you could use setOverrideTraitCollection from a containing view controller, copy the existing traits, but set the legibility weight to .regular.