Re: NSTextFieldCell and vertical alignment
Re: NSTextFieldCell and vertical alignment
- Subject: Re: NSTextFieldCell and vertical alignment
- From: Shaun Wexler <email@hidden>
- Date: Fri, 10 Mar 2006 23:47:36 -0800
On Mar 10, 2006, at 9:45 PM, David Rogers wrote:
I've done some research and I think I know the solution but I just
wanted to get a definitive answer.
My NSTextFieldCells are a bit tall(on purpose, in case of multi-
line entries), I'd like the single line entries to be centered
vertically within the cell. Is subclassing NSTextFieldCell and
overriding - drawInteriorWithFram:inView the best/appropriate/only
way to do this?
Here is my squishy-text subclass you can use as an example, but it
does use a few guerilla tactics.
// SKWTextField.h
// SKWApp.framework
//
// Created by Shaun Wexler on Thu May 31 2001.
// Copyright (c) 2001 SKW Development. All rights reserved.
@interface SKWTextField : NSTextField
@end
@interface SKWTextFieldCell : NSTextFieldCell
@end
@implementation SKWTextField
+ (Class)cellClass
{
return [SKWTextFieldCell class];
}
- (id)initWithCoder:(NSCoder *)coder
{
if ((self = [super initWithCoder:coder]))
{
Class desiredClass = [SKWTextFieldCell class];
NSCell *cell = [self cell];
if (![cell isMemberOfClass:desiredClass]) {
*(Class *)cell = desiredClass; // do not pose
}
}
return self;
}
@end
@interface NSTextFieldCell (AppKitPrivate)
- (id)_textAttributes;
@end
@implementation SKWTextFieldCell
- (void)drawInteriorWithFrame:(NSRect)frame inView:(NSView *)view
{
NSString *string;
if ((string = [self stringValue]) && [string length])
{
NSSize textSize = [string sizeWithAttributes:[self
_textAttributes]]; // private
textSize.width = ceilf(textSize.width);
if (textSize.width < frame.size.width) {
[super drawInteriorWithFrame:frame inView:view];
}
else {
float textWidth = MIN(textSize.width + 3.0f,
frame.size.width * 1.8f);
CGContextRef cgctx = [[NSGraphicsContext currentContext]
graphicsPort];
CGContextSaveGState(cgctx);
CGContextTranslateCTM(cgctx, frame.origin.x + 1.0f,
frame.origin.y - 1.0f);
CGContextScaleCTM(cgctx, (frame.size.width - 3.0f) /
textWidth, (textSize.height + 1.0f) / textSize.height);
[super drawInteriorWithFrame:NSMakeRect(0.0f, 0.0f,
textWidth + 3.0f, frame.size.height) inView:view];
CGContextRestoreGState(cgctx);
}
}
}
@end
--
Shaun Wexler
MacFOH
http://www.macfoh.com
"The true sign of intelligence is not knowledge but imagination." -
Albert Einstein
_______________________________________________
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