• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSTextFieldCell and vertical alignment
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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
References: 
 >NSTextFieldCell and vertical alignment (From: David Rogers <email@hidden>)

  • Prev by Date: Re: Attempting to draw upon a CGLayer upon a NSView
  • Next by Date: Re: Design Decision: Passing Information Between Two Applications
  • Previous by thread: NSTextFieldCell and vertical alignment
  • Next by thread: registering and use of bindings value transformers
  • Index(es):
    • Date
    • Thread