为什么 UIPageViewController 的 transition 会改变字体?

Why does UIPageViewController's transition change the font?

我有一个应用程序使用 UIPageViewController 在首次启动时提供快速指南。 UI 内置在故事板中 - 每个页面都有相同的笔尖和 VC,但文本会发生变化。 UI 标签在 Storyboard 中设置了自定义字体 (GothamRounded-Book),在预览中可以正确显示。 但是,当 运行 在设备上时,它们始终显示为系统字体。

经过一些调查,我在标签的字体 属性 中添加了一个观察者,它会在第一次出现时和页面发生变化时发生变化(水平滚动)。下面的日志记录和堆栈跟踪。

为什么?我怎样才能阻止它?

viewDidLoad: <UICTFont: 0x14783630> font-family: \"GothamRounded-Book\"; font-weight: normal; font-style: normal; font-size: 17.00pt

viewDidAppear: <UICTFont: 0x14783630> font-family: \"GothamRounded-Book\"; font-weight: normal; font-style: normal; font-size: 17.00pt

change: {
    kind = 1;
    new = "<UICTFont: 0x1478ab40> font-family: \".HelveticaNeueInterface-Regular\"; font-weight: normal; font-style: normal; font-size: 14.00pt";
    old = "<UICTFont: 0x14783630> font-family: \"GothamRounded-Book\"; font-weight: normal; font-style: normal; font-size: 17.00pt";
}

frame #1: 0x2b2d8eba Foundation`NSKeyValueNotifyObserver + 262
...
frame #6: 0x2b2f834e Foundation`-[NSObject(NSKeyValueCoding) setValue:forKeyPath:] + 310
frame #7: 0x2e053d20 UIKit`-[_UIAttributeTraitStorage applyRecordsMatchingTraitCollection:] + 296
frame #8: 0x2e053518 UIKit`-[NSObject(_UITraitStorageAccessors) _applyTraitStorageRecordsForTraitCollection:] + 164
frame #9: 0x2df6ade2 UIKit`-[UIView _traitCollectionDidChangeFromOldCollection:toNewCollection:scaleDidChange:] + 58
frame #10: 0x2df6aea0 UIKit`-[UIView _wrappedProcessDidChangeRecursivelyFromOldTraits:toCurrentTraits:scaleDidChange:forceNotification:] + 128
frame #11: 0x2df6b0a0 UIKit`__86-[UIView _processDidChangeRecursivelyFromOldTraits:toCurrentTraits:forceNotification:]_block_invoke + 44
frame #12: 0x2e2fe830 UIKit`-[UIView(AdditionalLayoutSupport) _withUnsatisfiableConstraintsLoggingSuspendedIfEngineDelegateExists:] + 120
frame #13: 0x2df6b068 UIKit`-[UIView _processDidChangeRecursivelyFromOldTraits:toCurrentTraits:forceNotification:] + 252
frame #14: 0x2dccafae UIKit`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 510
frame #15: 0x2d6ded28 QuartzCore`-[CALayer layoutSublayers] + 128
frame #16: 0x2d6da55c QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 360
frame #17: 0x2d6da3e4 QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 16
frame #18: 0x2d6d9d80 QuartzCore`CA::Context::commit_transaction(CA::Transaction*) + 224
frame #19: 0x2d6d9b6e QuartzCore`CA::Transaction::commit() + 434
frame #20: 0x2d6d3848 QuartzCore`CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 56
frame #21: 0x2a616fec CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 20
...
frame #28: 0x000fc840 MyApp`main(argc=1, argv=0x014a2a74) + 108 at main.m:14

Why?

字体自动调整不适用于自定义字体,这包括使用大小 类 或自动收缩设置。如果未使用 size 类,则 autshrink 无效。如果使用大小 类,自动收缩将始终处于活动状态并将字体重置为系统字体。我测试了 Xcode 6.4 和 Xcode 7b3.

How can I stop it?

如果使用任何动态字体大小调整,则仅在故事板中使用系统字体。 覆盖 viewDidLayoutSubviews 以设置您实际需要的字体。如果自定义字体的大小属性与系统字体明显不同,您将需要对计算出的大小进行更正。

- (void)viewDidLayoutSubviews {
    self.myLabel.font = [UIFont fontWithName:@"Custom" size:self.myLabel.font.pointSize];
}