xcode scaledValueForValue:在没有设置文本样式的字体上调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25773661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:31:03  来源:igfitidea点击:

scaledValueForValue: called on a font that doesn't have a text style set

iosobjective-cxcodeios8xcode6

提问by Josh

I am currently using the Xcode 6 pre release (not beta) and the simulator on OS X 10.10 Yosemite beta 7. I am trying to build a project developed in xcode 6, but the app crashes whenever I open a certain view controller. This view controller literally has no code in it (it is an empty, static, table view controller that has a couple of default cells and labels).

我目前在 OS X 10.10 Yosemite beta 7 上使用 Xcode 6 预发行版(非测试版)和模拟器。我正在尝试构建一个在 xcode 6 中开发的项目,但是每当我打开某个视图控制器时应用程序就会崩溃。这个视图控制器实际上没有代码(它是一个空的、静态的、表视图控制器,有几个默认的单元格和标签)。

The error given is:

给出的错误是:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason:  
'scaledValueForValue: called on a font that doesn't have a text style set'

And right before I am given this assertion failure:

就在我得到这个断言失败之前:

*** Assertion failure in -[UICTFont _scaledValueForValue:],         
/SourceCache/UIFoundation_Sim/UIFoundation-371/UIFoundation/iOS/UIFont.m:496

I seriously have no idea what is going on, I tried setting breakpoints in the VC but the error happens after the viewDidLoad method is called (and thus after all my code is executed).

我真的不知道发生了什么,我尝试在 VC 中设置断点,但是在调用 viewDidLoad 方法之后(因此在执行我的所有代码之后)发生错误。

Any Ideas? The fonts for everything in my project is 'Baskerville' and I have tried changing that but it does not affect the crash.

有任何想法吗?我项目中所有内容的字体都是“Baskerville”,我尝试更改它,但它不会影响崩溃。

Oh, and it works fine if I use Xcode 5.

哦,如果我使用 Xcode 5,它工作正常。

Update 9/24: So I am still unable to figure this out. I tried using the fontWithDescriptor method, but it still crashes. The funny thing is, I have plenty of pages that use custom fonts and most of them work fine, but there are two VCs that crash immediately when I go to them... one of them doesn't even have any custom fonts. I really appreciate all of your feedback, but does anyone have any other ideas/fixes? I am using the official release of Xcode and it still doesn't work.

9/24 更新:所以我仍然无法弄清楚。我尝试使用 fontWithDescriptor 方法,但它仍然崩溃。有趣的是,我有很多使用自定义字体的页面,其中大部分都可以正常工作,但是有两个 VC 在我访问它们时立即崩溃……其中一个甚至没有任何自定义字体。我真的很感谢您的所有反馈,但是有人有任何其他想法/修复吗?我正在使用 Xcode 的官方版本,但它仍然无法正常工作。

采纳答案by Jonathan

This problem has been fixed in iOS 8.1.

此问题已在 iOS 8.1 中修复。

Instead of spending time building a custom header/footer view, I just not apply the custom font to devices running iOS 8.0. Most people will probably have updated to iOS 8.1 anyway.

我没有花时间构建自定义页眉/页脚视图,而是不将自定义字体应用于运行 iOS 8.0 的设备。无论如何,大多数人可能已经更新到 iOS 8.1。

I use the following code for this:

我为此使用以下代码:

NSOperatingSystemVersion iOS_8_1 = (NSOperatingSystemVersion){8, 1, 0};

if (![[NSProcessInfo processInfo] respondsToSelector:@selector(isOperatingSystemAtLeastVersion:)]
    || [[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:iOS_8_1]) {
    header.textLabel.font = [UIFont fontWithName:@"Avenir-Medium" size:header.textLabel.font.pointSize];
}

The first statement is true if the device is running an iOS version lower than 8 (since isOperatingSystemAtLeastVersion:was introduced in iOS 8.0). The second statement is true if the device if running iOS 8.1 or later. With both statements we thus only exclude devices running iOS 8.0.

如果设备运行的 iOS 版本低于 8(自isOperatingSystemAtLeastVersion:iOS 8.0 中引入),则第一条语句为真。如果设备运行 iOS 8.1 或更高版本,则第二个语句为真。因此,对于这两个语句,我们只排除运行 iOS 8.0 的设备。

回答by Josh

Okay I finally figured it out.

好吧,我终于想通了。

This occurs when grouped table views have section headers and possibly footers. As a temporary workaround I am just removing the headers/footers from the grouped tableviews, but if you really need the font you can create a custom header view by overriding the "viewForHeaderInSection", and setting the font on your custom view's label.

当分组的表视图具有节标题和可能的页脚时,就会发生这种情况。作为临时解决方法,我只是从分组的 tableview 中删除页眉/页脚,但是如果您确实需要该字体,您可以通过覆盖“viewForHeaderInSection”并在自定义视图的标签上设置字体来创建自定义页眉视图。

Still, this is definitely a bug in iOS 8 and hopefully it will be fixed soon.

尽管如此,这绝对是 iOS 8 中的一个错误,希望它会很快得到修复。

回答by carlodurso

Take a look at: http://openradar.io/17623734

看一看:http: //openradar.io/17623734

Unfortunately at this early stages of xCode 6 I can only offer a workaround for the crash.

不幸的是,在 xCode 6 的早期阶段,我只能提供崩溃的解决方法。

Replace this line:

替换这一行:

[header.textLabel setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:17]];

