在Swift中为带有属性的文本添加属性



我尝试为文本标签添加属性:

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.Center
let para = NSMutableAttributedString()
let font:UIFont? = UIFont(name: "DIN Alternate", size: 18.0)
let font2:UIFont? = UIFont(name: "Arial", size: 12.0)
let labelColor = UIColor.whiteColor()
let text="some text"
//A, error appears here
let myAttributes = [NSParagraphStyleAttributeName:paragraphStyle,
                    NSFontAttributeName:font,
                    NSForegroundColorAttributeName:labelColor]
let attrString = NSAttributedString(
    string: csvObject.provideDetailsAsString(poi)!,
    attributes: myAttributes)
para.appendAttributedString(attrString)     
detailsText.attributedText = para

但是Swift编译器在//A行出错并说:

'_' is not convertible to 'String'

我使用XCode 6.4和Swift 1.2

找到了,添加一个!after font: NSFontAttributeName:font!

字体可选

最新更新