84

NSAttributedString is just really impenetrable to me.

I want to set a UILabel to have text of different sizes, and I gather NSAttributedString is the way to go, but I can't get anywhere with the documentation on this.

I would love it if someone could help me with a concrete example.

For instance, let's say the text I wanted was:

(in small letters:) "Presenting The Great..."
(in huge letters:) "HULK HOGAN!"

Can somebody show me how to do that? Or even, a reference that's plain and simple where I could learn for myself? I swear I've tried to understand this through the documentation, and even through other examples on Stack Overflow, and I'm just not getting it.

4 Answers 4

166

Suppose your input text is “Some of this text is larger than the rest,” and you want to change the size of the word “larger.” Here’s how you would do it:

let text = NSMutableAttributedString(
    "Some of this text is larger than the rest"
)

let range = text.mutableString.range(of: "larger")

if range.location != NSNotFound {
    text.addAttribute(.font,
                      value: UIFont.systemFont(ofSize: 20),
                      range: range)
}

Or in Objective-C,

NSMutableAttributedString *text = [[NSMutableAttributedString alloc]
    initWithString:@"Some of this text is larger than the rest"];

NSRange range = [text rangeOfString:@"larger"];

if (range.location != NSNotFound) {
    [text addAttribute:NSFontAttributeName
                 value:[UIFont systemFontOfSize:20.0]
                 range:range];
}

The thing that might be confusing about setting the text size specifically is that you have to set the typeface and the size at the same time—each UIFont object encapsulates both of those properties.

See the documentation of NSAttributedString.Key for the full list of NSAttributedString attributes.

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

2 Comments

Check out this answer for SWIFT ranges: stackoverflow.com/a/27041376/1736679
better to calculate the range of string instead of manually entering. Use: NSRange hulkRange = [@"Presenting the great... Hulk Hogan!" rangeOfString: @"Hulk Hogan!"];
24

Swift 3 Solution

Also, you can use the append function instead of specifying indices in ObjC or Swift:

let attrString = NSMutableAttributedString(string: "Presenting The Great...",
                                           attributes: [ NSFontAttributeName: UIFont.systemFont(ofSize: 20) ])

attrString.append(NSMutableAttributedString(string: "HULK HOGAN!",
                                            attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 40) ]))

1 Comment

Smarter than the accepted.
24

[UPDATE] Swift 5 Solution:

let attrString = NSMutableAttributedString(string: "Presenting The Great...",
                                           attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18)]);

attrString.append(NSMutableAttributedString(string: "HULK HOGAN!",
                                            attributes: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 36)]));

Swift 4 Solution:

let attrString = NSMutableAttributedString(string: "Presenting The Great...",
                                       attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 18)]);

attrString.append(NSMutableAttributedString(string: "HULK HOGAN!",
                                        attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 36)]));

1 Comment

Works like magic - thank you!
-1

If you want to do it the easy way, there is a git repo called OHAttributedLabel that I use that provides a category on NSAttributedString. It lets you do things like:

NSMutableAttributedString *mystring = [[NSMutableAttributedString alloc] initWithString:@"My String"];
[mystring setTextColor:[UIColor colorWithRGB:78 green:111 blue:32 alpha:1]];
mystring.font = [UIFont systemFontOfSize:14];

If you don't want to use a 3rd party lib, check out this link for a decent tutorial on how to get started with attributed strings.

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.