skip to Main Content

I am implementing accessibility dynamic fonts for UILabel with custom font in my iOS project.

Looks like this:

let pointSize = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .footnote).pointSize
myLabel.font = UIFont(name: "my-custom-font", size: pointSize)

The issue is that this code is applied even if Larger Accessibility sizes is disabled in device settings, which is no good for me.

How could I apply this code only if Larger Accessibility sizes is enabled?

2

Answers


  1. You can check it in the following way:

    Objective-C code, you can convert it to swift. and have your own check as you need.

     +(BOOL)isCurrentPreferredContentSizeCategoryIsAccessibilityLarge
    {
        NSString *contentSize = [UIApplication sharedApplication].preferredContentSizeCategory;
        if([contentSize isEqualToString:UIContentSizeCategoryExtraExtraExtraLarge]
           || [contentSize isEqualToString:UIContentSizeCategoryAccessibilityMedium]
           || [contentSize isEqualToString:UIContentSizeCategoryAccessibilityLarge]
           || [contentSize isEqualToString:UIContentSizeCategoryAccessibilityExtraLarge]
           || [contentSize isEqualToString:UIContentSizeCategoryAccessibilityExtraExtraLarge]
           || [contentSize isEqualToString:UIContentSizeCategoryAccessibilityExtraExtraExtraLarge]) {
            return YES;
        }
        return NO;
    }
    

    The second way around it is to check this with, trait collections’s preferredContentSizeCategory property. You can respond to these changes in traitCollectionDidChange and make UI change accordingly.

    Login or Signup to reply.
  2. There is an easier way actually:

    func isLargerTextEnabled() {
       
        let contentSize = UIApplication.shared.preferredContentSizeCategory;
        let accessibilitySizeEnabled = contentSize.isAccessibilityCategory;
        print("Larger text: (contentSize) accessibility: (accessibilitySizeEnabled)")
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search