2

On macOS Big Sur, a simple override of NSButtonCell's drawTitle:withFrame:inView messes up the vertical centering of the text. I'd like to understand why. (Note that this bug does not show up on macOS Mojave; I'm not sure about Catalina.)

I've created a sample Xcode project that demonstrates the issue at https://github.com/TroikaTronix/ButtonTextVerticalCenterIssue

If you look at the .xib file, you'll find a Window with a Check Box and a Radio Button, each set to a height of 40 pixels. The text is shown centered vertically in interface builder. Both of these are set to use my IZButton and IZButtonCell override classes.

In the only actual override in IZButton.m, I simply call super with the same parameters passed to the method in the first place, e.g.:

- (NSRect)drawTitle:(NSAttributedString*)title withFrame:(NSRect)frame inView:(NSView*)controlView
{
    return [super drawTitle:title withFrame:frame inView:controlView];
}

If you run the code as is, you will see that the text is now hugging the bottom of the 40px space.

enter image description here

If you then go to IZButton.m and comment out the entire drawTitle:withFrame:inView method, the text is vertically as it should be.

The titleRectForBounds override described here will solve the problem. But I'd like to understand why this happens in the first place.

Any ideas?

Mark Coniglio
  • 351
  • 1
  • 10

0 Answers0