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.

2
  • 1
    can you please show the line you define the buttonFont variable? Commented Feb 5, 2015 at 4:19
  • see how to use NSAttributedString stackoverflow.com/questions/27728466/… Commented Feb 5, 2015 at 4:56

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I was using a specific font: let buttonFont = UIFont(name: "AvenirNext-Bold", size: 25) So that fixed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.