Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are a couple of potential solutions posted in a <a href="https://stackoverflow.com/questions/1235219/is-there-a-right-way-to-have-nstextfieldcell-draw-vertically-centered-text">similar question</a> which I asked a while back.</p> <p>In all honesty, I still use the undocumented <code>_cFlags.vCentered</code> boolean (<em>tsk</em> <em>tsk</em>, bad programmer!) to get the job done. It's simple, and it works. I'll reinvent the wheel later on if I have to.</p> <p><strong>update:</strong></p> <p>OK, I think I've figured it out. Both solutions rely on a call to <code>super</code> to get the default rect, and then modify <code>origin.y</code> and <code>size.height</code> to perform the vertical centering. The calls to <code>super</code>, however, return a rectangle whose width has already been adjusted to fit the text horizontally.</p> <p>The solution is to use <code>origin.x</code> and <code>size.width</code> from the bounds rect that is passed in to the method:</p> <blockquote> <p>In solution #1: </p> </blockquote> <pre><code>- (NSRect)titleRectForBounds:(NSRect)theRect { NSRect titleFrame = [super titleRectForBounds:theRect]; NSSize titleSize = [[self attributedStringValue] size]; // modified: theRect.origin.y += (theRect.size.height - titleSize.height)/2.0 - 0.5; return theRect; } </code></pre> <blockquote> <p>In solution #2: </p> </blockquote> <pre><code>- (NSRect)drawingRectForBounds:(NSRect)theRect { NSRect newRect = [super drawingRectForBounds:theRect]; // modified: newRect.origin.x = theRect.origin.x; newRect.size.width = theRect.size.width; 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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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