动态模拟iOS动态类型系统文本大小(UIContentSizeCategory) [英] Dynamically Mocking iOS Dynamic Type System Text Size (UIContentSizeCategory)

查看:215
本文介绍了动态模拟iOS动态类型系统文本大小(UIContentSizeCategory)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用不同的系统文字大小选项轻松测试我的应用,包括可访问性大小。这些可以在设置应用中设置(显示和亮度=>文字大小或常规=>辅助功能=>较大文本)。



我目前唯一可以找到的方法是进入设置并使用UI更改值(编辑:



我还使用命令行实用程序 nm <找到这些符号/ code> UIFoundation.framework 。它们标有小写字母s,显然是符号在小对象的未初始化数据部分。。我不知道这意味着什么(我收集到的只是他们不是一个班级或方法)。



在网上搜索 _getUIContentSizeCategory ... 符号不产生任何结果,但附近还有另一个符号 _getUIApplicationClass 。我搜索了那个,因为它听起来更一般,并在一些 WebKit源。可能一无所获,但也许这是一个内部的苹果公约。无论如何,这个例子并没有真正帮助我解决问题。



无论如何,感谢您的阅读。如果你还在这里,我的问题是:



我希望能够动态模拟动态类型大小首选项的值。这些反汇编符号可能有所帮助,但也许我走错了路。感觉解决方案很接近,但我不能将所有部分组合在一起。



将此值设置为启动参数很好,但不能完全解决我的问题。同样,在 plist 中修改模拟器的值也很适合自动化,但不能解决我的问题。



有没有办法在运行时动态改变这个值?

解决方案

多么尴尬!我正在查看一些过时的iOS 9.x文档并错过了 UITraitCollection 获得 init(preferredContentSizeCategory:UIContentSizeCategory) var preferredContentSizeCategory:iOS 10中的UIContentSizeCategory Brandon Williams 。这完全满足了我的需求。


I'd like to easily test my app with different selections of system text size, including accessibility sizes. These can be set in the Settings app (Display & Brightness => Text Size or General => Accessibility => Larger Text).

The only way I can find to currently do this is to go into Settings and change the value with the UI (edit: partial solution described below). This is slow and cumbersome. I suspect there's a way to dynamically alter it using private APIs, but I can't figure out how. Since my goal is to only use this for debugging, private API use and swizzling is fine (this code won't be making its way to production).

To try to find a private API to do this, I looked at some reverse engineering resources. I'm new to disassemblers, symbol tables, class dumps, and finding private APIs I can use, but this is what I've tried so far:

  • I successfully swizzled -[UIApplication preferredContentSizeCategory] (other posts said this worked in the past), but this doesn't affect the result returned from +[UIFont preferredFontForTextStyle:].

  • Using the disassembler IDA I discovered that +[UIFont preferredFontForTextStyle:] is in the private framework UIFoundation.framework. (-[UIApplication preferredContentSizeCategory] is in UIKit.framework but the disassembly didn't look at all useful).

  • Just as I started writing this question (it always happens that way), I discovered a partial solution. One can set a launch argument in the scheme to set the value on launch. This is useful, but not what I'm after.

  • Through the above answer, I found the value for the user's preference is apparently stored in "the com.apple.UIKit.plist file located in the Simulator's data/Library/Preferences directory". The value we're after can be set from the command line utility plutil. This is also an improvement! But I'd like to dynamically alter it at runtime.

More on my results from IDA:

I don't really know how to read disassembly, but +[UIFont preferredFontForTextStyle:] seems to point to a symbol called ___UIFontForTextStyle which seems to point to some interesting-sounding symbols called _getUIContentSizeCategoryUnspecified and _getUIContentSizeCategoryPreferenceClass

I also found these symbols using the command-line utility nm on UIFoundation.framework. They were marked with the lowercase letter "s" meaning, apparently, "The symbol is in an uninitialized data section for small objects.". I have no idea what this means (all I've gathered is that they're not a class or method).

Searching the web for the _getUIContentSizeCategory... symbols yields nothing, but nearby there's another symbol _getUIApplicationClass. I searched for that one since it sounded a bit more general, and found something similar in some WebKit source. Could be nothing, but maybe it's an internal Apple convention. Regardless, the example doesn't really help me solve the problem.

Anyway thanks for reading so far. If you're still here, my question is:

I'd like to be able to dynamically mock the value for the dynamic type size preference. These disassembly symbols might help, but maybe I'm on the wrong track. It feels like the solution is close but I just can't put all the pieces together.

Setting this value as a launch argument is nice, but doesn't fully solve my problem. Similarly, modifying the Simulator's value in the plist is also nice for automation, but doesn't solve my problem.

Is there a way to dynamically alter this value at runtime?

解决方案

How embarrassing! I was looking at some out-of-date iOS 9.x documentation and missed that UITraitCollection gained init(preferredContentSizeCategory: UIContentSizeCategory) and var preferredContentSizeCategory: UIContentSizeCategory in iOS 10, which was helpfully pointed out to me by Brandon Williams. This addresses my needs perfectly.

这篇关于动态模拟iOS动态类型系统文本大小(UIContentSizeCategory)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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