Note that there are some explanatory texts on larger screens.

plurals
  1. PONSTextFieldCell vertical alignment, solutions seem to squash the horizontal alignment
    text
    copied!<p>I have a NSTextFieldCell that I wish to display with middle vertical alignment. Thanks to an older question here and a blog entry I have two working solutions. </p> <p>However, both solutions seem to squash my ability to set the cell as right aligned. Can anyone help me make either of these solutions support both forms of alignment?</p> <p>Here is the code for one solution:</p> <pre><code>@implementation MiddleAlignedTextFieldCell - (NSRect)titleRectForBounds:(NSRect)theRect { NSRect titleFrame = [super titleRectForBounds:theRect]; NSSize titleSize = [[self attributedStringValue] size]; titleFrame.origin.y = theRect.origin.y - .5 + (theRect.size.height - titleSize.height) / 2.0; return titleFrame; } - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { NSRect titleRect = [self titleRectForBounds:cellFrame]; [[self attributedStringValue] drawInRect:titleRect]; } @end </code></pre> <p>The alternative solution is (obtained from <a href="http://www.red-sweater.com/blog/148/what-a-difference-a-cell-makes" rel="noreferrer">this blog</a>):</p> <pre><code>@implementation RSVerticallyCenteredTextFieldCell - (NSRect)drawingRectForBounds:(NSRect)theRect { NSRect newRect = [super drawingRectForBounds:theRect]; 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 &gt; 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:theEvent]; mIsEditingOrSelecting = NO; } @end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload