6

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

Ekta Padaliya
  • 5,743
  • 3
  • 39
  • 51
Lakshmi Reddy
  • 313
  • 5
  • 17

1 Answers1

13

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;

Community
  • 1
  • 1
Dinesh
  • 1,137
  • 9
  • 14
  • Thanks for your response, your code always returns ** font-family: ".SFUIText-Regular"; font-weight: normal; font-style: normal; font-size: 14.00pt** even after text size changes in accessibility. My aim is to get lower font if i select less in accessibility and more font if i select more in accessibility text size. Do you have any idea ? – Lakshmi Reddy Apr 14 '16 at 05:56
  • Hey, please check here : (Also prefer using real device to test this) http://stackoverflow.com/a/20510095/5756850 – Dinesh Apr 14 '16 at 07:08
  • 1
    Yes, I followed that link, but **[UIApplication sharedApplication].preferredContentSizeCategory** always returns same value . – Lakshmi Reddy Apr 14 '16 at 11:36