I would like a NSTextField whose text is vertically centered. All examples of doing so, which I have been able to find, are to subclass NSTextField. All examples of subclassing which I have found use Xcode. (I refer to subclassing anything, not only NSTextField.) However, I do not use Xcode. So…is it possible to subclass without using Xcode and, if so, how? I located a subclass solution by Daniel Jalkut of Red Sweater Software, which I have included in hope anyone with a solution can use it in their explanation.
Thank you in advance for any assistance.
The .m file:
//
//  RSVerticallyCenteredTextField.m
//  RSCommon
//
//  Created by Daniel Jalkut on 6/17/06.
//  Copyright 2006 Red Sweater Software. All rights reserved.
//
#import "RSVerticallyCenteredTextFieldCell.h"
@implementation RSVerticallyCenteredTextFieldCell
- (NSRect)drawingRectForBounds:(NSRect)theRect
{
    // Get the parent's idea of where we should draw
    NSRect newRect = [super drawingRectForBounds:theRect];
    // When the text field is being 
    // edited or selected, we have to turn off the magic because it screws up 
    // the configuration of the field editor.  We sneak around this by 
    // intercepting selectWithFrame and editWithFrame and sneaking a 
    // reduced, centered rect in at the last minute.
    if (mIsEditingOrSelecting == NO)
    {
        // Get our ideal size for current text
        NSSize textSize = [self cellSizeForBounds:theRect];
        // Center that in the proposed rect
        float heightDelta = newRect.size.height - textSize.height;    
        if (heightDelta > 0)
        {
            newRect.size.height -= heightDelta;
            newRect.origin.y += (heightDelta / 2);
        }
    }
    
    return newRect;
}
- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(int)selStart length:(int)selLength
{
    aRect = [self drawingRectForBounds:aRect];
    mIsEditingOrSelecting = YES;    
    [super selectWithFrame:aRect inView:controlView editor:textObj delegate:anObject start:selStart length:selLength];
    mIsEditingOrSelecting = NO;
}
- (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent
{    
    aRect = [self drawingRectForBounds:aRect];
    mIsEditingOrSelecting = YES;
    [super editWithFrame:aRect inView:controlView editor:textObj delegate:anObject event];
    mIsEditingOrSelecting = NO;
}
@end
[/AppleScript]
The .h file:
//
//  RSVerticallyCenteredTextFieldCell.h
//  RSCommon
//
//  Created by Daniel Jalkut on 6/17/06.
//  Copyright 2006 Red Sweater Software. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface RSVerticallyCenteredTextFieldCell : NSTextFieldCell
{
BOOL mIsEditingOrSelecting;
}
@end
Model: Mac Studio Ultra
Browser: Safari 605.1.15
Operating System: macOS 12