I have an NSTextField and I want to vertically center align the text in it. Basically I need the NSTextField answer of How do I vertically center UITextField Text?
Anyone got some pointers? Thanks!
I have an NSTextField and I want to vertically center align the text in it. Basically I need the NSTextField answer of How do I vertically center UITextField Text?
Anyone got some pointers? Thanks!
You could subclass NSTextFieldCell
to do what you want:
MDVerticallyCenteredTextFieldCell.h:
#import <Cocoa/Cocoa.h>
@interface MDVerticallyCenteredTextFieldCell : NSTextFieldCell {
}
@end
MDVerticallyCenteredTextFieldCell.m:
#import "MDVerticallyCenteredTextFieldCell.h"
@implementation MDVerticallyCenteredTextFieldCell
- (NSRect)adjustedFrameToVerticallyCenterText:(NSRect)frame {
// super would normally draw text at the top of the cell
NSInteger offset = floor((NSHeight(frame) -
([[self font] ascender] - [[self font] descender])) / 2);
return NSInsetRect(frame, 0.0, offset);
}
- (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView
editor:(NSText *)editor delegate:(id)delegate event:(NSEvent *)event {
[super editWithFrame:[self adjustedFrameToVerticallyCenterText:aRect]
inView:controlView editor:editor delegate:delegate event:event];
}
- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView
editor:(NSText *)editor delegate:(id)delegate
start:(NSInteger)start length:(NSInteger)length {
[super selectWithFrame:[self adjustedFrameToVerticallyCenterText:aRect]
inView:controlView editor:editor delegate:delegate
start:start length:length];
}
- (void)drawInteriorWithFrame:(NSRect)frame inView:(NSView *)view {
[super drawInteriorWithFrame:
[self adjustedFrameToVerticallyCenterText:frame] inView:view];
}
@end
You can then use a regular NSTextField
in Interface Builder, and specify MDVerticallyCenteredTextFieldCell
(or whatever you want to name it) as the custom class for the text field's text field cell (select the textfield, pause, then click the textfield again to select the cell inside the text field):
Swift 3.0 version (Create a custom subclass for NSTextFieldCell):
override func drawingRect(forBounds rect: NSRect) -> NSRect {
var newRect = super.drawingRect(forBounds: rect)
let textSize = self.cellSize(forBounds: rect)
let heightDelta = newRect.size.height - textSize.height
if heightDelta > 0 {
newRect.size.height -= heightDelta
newRect.origin.y += (heightDelta / 2)
}
return newRect
}
It's better to use boundingRectForFont
and the function ceilf()
when calculating possible maximum font height, because the above-mentioned solution causes text to be cut off under the baseline. So the adjustedFrameToVerticallyCenterText:
will look like this
- (NSRect)adjustedFrameToVerticallyCenterText:(NSRect)rect {
CGFloat fontSize = self.font.boundingRectForFont.size.height;
NSInteger offset = floor((NSHeight(rect) - ceilf(fontSize))/2);
NSRect centeredRect = NSInsetRect(rect, 0, offset);
return centeredRect;
}