Вопрос

I'm using iOS 7 text styles in my app. When the user changes them, I'd like to adapt my UI.

I understand I can register for UIContentSizeCategoryDidChangeNotification:

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

But I'm not really sure how to update the font size, other than something like this:

- (void)contentSizeCategoryDidChangeNotification:(NSNotification *)notification {
    _label1.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
    _label2.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
    _label3.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
    _label4.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
    _label5.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
    _label6.font = [UIFont preferredFontForTextStyle:UIFontTextStyleCaption1];
    [self.view setNeedsLayout];
    [self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationNone];
}

I don't really want to specify every font style in the code since I've already specified them in the storyboard, but I don't know what else I can do here. Any better solutions?

Это было полезно?

Решение

Given a font f, call:

NSString* style = [[f fontDescriptor] objectForKey:UIFontDescriptorTextStyleAttribute];

Now you can call [UIFont preferredFontForTextStyle:style] to generate the new font.

So, no, you don't have to hard-code the text style for every label into your code, but yes, you do have to set the font again for every label, because (get this) Dynamic Type is not dynamic.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top