Re: Problem with fonts/traits in attributed strings
Re: Problem with fonts/traits in attributed strings
- Subject: Re: Problem with fonts/traits in attributed strings
- From: Greg Titus <email@hidden>
- Date: Tue, 25 Sep 2001 16:44:10 -0700
On Tuesday, September 25, 2001, at 03:53 PM, Brian Webster wrote:
I am attempting to construct an NSAttributedString from another data
structure which specifies fonts and styles for various character runs
within the string. Initially, my code looked something like this:
[...shortened...]
//get font name, size, range, etc. from data
[attString applyFontTraits:fontTraitMask range:range];
font = [NSFont fontNamed:fontName size:fontSize;
[attString addAttribute:NSFontAttributeName value:font range:range];
When I ran this, the font would be set correctly, but none of the font
traits (bold, italic, etc.) would show up. If I take out the line that
sets the NSFontAttributeName, then the traits show up correctly, but
the font is set to the default.
This behavior is what I would expect. Setting the font attribute
directly is sort of a lower level than applying font traits because an
NSFont object is a particular combination of family and traits, not the
family itself. For instance, "Helvetica-Bold" is a different font than
"Helvetica", so if your fontName in the above code is "Helvetica", it
will undo any traits in the range.
So something you might try is simply reversing the order here. Set the
font, then apply the traits.
Next, I tried removing the applyFontTraits:range: message and changing
the font assignment to:
font = [[NSFontManager sharedFontManager] fontWithFamily:fontName
traits:fontTraitMask weight:5 size:fontSize];
This, on the other hand, I would have expected to work. Are you certain
that your fontTraitMask is correct? If so, are you certain that you have
a font on your system which matches that family name and traits (you
can't italicize Arial Black, for instance, since there's no italic or
oblique version of the font - at least not built in)?
Hope this helps,
-Greg