-[UILabel setFont:] 中断言失败
我在 iOS 3.1.2 中设置 UILabel 字体时遇到此异常,但在 iOS 4 中运行良好,
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: font != nil'
代码行是,
Outbound_Label.font = [UIFont fontWithName:@"DS-Digital" size:24];
其中 DS-Digital 是我的自定义字体。
I am getting this exception on setting a font of UILabel in iOS 3.1.2 but its running fine in iOS 4,
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: font != nil'
The code line is,
Outbound_Label.font = [UIFont fontWithName:@"DS-Digital" size:24];
where DS-Digital is my custom font.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
[UIFont fontWithName:@"DS-Digital" size:24]
返回 nil。因为“DS-Digital”没有可用的字体(Apple 支持的字体列表)。所以它导致了崩溃。[UIFont fontWithName:@"DS-Digital" size:24]
returns nil. Because no font available for "DS-Digital" (Apple supported font list). so it giving the crash.我的猜测是您的自定义字体没有被实例化。
每当我遇到这样的问题,其中发生两件事:获取字体和设置字体时,我会将它们分开以查找问题:
然后我可以使用调试器或 NSLog() 轻松确定错误在哪里。
事实上,这是一种非常清晰的编写代码的方式。我从不担心优化,我只担心清晰度和易于维护。稍后,如果我分析出性能问题,找到真正的罪魁祸首并解决它们。代码越清晰就越容易。
在这种情况下,编译器无疑会优化掉字体变量,因此没有任何害处。
请参阅:所以答案
想要使用自定义字体的应用程序现在可以将这些字体包含在其应用程序包中,并通过在 Info.plist 文件中包含 UIAppFonts 键来向系统注册这些字体。该键的值是一个字符串数组,用于标识应用程序包中的字体文件。当系统看到该密钥时,它会加载指定的字体并使它们可供应用程序使用。
My guess is that your custom font is not being found instantiated.
Whenever I have a problem like this where there are two things happening: obtaining a font and setting a font, I separate them to find the problem:
Then I can determine where the error is easily with either the debugger or
NSLog()
.In fact this is a very clear way to write the code in the first place. I never worry about optimization, I only worry about clarity and ease of maintenance. Later if there are performance issues I profile, find the real culprits and resolve them. The clearer the code the easier that is.
In a case like this the compiler will undoubtedly optimize away the font variable so there is no harm.
See this:SO answer
Applications that want to use custom fonts can now include those fonts in their application bundle and register those fonts with the system by including the UIAppFonts key in their Info.plist file. The value of this key is an array of strings identifying the font files in the application’s bundle. When the system sees the key, it loads the specified fonts and makes them available to the application.
请尝试一下这个,它对我来说效果很好。
https://github.com/zynga/FontLabel
我已经从 http://www.fontriver.com/font/ds-digital/
并使用此字体运行上述应用程序,它适用于 IOS 3.1.2。
干杯。
Please try this, it's works fine for me.
https://github.com/zynga/FontLabel
i have download DS-Digital font from http://www.fontriver.com/font/ds-digital/
and run the above app with this font and it's works with IOS 3.1.2.
Cheers.