可以在设置中检测粗体文字设置>辅助功能?

时间:2014-04-27 21:29:43

标签: ios objective-c accessibility font-size

使用iOS 7,您可以对应用进行编码,以尊重用户对动态类型的设置 - 更大或更小的字体大小。您可以使用方法preferredFontForTextStyle:然后收听通知,以便在用户在您的应用运行时更改设置时更新用户界面。我想知道是否可以使用设置>中找到的辅助功能选项“粗体文字”做同样的事情。可访问性。我意识到Bold Text选项实际上要求您重新启动设备,因此不需要收听通知,因为您的应用程序将被杀死并重新启动。

这是我最终要完成的事情:我想将导航栏标题文字更改为较轻的字体。它可能不是默认的系统字体 - 它可能是iOS可以显示的任何字体,但我可能会使用HelveticaNeue-Light。我还想尊重用户对Bold Text的偏好。如果它已启用,我想将标题文本更改为相同字体的较重 - 就像iOS默认情况下那样,即使默认值已经非常重 - Helvetica Neue Medium。实际上它确实在启用时会使它更重一些。我想用不同的字体做同样的事情。

这就是我正在做的改变它,但无论粗体设置如何,这显然都会得到解决:

[self.navigationController.navigationBar setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"HelveticaNeue-Light" size:17],  [NSFontAttributeName, nil]];

我可能有一个解决方案,但这似乎是一个糟糕的方法。我正在为preferredFont subheadline制作一个固定大小的新字体。这几乎完全符合我的要求 - 它会根据粗体文本设置自动处理字体粗细(HelveticaNeueRegular [我实际上想要光线]禁用时,HelveticaNeueMedium启用时),但不适用于不同的字体。也许有更好的方法?

UIFont *subtitleFont = [UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline];
UIFont *titleFont = [subtitleFont fontWithSize:17];
[self.navigationController.navigationBar setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:titleFont, NSFontAttributeName, nil]];

5 个答案:

答案 0 :(得分:7)

从iOS 8开始,可以使用UIAccessibilityIsBoldTextEnabled()docs)和UIAccessibilityBoldTextStatusDidChangeNotificationdocs)检测用户是否在设置中启用了粗体文字。< / p>

对于同样需要iOS 7支持的应用程序,我写了一个优雅的单行程,适用于iOS 7&amp; 8与Helvetica Neue,甚至在iOS 9上使用旧金山字体,基于标准重量字体通常被称为“常规”重量这一事实,并且正文使用此重量来提高可读性:

<强>目标-C:

BOOL hasBoldText = ![[UIFont preferredFontForTextStyle:UIFontTextStyleBody].fontName hasSuffix:@"-Regular"];

<强>夫特:

let hasBoldText = !UIFont.preferredFontForTextStyle(UIFontTextStyleBody).fontName.hasSuffix("-Regular")

答案 1 :(得分:1)

您可以使用UIFontDescriptor

UIFontDescriptor *fontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleSubheadline];
UIFont *font = [UIFont fontWithDescriptor:fontDescriptor size:17]; // better to use a constant

如果要在字体大小更改时更改,可以观察UIApplicationContentSizeDidChangeNotification。我不确定粗体文字设置是否也会发送此通知,但您始终可以在applicationWillEnterForeground:上进行更新。 99%的时间你不必要地更新,但如果用户决定改变它,它应该有效。

答案 2 :(得分:0)

我找到了另一个解决方案。只需解析当前标题字体,看它是否包含子字符串&#39; bold&#39;如果找不到,那么您知道Bold Text已被禁用,您可以应用自定义字体。请注意,如果Apple更改了标题权重,这将停止工作。例如,将其缩小到常规和中等,而不是中等和粗体。如果Apple改变字体系列,你的固定字体显然不会匹配它。但它似乎并不是一个糟糕的解决方案。

UIFont *currentTitleFont = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
//if Bold Text is disabled
if ([currentTitleFont.fontName rangeOfString:@"bold" options:NSCaseInsensitiveSearch].location == NSNotFound) {
    UIFont *titleFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:17];
    [self.navigationController.navigationBar setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:titleFont, NSFontAttributeName, nil]];
}
else {
    //put custom font here for when Bold Text is enabled, or do nothing to get the default
}

答案 3 :(得分:0)

在iOS 7及更高版本中,我注意到UIFontTextStyleHeadline是:HelveticaNeueInterface-Heavy。

我修改了op的响应如下:

UIFont *currentTitleFont = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
//if Bold Text is disabled
if ([currentTitleFont.fontName rangeOfString:@"bold" options:NSCaseInsensitiveSearch].location == NSNotFound && [currentTitleFont.fontName rangeOfString:@"heavy" options:NSCaseInsensitiveSearch].location == NSNotFound) {
    UIFont *titleFont = [UIFont fontWithName:@"HelveticaNeue-Light" size:17];
    [self.navigationController.navigationBar setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:titleFont, NSFontAttributeName, nil]];
}
else {
    //put custom font here for when Bold Text is enabled, or do nothing to get the default
}

答案 4 :(得分:0)

尝试这个。它对我有用。

let hasBoldText = UIFont.preferredFont(forTextStyle: UIFont.TextStyle.body).fontName.contains("bold")

UIFont.preferredFont(forTextStyle:UIFont.TextStyle.body).fontName

11.4 12.0 “ .SFUIText” “ .SFUIText-Semibold”

13.3 “ .SFUI-常规” “ .SFUI-Semibold”