3

I'm trying to set some attributes of a string in code, but can't get NSAttributedString to work. This is the function, that's supposed to change the string:

func getAttributedString(string: String) -> NSAttributedString
{
    var attrString = NSMutableAttributedString(string: string)
    var attrs = [NSFontAttributeName : UIFont.boldSystemFontOfSize(18.0)]

    attrString.setAttributes(attrs, range: NSMakeRange(0, attrString.length))

    return attrString
}

And this is how I use it:

if (self.product.packageDimensions != nil) {
        self.descriptionLabel.text = 
                   self.descriptionLabel.text + self.getAttributedString("Package dimensions:").string + 
                   "\n\(self.product.packageDimensions) \n"
    }

But the font stays the same. What am I doing wrong ?

4
  • I think the concatenation of the attributed string with the non attributed string is causing the loss of formatting. Commented Aug 8, 2014 at 9:01
  • Unfortunately, even without the concatenation, the string doesn't get formatted Commented Aug 8, 2014 at 9:03
  • I don't use Swift, but shouldn't it be self.descriptionLabel.attributedText = instead? Commented Aug 8, 2014 at 9:15
  • It doesn't work with self.descriptionLabel.attributedText either. Commented Aug 8, 2014 at 9:19

2 Answers 2

3

You make 2 errors in your code.

  1. setAttributes needs a Dictionary, not an Array
  2. when you use the string attribute, you will only get a String, all attributes are lost.

To add or change attributes to a attributedString it has to be mutable. You only get a NSMutableString from the attributedText attribute. If you want to change it create a mutable version from it and change it. Then you may set the attributedText to the new mutable version.


If you can give the attributed string as an argument, I will give you an example that works:

func setFontFor(attrString: NSAttributedString) -> NSMutableAttributedString {
    var mutableAttrString: NSMutableAttributedString = NSMutableAttributedString(attributedString: attrString)
    let headerStart: Int = 0
    let headerEnd: Int = 13
    mutableAttrString.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFontOfSize(18.0), range: NSMakeRange(headerStart, headerEnd))

    return mutableAttrString
}

Usage:

myLabel.attributedText = setFontFor(myLabel.attributedText)

As you can see I used the attributedText property of the UILabel class, it also works for UITextView and others. If you have another label, you can create a new NSAttributedString with the initializer NSAttributedString(normalString) as you already used in the question code.

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

8 Comments

I want to format just a part of the text, so with this code I'd have to set the header text (which I want formatted) first, use the function and then append the rest, or am I getting it wrong ?
You are getting it wrong with NSAttributedString. You define the part you want to format with the range parameter. When you set the NSRange to the range of the text for your header, only that text will have the new attribute.
Changed it to more reflect your question in the comment
Well, this is the desired outcome: I want to style the header only, so I pass only it as the function parameter. But anyway, even with your code, it doesn't work.
Have you tried something easier to make sure it is not the fault of the function, like: label. attributedText = label.attributedText.addAttribute(NSBackgroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, label.attributedText.length))?
|
2
if (self.product.packageDimensions != nil) {
        self.descriptionLabel.attributedText = 
                   self.descriptionLabel.attributedText + self.getAttributedString("Package dimensions:").string + 
                   "\n\(self.product.packageDimensions) \n"
    }

You should use the attributedText method

Comments

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.