【问题标题】:iOS 7 UIKit text sizingiOS 7 UIKit 文本大小
【发布时间】:2014-04-04 16:47:06
【问题描述】:

我在我的应用程序中使用 iOS 7 文本样式。当用户更改它们时,我想调整我的 UI。

我知道我可以注册UIContentSizeCategoryDidChangeNotification

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

但我不太确定如何更新字体大小,除了这样的:

- (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];
}

我真的不想在代码中指定每种字体样式,因为我已经在情节提要中指定了它们,但我不知道我还能在这里做什么。有更好的解决方案吗?

【问题讨论】:

    标签: ios7 uikit accessibility


    【解决方案1】:

    给定字体f,调用:

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

    现在您可以调用[UIFont preferredFontForTextStyle:style] 来生成新字体了。

    所以,不,您不必将每个标签的文本样式硬编码到代码中,但是是的,您必须为每个标签再次设置字体,因为(获取此)动态类型不是动态的。

    【讨论】:

    • 你在描述符上缺少一个 fontAttributes,对吧?
    • 一点也不。 preferredFontForTextStyle: 只接受一个参数,即我从现有字体中提取的参数。事实上,用于动态类型的 actual 字体是私人信息,你不应该看它。
    • 啊,好吧。我没有你的代码会编译,但它会。我以为你错过了[[f fontDescriptor] fontAttributes],但从文档objectForKey: 无论如何都只是映射了它。我的错。 :)
    • 我误解了你的问题,所以那里! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 2015-03-03
    • 2015-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多