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
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.
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.
A mutable string with associated attributes (such as visual style, hyperlinks, or accessibility data) for portions of its text.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With