An NSAttributedString example with two different font sizes?

NSAttributedString is simply impenetrable to me.

I want to set UILabel for text of different sizes, and I understand that NSAttributedString is the way to go, but I can't document it anywhere.

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

For example, suppose the text I wanted was:

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

Can someone show me how to do this? Or even, a link that simple and simple, where could I find out for myself? I swear I tried to figure this out through the documentation and even through other examples, and I just don't get it.

+77
ios nsattributedstring
Aug 21 '13 at 19:04 on
source share
5 answers

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 establish the last two words in a 20 point text; the rest of the line will use the default value (which, in my opinion, is 12 points). The thing that can confuse text size adjustment is that you must set the font and size at the same time - each UIFont object encapsulates both of these properties.

+158
Aug 21 '13 at 19:18
source share

Swift 3 Solution

Alternatively, 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) ])) 
+18
Jan 14 '17 at 9:15
source share

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)])); 
+10
Nov 23 '17 at 9:31 on
source share

If you want to do this in a simple way, there is a git repo called OHAttributedLabel that I use that provides a category in NSAttributedString. It allows you to 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 third-party library, check out this link for a decent tutorial on how to get started with a string attribute.

0
Aug 21 '13 at 19:17
source share

Swift 4.2 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)])) 
0
Feb 01 '19 at 10:05
source share



All Articles