How to test Dynamic Type (larger font sizes) in iO

2019-02-10 01:18发布

Changing Dynamic Type settings in iOS can be done manually (Settings > General > Accessibility > Larger Text).

But this does not appear to work in the current Simulator (v9.3 at the time of writing), and doing so manually is not a solution where automation is required.

Question: Is there a way to launch an app with Dynamic Type settings to launch an app with larger font sizes?

Note: This is not only useful for UI testing, but also for the purpose of creating screenshots using Fastlane Snapshot with larger font sizes.

5条回答
成全新的幸福
2楼-- · 2019-02-10 01:24

Although (to my knowledge) not documented, a launch argument can be used to define the initial Dynamic Type setting.

Key: UIPreferredContentSizeCategoryName

Value: One of the below

  • UICTContentSizeCategoryXS
  • UICTContentSizeCategoryS
  • UICTContentSizeCategoryM
  • UICTContentSizeCategoryL
  • UICTContentSizeCategoryXL
  • UICTContentSizeCategoryXXL
  • UICTContentSizeCategoryXXXL
  • UICTContentSizeCategoryAccessibilityM
  • UICTContentSizeCategoryAccessibilityL
  • UICTContentSizeCategoryAccessibilityXL
  • UICTContentSizeCategoryAccessibilityXXL
  • UICTContentSizeCategoryAccessibilityXXXL

Source of this undocumented key/values: GitHub.

A convenient way to do so is to add a launch argument to an Xcode scheme (add for example -UIPreferredContentSizeCategoryName UICTContentSizeCategoryXL under Arguments Passed On Launch), and this setting will apply when you run the app in Xcode. You can create multiple schemes with different Dynamic Type settings to switch between them.

If the purpose is creating screenshots using Fastlane Snapshot, the equivalent code is as follows:

app.launchArguments += [ "-UIPreferredContentSizeCategoryName", "UICTContentSizeCategoryXL" ]
app.launch()
查看更多
家丑人穷心不美
3楼-- · 2019-02-10 01:29

Before doing this in Accessibility Inspector, don't forget to turn on "Larger Text" in your simulated device's settings: General -> Accessibility -> Larger Font. Else, the Accessibility Inspector won't show the dynamic font option.

enter image description here

查看更多
可以哭但决不认输i
4楼-- · 2019-02-10 01:34

As of Xcode 8, there is a better option than the undocumented launch arguments: Accessibility Inspector. It is covered in this talk and can be found under Xcode > Open Developer Tool.

Xcode Accessibility Inspector

查看更多
姐就是有狂的资本
5楼-- · 2019-02-10 01:40

It is actually possible to overwrite the content category size at the application-level which is especially useful for unit and UI tests.

Check it out here: https://medium.com/livefront/practical-dynamic-type-part-2-testing-613bb845f26b

查看更多
看我几分像从前
6楼-- · 2019-02-10 01:48

As of iOS 10, it is possible to dynamically alter the dynamic type choice using the UITraitCollection API. See https://stackoverflow.com/a/43983241/1148702 for more info.

查看更多
登录 后发表回答