简体   繁体   中英

Adding attributes to attributed text in Swift

I tried to add attributes to a text label:

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

But Swift compiler errors in line //A and says:

'_' is not convertible to 'String'

I am using XCode 6.4 and Swift 1.2

Found it, add a ! after font : NSFontAttributeName:font!

font is optional

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM