1

I'm getting issues with NSAtrributedString no matter which path I take and can't seem to figure out what the issue is.

With the code below I get: Cannot invoke 'init' with an argument list of type '(string: StringLiteralConvertible, attributes: $T6)'

let cancelButtonTitle = NSAttributedString(string: "CANCEL", attributes: [NSFontAttributeName: buttonFont, NSForegroundColorAttributeName: UIColor.whiteColor()])

Any ideas where the issue lies? I've tried in xcode 6.1 and 6.2.

Frolie
  • 13
  • 3
  • 1
    can you please show the line you define the buttonFont variable? – rakeshbs Feb 05 '15 at 04:19
  • see how to use NSAttributedString http://stackoverflow.com/questions/27728466/use-multiple-font-colors-in-a-single-label-swift/27728516#27728516 – Kirit Modi Feb 05 '15 at 04:56

1 Answers1

2

NSFontAttributeName should be set to a UIFont object.

let buttonFont = UIFont.systemFontOfSize(10)

let cancelButtonTitle = NSAttributedString(string: "CANCEL",
    attributes: [NSFontAttributeName: buttonFont,
        NSForegroundColorAttributeName: UIColor.whiteColor()])

or if you are using a specific font.

if let buttonFont = UIFont(name: "Your Font", size: 10) {

    let cancelButtonTitle = NSAttributedString(string: "CANCEL",
    attributes: [NSFontAttributeName: buttonFont,
        NSForegroundColorAttributeName: UIColor.whiteColor()])
}

because UIFont(name:,size:) constructor returns an optional, you need to unwrap it before using it in NSAttributedString

rakeshbs
  • 24,392
  • 7
  • 73
  • 63
  • I was using a specific font: `let buttonFont = UIFont(name: "AvenirNext-Bold", size: 25)` So that fixed it. – Frolie Feb 05 '15 at 15:24