Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessibility Large Text from Settings

When I try to implement user selected font of text from settings accessibility in my application, unable to get the user selected text size into my app. code is like

let contentSize = UIApplication.sharedApplication().preferredContentSizeCategory

preferredContentSizeCategory always returning same font like UICTContentSizeCategoryL even after changing in setting/accessibility

like image 958
Lakshmi Reddy Avatar asked Dec 19 '22 17:12

Lakshmi Reddy


1 Answers

add the following code to your viewDidLoad to get notified when font size is changed from settings accessibility.

[[NSNotificationCenter defaultCenter]
                              addObserver:self
                                 selector:@selector(preferredContentSizeChanged:)
                                     name:UIContentSizeCategoryDidChangeNotification
                                   object:nil];

Next, add the following method:

- (void)preferredContentSizeChanged:(NSNotification *)notification {
    self.textView.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
}

This simply sets the text view font to one based on the new preferred size.

Note: You might be wondering why it seems you’re setting the font to the same value it had before. When the user changes their preferred font size, you must request the preferred font again; it won’t be updated automatically. The font returned via preferredFontForTextStyle: will be different when the font preferences are changed.

EDIT:

Refer to this post: https://stackoverflow.com/a/20510095/5756850

Prefer using real device to test [UIApplication sharedApplication].preferredContentSizeCategory;

like image 102
Dinesh Avatar answered Jan 02 '23 19:01

Dinesh