Problem with fonts/traits in attributed strings
Problem with fonts/traits in attributed strings
- Subject: Problem with fonts/traits in attributed strings
- From: Brian Webster <email@hidden>
- Date: Tue, 25 Sep 2001 17:53:13 -0500
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:
NSMutableAttributedString *attString;
for(i = 0; i < numRuns; i++)
{
NSRange range;
NSFontTraitMask fontTraitMask;
NSFont *font;
NSString *fontName;
int fontSize;
//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. 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 resulted in the same thing, with the font set correctly,
but all the text being plain. So it seems that the
NSFontAttributeName attribute is overrwriting the traits
somehow. I think it should be possible to do this, but I can't
see what it is I'm doing wrong.
BTW, I checked to make sure that the text view I'm displaying it
in is set to rich text, allows multiple fonts, etc., so I'm
pretty sure it's not a display problem. Also, setting color
attributes works fine in conjunction with the above.
--
Brian Webster
email@hidden
http://homepage.mac.com/bwebster