“UIFont"不能转换为“UIFont?" [英] 'UIFont' is not convertible to 'UIFont?'
问题描述
所以我今天晚上将我的 XCode 更新到了 7.3.
So I had updated my XCode to 7.3 today evening.
在我的一个项目中,设置字体的几个标签出现以下错误:
In one of my projects, I get the following error for few labels where I set the font:
'(name: String, size: CGFloat) -> UIFont' is not convertible to '(name: String, size: CGFloat) -> UIFont?'
这是我在导航栏中标题视图的代码:
This is my code for Title View in Navigation Bar:
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 属性字符串代码:
This is my code for an Attributed String for a UILabel:
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
这仅发生在我整个项目的两个文件中.
This happens only in two files in my entire project.
我打开了另一个项目,但我能够在没有任何错误的情况下构建和运行它,即使我也在那里为多个标签设置了字体.
I opened up another project, but I was able to build and run it without any errors, even though I do set the font for multiple labels there as well.
知道为什么会这样吗?
TIA!
推荐答案
在 SO 的其他地方,有人建议你在哪里有这个:
Elsewhere on SO, someone suggest that where you have this:
aTitleView.font = UIFont(name: "Roboto-Regular", size: 15)
...你应该试试写这个:
...you should try writing this:
aTitleView.font = UIFont.init(name: "Roboto-Regular", size: 15)
我不能为此相信(因为我无法重现错误)所以我只是猜测!但知道它是否真的有效会很有趣.
I can take no credit for this (because I can't reproduce the bug) so I'm just guessing! But it would be very interesting to know if it actually works.
这篇关于“UIFont"不能转换为“UIFont?"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!