Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

swift - 'UIFont' is not convertible to 'UIFont?'

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

EDIT: 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()

This is my code for an Attributed String for a UILabel:

let aAttributedFundLabel: NSMutableAttributedString = NSMutableAttributedString(string: "Raising
$ (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.

Any idea why this is happening?

TIA!

question from:https://stackoverflow.com/questions/36163273/uifont-is-not-convertible-to-uifont

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...