problems in measuring text rendering size
problems in measuring text rendering size
- Subject: problems in measuring text rendering size
- From: Brian Burton <email@hidden>
- Date: Mon, 23 Feb 2004 20:59:01 -0600
I am writing a terminal emulator that has a fixed size of text of 80
columns and 24 rows, and I was using a subclass of NSTextView to
display the text.
In order to set the size of the view and the window surrounding it, I
was measuring the text with the following bit of code
NSFont* the24Font = [NSFont fontWithName:"Courier" size:11];
float width = [the24Font advancementForGlyph:(NSGlyph)' '].width *
80;
// create an attributed string with 80 characters per line and 24
lines with the character of ' '
attString = (the above attributed string)
NSTextStorage *textStorage = [[[NSTextStorage alloc]
initWithAttributedString: attString] autorelease];
NSTextContainer *textContainer = [[[NSTextContainer alloc]
initWithContainerSize:NSMakeSize(width, 1e7)] autorelease];
NSLayoutManager *layoutManager = [[[NSLayoutManager alloc] init]
autorelease];
[textContainer setLineFragmentPadding:0.0]; // padding usually is
not appropriate for string drawing
[layoutManager addTextContainer:textContainer];
[textStorage addLayoutManager:layoutManager];
(void)[layoutManager glyphRangeForTextContainer:textContainer]; //
force layout
retSize = [layoutManager usedRectForTextContainer:textContainer].size;
If I create the attributed string with 80 spaces and a line feed
repeated 24 times, I get one size, but if I use a non-space character,
such as a '1', then I get a ridiculous height for retSize. I've dumped
the RTF string from the attributed string, but it looks fine. Why
would I get one size for spaces and one size for '1' for a monospace
font like Courier? I would expect a width of 528.085938 and a height
of 312 (the defaultLineHeightForFont of the font * 24) but I end up
with a width of 525 and height of 624 if I use the '1' character. If I
use the space character, I end up with the size that I expect.
Brian
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.