Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSAttributedString default color/font etc

I'm using drawGlyphsForGlyphRange to render an NSAttributedString (in NSView drawRect)

Is there any way to default the color/font etc of an NSAttributedString? Clearly I could set those values specifically for a given range - but that will then override any specific settings in the string. Got the feeling I have a fundamental misunderstanding of how NSAttributedStrings should be used!!

Thanks

like image 642
Scotty Avatar asked Oct 23 '12 11:10

Scotty


People also ask

How do I change the color of NSMutableAttributedString?

The main here is to use a NSMutableAttributedString and the selector addAttribute:value:range with the attribute NSForegroundColorAttributeName to change a color of a string range: NSMutableAttributedString *attrsString = [[NSMutableAttributedString alloc] initWithAttributedString:label.

What is NSAttributedString in Swift?

An NSAttributedString object manages character strings and associated sets of attributes (for example, font and kerning) that apply to individual characters or ranges of characters in the string. An association of characters and their attributes is called an attributed string.

What is NSMutableAttributedString?

A mutable string with associated attributes (such as visual style, hyperlinks, or accessibility data) for portions of its text.


1 Answers

The default font for NSAttributedString objects is Helvetica 12-point on iOS and Lucida Grande (or whatever the default system font is set to) on OS X. Use initWithString:attributes: to create an NSAttributedString with some pre-set attributes that would initially apply to the whole string. Any attributes you will set to different ranges of your string afterwards will supplement/replace the attributes you define when you create your instance of NSAttributedString.

like image 171
kervich Avatar answered Oct 03 '22 06:10

kervich