for this:

为了这:

header.textLabel.font = [UIFont fontWithDescriptor:[UIFontDescriptor fontDescriptorWithFontAttributes:@{@"NSCTFontUIUsageAttribute" : UIFontTextStyleBody,
                                                                                                        @"NSFontNameAttribute" : @"HelveticaNeue-Italic"}] size:17.0];

回答by Tyten

Like everyone here, applying the fix in other answers caused the font setting to be ignored. What I ended up doing was having my own UITableViewHeaderFooterView (Where it seems the cause for the crash is coming from) subclasses that had a label that I added myself. I then used this label instead of the textLabel property inherited from UITableViewHeaderFooterView, set up the auto layout constraints, set the font, and all was right once again (though not what I would consider ideal). I'll add again that this seems to be iOS 8 unique.

像这里的每个人一样,在其他答案中应用修复会导致字体设置被忽略。我最终做的是拥有自己的 UITableViewHeaderFooterView(似乎是崩溃的原因)子类,其中包含我自己添加的标签。然后我使用这个标签而不是从 UITableViewHeaderFooterView 继承的 textLabel 属性,设置自动布局约束,设置字体,一切又一次正确(虽然不是我认为的理想)。我会再次补充一点,这似乎是 iOS 8 的独特之处。

回答by Yury Korolev

Finally found workaround. It is simple, you just need to construct font in other direction :)

终于找到了解决办法。很简单,你只需要在其他方向构造字体:)

// 1. Grab your custom font
CGFloat size = 17.0f;
UIFont *font = [UIFont fontWithName:@"Brandon Grotesque" size:size];

// 2. Get descriptor for your font and create new descriptor with
UIFontDescriptorTextStyleAttribute and UIFontDescriptorSizeAttribute attributes from it.

UIFontDescriptor *des = [[font fontDescriptor] fontDescriptorByAddingAttributes:@{
   UIFontDescriptorTextStyleAttribute: UIFontTextStyleBody, // You can tune this style based on usage
   UIFontDescriptorSizeAttribute: @(size)
}];

// 3. Get your font
UIFont *finalFont = [UIFont fontWithDescriptor:des size:0.0]

回答by Dario Fiumicello

This problem is still present in iOS 8.0.2 but seems to be fixed in 8.1.

这个问题在 iOS 8.0.2 中仍然存在,但似乎在 8.1 中得到修复。

My workaround was to set the custom font only if iOS is greater than 8.0.2:

我的解决方法是仅当 iOS 大于 8.0.2 时才设置自定义字体:

#define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)

...

if (SYSTEM_VERSION_GREATER_THAN(@"8.0.2")) {
    [[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setFont:[UIFont fontWithName:@"Helvetica" size:14.0]];
}

回答by Matthew Phillips

Same problem here: set a custom font for UITableViewHeaderFooterView in my app init, get this crash when showing a grouped UITableView.

同样的问题:在我的应用程序初始化中为 UITableViewHeaderFooterView 设置自定义字体,在显示分组的 UITableView 时出现此崩溃。

What works for me is to delay customisation until the view controller is being created, i.e. do whatever you would have done at app init in the view controller's init. Irritating, but works for me.

对我有用的是将自定义延迟到创建视图控制器之前,即在视图控制器的 init 中执行您在 app init 中所做的任何操作。刺激,但对我有用。

- (id) init
{
  ...

  if (self = [super init])
  {
    // customise table view headers
    UILabel *headerLabel =
      [UILabel appearanceWhenContainedIn: [UITableViewHeaderFooterView class], nil];

    headerLabel.font =
      [UIFont fontWithName: @"AvenirNext-Medium" size: 15];

    ...
  }
}

回答by Byron Rode

This only happens for me when using either a UITableView or UITableViewController where initWithStyle:method call is set to UITableViewStyleGrouped. Changing the style to UITableViewStylePlainresolved the issues.

这只发生在我使用 UITableView 或 UITableViewController 时,其中initWithStyle:方法调用设置为UITableViewStyleGrouped. 更改样式以UITableViewStylePlain解决问题。