floats in AppKit

Recently I wanted to automatically center a NSView inside a window. But the buttons inside this view were displayed pretty strange:

You can see, the buttons and the textfield are drawn blurred and the buttons have a stripe on both sides of their label.

It took me quite some hours to figure out what was going wrong, but then I found the bug: floats with a fraction portion.
AppKit uses floats all over the place, but it seems those floats have to be truncated in order to draw the view properly. After using truncf(aFloat) everything was displayed as one could expect it:

I attached a sample project to this post, so you can have a look yourself.

sample project

3 Responses to “floats in AppKit”

  1. Mike Says:

    I’ve also had trouble with weird rendering bugs in WebView when NSViews in NSSplitView were floats. So I guess as a general rule its good to round off all your view demensions.

  2. Patrick Says:

    Hmm, that really does look strange…

    Rather than use truncf I would try NSIntegralRect(). It might even be designed just for these kinds of situations?

  3. Karsten Says:

    thx a lot for the hint Patrick, I didn’t see that function 😉