Font metrics
Font metrics
- Subject: Font metrics
- From: "Frode" <email@hidden>
- Date: Thu, 8 Dec 2005 14:56:09 +0100
- Importance: Normal
Hi!
I'm trying to mesasure text with NSLayoutManager, i.e. something similar
to Quickdraw's TextWidth() routine. But why does [NSLayoutManager
usedRectForTextContainer:] returns a rectangle slightly larger than the
actual text rectangle? It looks like it appends some advancement units
after the end, similar to leadings in font height.
I've checked the documentation about Cocoa text architecture but can't
find how to control this and what it depends on. What is the reason for
this, can it be adjusted and how do I fix it?
Thanks for any hints!
Regards
Frode
---------------------------------------------------------------------
Below is a sample of my problem implementing a custom view. It paints
the following:
NSLayoutManager
NS Layout Manager
The last line painted was suppose to be "NSLayouManager" and not "NS
Layout Manager".
I created a new project and added a custom view "TextView" in IB:
#import "TextView.h"
@implementation TextView
- (id)initWithFrame:(NSRect)frameRect
{
if ((self = [super initWithFrame:frameRect]) != nil) {
// Add initialization code here
}
return self;
}
- (BOOL)isFlipped
{
return (YES);
}
- (NSSize)drawText:(NSString *)theText withStyle:(NSDictionary
*)attributes atPoint:(NSPoint)atPoint
{
NSRange glyphRange;
NSRect textRect;
NSTextStorage *textStorage =[[NSTextStorage alloc]
initWithString:theText attributes:attributes];
NSLayoutManager *layoutManager = [NSLayoutManager new];
NSTextContainer *textContainer = [NSTextContainer new];
[layoutManager addTextContainer:textContainer];
[textContainer release];
[textStorage addLayoutManager:layoutManager];
[layoutManager release];
[textContainer setContainerSize:NSMakeSize(FLT_MAX, FLT_MAX)];
glyphRange = [layoutManager glyphRangeForTextContainer:textContainer];
textRect = [layoutManager usedRectForTextContainer:textContainer];
[self lockFocus];
[layoutManager drawGlyphsForGlyphRange:glyphRange atPoint:atPoint];
[self unlockFocus];
[textStorage release];
return (textRect.size);
}
- (void)drawRect:(NSRect)rect
{
float fontSize = [self frame].size.width * 0.08;
NSFont *font = [NSFont fontWithName: @"Monaco" size:fontSize];
NSDictionary *style = [NSDictionary dictionaryWithObjectsAndKeys:font,
NSFontAttributeName, nil];
float height = [self drawText:@"NSLayoutManager" withStyle:style
atPoint:NSMakePoint(0,0)].height;
float x = [self drawText:@"NS" withStyle:style atPoint:NSMakePoint(x,
height)].width;
x += [self drawText:@"Layout" withStyle:style atPoint:NSMakePoint(x,
height)].width;
x += [self drawText:@"Manager" withStyle:style atPoint:NSMakePoint(x,
height)].width;
}
@end
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden