Hovanky
Hovanky

Reputation: 303

Cocoa: NSMutableAttributedString - Is there a better way to add bold?

Is there an easier way than doing it like this. "test" is a NSMutableAttributedString

[test addAttributes:myDefault range:NSMakeRange(0, [test length])] ;    
[test applyFontTraits:NSBoldFontMask range: NSMakeRange(0, [test length])]; 

I've been looking all over the documentation and bold seems to be neglected. Thanks in advance.

Upvotes: 2

Views: 1019

Answers (2)

Rob Napier
Rob Napier

Reputation: 299355

I'm trying to imagine how much simpler you had in mind. One line of code to add bold seems pretty simple.

If you are doing complex formatting in code, however, you may want to look at MTStringAttributes, which provides several nice ways to express formatting.

Upvotes: 0

Kris Gellci
Kris Gellci

Reputation: 9687

Attributed strings are useful if you are trying to add bold or underline, etc to sections of text. When you are trying to set a whole string and not just sections of it, you can just set fonts to the properties of whatever you are using to display that text. For example if you want the text in a UILabel to be bold, you would do something like: label.font = [UIFont boldSystemFontOfSize:12.0f]; But if your string is @"This is my bold string" and you are trying to only make the words "my bold" a bold font, you will need to use attributed strings.

Upvotes: 4

Related Questions