XCode6大小类中的自定义字体大小调整与自定义字体无法正常工作 [英] Custom Font Sizing in XCode6 Size Classes not working properly with Custom Fonts

查看:148
本文介绍了XCode6大小类中的自定义字体大小调整与自定义字体无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Xcode 6有一项新功能,可以根据故事板中的当前设备配置的大小类自动设置UILabel,UITextField和UIButton中的字体和字体大小。例如,您可以将UILabel设置为在任何宽度,紧凑高度(例如在横向上的iPhone上)配置上使用字体大小12,在常规宽度,常规高度配置(例如在iPad上)上使用大小18。更多信息请点击此处:

Xcode 6 has a new feature where fonts and font sizes in UILabel, UITextField, and UIButton can be set automatically based on the size class of the current device configuration, right in the storyboard. For example, you can set a UILabel to use font size 12 on "any width, compact height" (such as on iPhones in landscape) configurations and size 18 on "regular width, regular height" configurations (such as on iPads). More information is available here:

https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/Size-ClassSpecificLayout.html

理论上这是一个非常棒的功能,因为它可以使得不必根据设备配置以编程方式在UI功能上设置不同的字体。现在,我有一些条件代码根据设备类型设置字体,但显然这意味着我必须在整个应用程序的各个地方以编程方式设置字体。所以我最初对此功能感到非常兴奋,但我发现它在实际使用中存在严重问题(可能是一个bug)。请注意,我正在构建针对SDK 8并设置iOS 8的最低部署目标,因此这与与旧版iOS的兼容性无关。

This is a really great feature in theory because it could make it unnecessary to programmatically set different fonts on UI features based on the device configuration. Right now, I have some conditional code that sets the fonts based on the device type, but obviously that means I have to set the fonts programmatically everywhere throughout the app. So I was initially really excited about this feature, but I found that it has a serious problem in actual usage for me (perhaps a bug). Note that I am building against SDK 8 and setting a minimum deployment target of iOS 8 so this has nothing to do with compatibility with old versions of iOS.

问题是这个:
如果我为不同的大小类设置不同的字体大小,并使用iOS提供的系统字体,一切都按预期工作,字体大小根据大小类而变化。如果我使用我的应用程序提供的自定义字体(是的,我在应用程序包中正确设置,因为它以编程方式工作)并将自定义字体设置为XCode 6故事板中的标签,这也可以按预期工作。但是当我尝试为不同大小的类使用不同大小的自定义字体时,在故事板中,它突然不起作用。配置的唯一区别是我选择的字体(自定义字体与系统字体)。相反,所有字体都显示在设备和模拟器上作为默认大小的默认系统字体,无论大小等级(我通过调试器验证它是用系统字体替换故事板中指定的实际字体) 。所以基本上大小类功能似乎被自定义字体打破了。另外,有趣的是,自定义字体实际上在视图控制器的XCode 6预览窗格中正确显示和调整大小:只有在实际iOS系统上运行它才停止工作(这让我觉得我正在配置它正确)。

The problem is this: If I set different font sizes for different size classes, and use the "System" font provided by iOS, everything works as expected and the font sizes change based on the size class. If I use a custom font provided by my application (yes, I have it set up correctly in my application bundle, as it works programmatically) and set the custom font to a label in an XCode 6 storyboard, that also works as expected. But when I try to use different sizes of the custom font for different size classes, in the storyboard, it suddenly doesn't work. The only difference in configuration is the font I've chosen (a custom one vs. the System font). Instead, all of the fonts show up on the device and simulator as the default system font at the default size, regardless of size class (and I verified via the debugger that it is substituting the system font for the actual one specified in the storyboard). So basically the size class feature appears to be broken for custom fonts. Also, interestingly, the custom fonts actually display and adjust size properly in the XCode 6 "Preview" pane for the view controller: it's only when running on the actual iOS system that it stops working (which makes me think that I'm configuring it correctly).

我尝试了多种不同的自定义字体,它似乎不适用于任何一种,但如果我使用系统,它总是有效。

I tried multiple different custom fonts and it doesn't seem to work for any of them, but it always works if I use "System" instead.

无论如何,还有其他人在Xcode 6中看到过这个问题吗?关于这是否是iOS 8,Xcode中的错误或者我做错了什么的任何想法?我发现,我发现的唯一解决方法是继续以编程方式设置字体,就像我对大约3个版本的iOS一样,因为这确实有效。但我希望能够使用此功能,如果我可以使用自定义字体。我们的设计不接受使用系统字体。

Anyway, has anyone else seen this problem in Xcode 6? Any ideas on whether this is a bug in iOS 8, Xcode, or something I'm doing wrong? The only workaround I've found, like I said, is to continue to programmatically set the fonts like I have been for about 3 versions of iOS, because that does work. But I'd love to be able to use this feature if I could get it to work with custom fonts. Using the System font is not acceptable for our design.

附加信息:
从Xcode 8.0开始,bug是固定的。

ADDITIONAL INFO: As of Xcode 8.0, bug is fixed.

推荐答案

快速修复:

1)将字体设置为System for size size

1) Set fonts as System for size classes

2)子类UILabel并覆盖layoutSubviews方法,如:

2) Subclass UILabel and override "layoutSubviews" method like:

- (void)layoutSubviews
{
  [super layoutSubviews];

   // Implement font logic depending on screen size
    if ([self.font.fontName rangeOfString:@"bold" options:NSCaseInsensitiveSearch].location == NSNotFound) {
        NSLog(@"font is not bold");
        self.font = [UIFont fontWithName:@"Custom regular Font" size:self.font.pointSize];
    } else {
        NSLog(@"font is bold");
        self.font = [UIFont fontWithName:@"Custom bold Font" size:self.font.pointSize];
    }

}

顺便说一下,它是一个非常方便的标志性字体技术

By the way, it is a very convenient technique for iconic fonts

这篇关于XCode6大小类中的自定义字体大小调整与自定义字体无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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