首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“UIFont”不能转换为“UIFont?”

“UIFont”不能转换为“UIFont?”
EN

Stack Overflow用户
提问于 2016-03-22 19:18:00
回答 2查看 8.5K关注 0票数 41

所以我今天晚上把我的XCode更新到7.3了。

在我的一个项目中,对于设置字体的少数标签,我得到以下错误:

代码语言:javascript
复制
'(name: String, size: CGFloat) -> UIFont' is not convertible to '(name: String, size: CGFloat) -> UIFont?'

编辑:--这是我在导航栏中的标题视图代码:

代码语言:javascript
复制
let aTitleFrame: CGRect = CGRectMake(0, aHeaderTitleSubtitleView.frame.midY / 2, 200, 24)
let aTitleView: UILabel = UILabel(frame: aTitleFrame)
aTitleView.backgroundColor = UIColor.clearColor()
aTitleView.font = UIFont(name: "Roboto-Regular", size: 15) // ERROR POPS UP HERE
aTitleView.textAlignment = NSTextAlignment.Center
aTitleView.textColor = UIColor.whiteColor()

这是UILabel属性化字符串的代码:

代码语言:javascript
复制
let aAttributedFundLabel: NSMutableAttributedString = NSMutableAttributedString(string: "Raising\n$ \(fund)")
aAttributedFundLabel.addAttribute(NSForegroundColorAttributeName, value: UIColor.darkGrayColor(), range: NSRange(location: 0, length: 7))
aAttributedFundLabel.addAttribute(NSFontAttributeName, value: UIFont(name: "Roboto-Regular", size: 15)!, range: NSRange(location: 0, length: 7)) // ERROR POPS UP HERE 
aAttributedFundLabel.addAttribute(NSForegroundColorAttributeName, value: UIColor.blackColor(), range: NSRange(location: 8, length: fund.characters.count + 2))
aAttributedFundLabel.addAttribute(NSFontAttributeName, value: UIFont(name: "Roboto-Regular", size: 16)!, range: NSRange(location: 8, length: fund.characters.count + 2)) // ERROR POPS UP HERE
startupFund.attributedText = aAttributedFundLabel

这种情况只发生在我整个项目中的两个文件中。

我打开了另一个项目,但是我能够在没有任何错误的情况下构建和运行它,尽管我也为多个标签设置了字体。

知道为什么会这样吗?

蒂娅!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-03-23 04:01:56

在其他地方,有人建议你在哪里有这样的东西:

代码语言:javascript
复制
aTitleView.font = UIFont(name: "Roboto-Regular", size: 15)

...you应该尝试这样写:

代码语言:javascript
复制
aTitleView.font = UIFont.init(name: "Roboto-Regular", size: 15)

我对此没有任何功劳(因为,我不能复制bug),所以我只是猜测!但是如果知道它是否真的有效的话,那将是非常有趣的。

票数 116
EN

Stack Overflow用户

发布于 2016-03-31 15:58:42

我也有这个问题。为我修正的是关闭整个模块优化。

Bulid设置>快速编译器-代码生成>优化级别

我把它设置为最快的(整个模块优化)。当我把它设置为零时,我就不再有这个问题了。对于上下文,这是一个目标C和Swift的混合代码库。

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36163273

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档