簡體   English   中英

'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!

在其他地方,有人建議你有這個:

aTitleView.font = UIFont(name: "Roboto-Regular", size: 15)

...你應該嘗試寫這個:

aTitleView.font = UIFont.init(name: "Roboto-Regular", size: 15)

我不能相信這一點(因為我無法重現這個錯誤 )所以我只是在猜測! 但要知道它是否真的有用會非常有趣。

我也有這個問題。 為我修復的是關閉整個模塊優化。

Bulid設置> Swift編譯器 - 代碼生成>優化級別

我把它設置為Fastest(整個模塊優化)。 當我將其設置為None時,我不再有這個問題了。 對於上下文,這是一個包含Objective-C和Swift的混合代碼庫。

我有同樣的問題,這樣做可以讓我再次編譯:

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM