problem with CGAffineTransform?
problem with CGAffineTransform?
- Subject: problem with CGAffineTransform?
- From: Shaun Wexler <email@hidden>
- Date: Mon, 31 May 2004 20:47:20 -0700
I needed a quick and dirty NSTextFieldCell subclass which compresses
its text horizontally to fit its frame if needed. My popup button
subclass which I've used for years also does this and works quite well;
it scales its bounds rather than apply a separate transform... but for
these cells (which are in a NSTableView, from IB), I think I'll need to
actually change the CTM. This works as per the code below, except it's
somehow scaling the text vertically as well, by approx 1px, and "she
just don't look s'good". Any ideas? Here is the subclass:
@interface SKWTextFieldCell : NSTextFieldCell {}
@end
@interface NSTextFieldCell (AppKitPrivate)
- (id)_textAttributes;
@end
@implementation SKWTextFieldCell
#define padding 3.0f
- (void)drawInteriorWithFrame:(NSRect)frame inView:(NSView *)controlView
{
NSString *title = [self stringValue];
if (title && [title length])
{
NSSize textSize = [title sizeWithAttributes:[self _textAttributes]];
if (textSize.width > frame.size.width)
{
float textWidth = MIN(textSize.width + padding, frame.size.width *
1.5f);
CGContextRef cgctx = [[NSGraphicsContext currentContext]
graphicsPort];
CGContextSaveGState(cgctx);
CGContextTranslateCTM(cgctx, frame.origin.x, frame.origin.y);
CGContextScaleCTM(cgctx, frame.size.width / textWidth, 1.0f);
NSRect newFrame = NSMakeRect(0.0f, 0.0f, textWidth + padding,
frame.size.height);
[super drawInteriorWithFrame:newFrame inView:controlView];
CGContextRestoreGState(cgctx);
}
else {
[super drawInteriorWithFrame:frame inView:controlView];
}
}
}
@end
Nothing above should be affecting the Y-axis scale, correct? I also
examined the text matrix before and after each call above, and it is
[only] being changed by super for some reason. Setting it to identity
or anything else prior to calling super doesn't work, either. And if I
manually draw the title string using drawWithAttributes: instead of
calling super, it still glitches the same way... what's going on?!!
--
Shaun Wexler
MacFOH
http://www.macfoh.com
_______________________________________________
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.