Example of NSAttributedString with two different font sizes?

IosNsattributedstring

Ios Problem Overview


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.

Ios Solutions


Solution 1 - Ios

You would do something like this…

NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"Presenting the great... Hulk Hogan!"];
[hogan addAttribute:NSFontAttributeName              value:[UIFont systemFontOfSize:20.0]
              range:NSMakeRange(24, 11)];

This will set the last two words in 20-point text; the rest of the string will use the default value (which I believe is 12 points). The thing that might be confusing about setting the text size is that you have to set the typeface and the size at the same time—each UIFont object encapsulates both of those properties.

Solution 2 - Ios

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) ]))

Solution 3 - Ios

[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)]));

Solution 4 - Ios

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLe Mot JuicedView Question on Stackoverflow
Solution 1 - IosbdeshamView Answer on Stackoverflow
Solution 2 - IosiljnView Answer on Stackoverflow
Solution 3 - IosKunal ShahView Answer on Stackoverflow
Solution 4 - IosJonahGabrielView Answer on Stackoverflow