可以在“设置”>中检测粗体字设置。辅助功能? [英] Possible to detect Bold Text setting in Settings > Accessibility?

查看:121
本文介绍了可以在“设置”>中检测粗体字设置。辅助功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS 7中,可以对您的应用进行编码以遵守用户对动态类型的设置-较大或较小的字体大小。您可以使用方法 preferredFontForTextStyle:,然后在用户运行应用程序时,如果用户更改了设置,则侦听通知以更新UI。我想知道是否可以使用设置>辅助功能中的辅助功能选项粗体来做同样的事情。我意识到,粗体文字选项实际上要求您重新启动设备,因此,您的应用将被终止并重新启动,因此无需监听通知。

With iOS 7, it's possible to code your app to respect the user's setting for Dynamic Type - larger or smaller font sizes. You use the method preferredFontForTextStyle: and then listen to notifications in order to update the UI if the user changes the setting while your app is running. I am wondering if it's possible to do the same thing with the accessibility option "Bold Text" found in Settings > Accessibility. I realized that the Bold Text option actually requires you to restart the device, so there should be no need to listen to notifications because your app will be killed and relaunched anyways.

这是我最终要完成的工作:我想将导航栏标题文本更改为较浅的样式字体。它可能不是默认的系统字体-它可能是iOS可以显示的任何字体,但我可能会使用HelveticaNeue-Light。我还要尊重用户对粗体字的偏爱。如果启用了此功能,我想将标题文本更改为具有相同字体的较粗字体-就像iOS默认情况下一样,即使默认字体已经很重-Helvetica Neue Medium。确实,启用后确实会使它重一些。我想用不同的字体来做同样的事情。

This is what I ultimately want to accomplish: I would like to change the navigation bar title text to a lighter style font. It may not be the default System font - it could be any font iOS can display, but I'll probably use HelveticaNeue-Light. I would also like to respect the user's preference for Bold Text. If it's enabled, I want to change the title text to a heavier weight of that same font - just like iOS does by default even though the default is already quite heavy - Helvetica Neue Medium. Indeed it does make it a little bit heavier when enabled. I want to do the same with a different font.

这是我要更改的字体,但是无论粗体设置是什么,这显然都是固定的:

Here's what I'm doing to change it, but this obviously will be fixed no matter what the bold setting is:

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

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

I may have a solution but it seems to be a bad approach. I'm making a new font with a fixed size from the preferredFont for subheadline. This does almost exactly what I want - it automatically takes care of font-weight based on the Bold Text setting (HelveticaNeueRegular [I actually want Light] when disabled, HelveticaNeueMedium when enabled), but won't work for a different typeface. Perhaps there is a better approach?

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


推荐答案

从iOS 8开始,可以检测是否用户已使用 UIAccessibilityIsBoldTextEnabled()文档)和 UIAccessibilityBoldTextStatusDidChangeNotification docs )。

As of iOS 8, it is possible to detect whether the user has enabled Bold Text in Settings using UIAccessibilityIsBoldTextEnabled() (docs) and UIAccessibilityBoldTextStatusDidChangeNotification (docs).

对于同样需要iOS 7支持的应用程序,我写了一篇优雅的文章,适用于iOS 7& Helvetica Neue甚至在iOS 9上使用San Francisco字体也是如此,这是基于以下事实:标准粗体字体通常被称为常规粗体,并且主体文本使用此粗体来提高可读性:

For apps that also require iOS 7 support, I’ve written an elegant one-liner that works on iOS 7 & 8 with Helvetica Neue and even on iOS 9 with the San Francisco typeface, based on the fact that standard-weight fonts are commonly referred to as the "Regular" weight, and that body text uses this weight for readability:

Objective-C:

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

Swift:

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

这篇关于可以在“设置”>中检测粗体字设置。辅助功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