'UIFont' 不可转换为 'UIFont?'
'UIFont' is not convertible to 'UIFont?'
所以我今天晚上将 XCode 更新为 7.3。
在我的一个项目中,我设置字体的几个标签出现以下错误:
'(name: String, size: CGFloat) -> UIFont' is not convertible to '(name: String, size: CGFloat) -> UIFont?'
编辑: 这是我的导航栏标题视图代码:
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 属性字符串代码:
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
在我的整个项目中,这只发生在两个文件中。
我打开了另一个项目,但我能够构建并 运行 它没有任何错误,即使我在那里也为多个标签设置了字体。
知道为什么会这样吗?
TIA!
我遇到了同样的问题,这样做可以让我再次编译:
let descriptor = UIFontDescriptor(name: "OpenSans-Semibold", size: 10.0)
label.font = UIFont(descriptor: descriptor, size: 10.0)
所以,使用 UIFontDescriptor ...
我也这样做:
if let font = UIFont(name: "OpenSans-Semibold", size: 10) {
label.font = font
}
在 SO 的其他地方,有人建议你在哪里:
aTitleView.font = UIFont(name: "Roboto-Regular", size: 15)
...你应该尝试这样写:
aTitleView.font = UIFont.init(name: "Roboto-Regular", size: 15)
我对此不以为然(因为我无法重现错误)所以我只是在猜测!但如果知道它是否真的有效,那将是非常有趣的。
我也遇到了这个问题。为我修复的是关闭整个模块优化。
构建设置 > Swift 编译器 - 代码生成 > 优化级别
我已将其设置为最快(全模块优化)。当我将它设置为 None 时,我不再有这个问题了。对于上下文,这是一个包含 Objective-C 和 Swift.
的混合代码库
所以我今天晚上将 XCode 更新为 7.3。
在我的一个项目中,我设置字体的几个标签出现以下错误:
'(name: String, size: CGFloat) -> UIFont' is not convertible to '(name: String, size: CGFloat) -> UIFont?'
编辑: 这是我的导航栏标题视图代码:
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 属性字符串代码:
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
在我的整个项目中,这只发生在两个文件中。
我打开了另一个项目,但我能够构建并 运行 它没有任何错误,即使我在那里也为多个标签设置了字体。
知道为什么会这样吗?
TIA!
我遇到了同样的问题,这样做可以让我再次编译:
let descriptor = UIFontDescriptor(name: "OpenSans-Semibold", size: 10.0)
label.font = UIFont(descriptor: descriptor, size: 10.0)
所以,使用 UIFontDescriptor ...
我也这样做:
if let font = UIFont(name: "OpenSans-Semibold", size: 10) {
label.font = font
}
在 SO 的其他地方,有人建议你在哪里:
aTitleView.font = UIFont(name: "Roboto-Regular", size: 15)
...你应该尝试这样写:
aTitleView.font = UIFont.init(name: "Roboto-Regular", size: 15)
我对此不以为然(因为我无法重现错误)所以我只是在猜测!但如果知道它是否真的有效,那将是非常有趣的。
我也遇到了这个问题。为我修复的是关闭整个模块优化。
构建设置 > Swift 编译器 - 代码生成 > 优化级别
我已将其设置为最快(全模块优化)。当我将它设置为 None 时,我不再有这个问题了。对于上下文,这是一个包含 Objective-C 和 Swift.
的混合代码库