Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTextField non-system-font content clipped when usesSingleLineMode is true

Setting usesSingleLineMode to true for a non-system font causes the top of the text to be clipped.

I've created 3 very simple test cases that illustrate this:

  • good : non-system font, with usesSingleLineMode = false. Works fine.
  • bad : non-system font with usesSingleLineMode = true. Does not work.
  • system : system font with usesSingleLineMode = true. Works fine.

Add the following to the viewDidLoad() method of a new Cocoa OSX application:

// Do any additional setup after loading the view.
let good = NSTextField(frame: NSRect(x: 0, y: 0, width: 800, height: 55))
good.usesSingleLineMode = false
good.font = NSFont(name: "HelveticaNeue-UltraLight", size: 24)
good.stringValue = "Good usesSingleLineMode false "
self.view.addSubview(good)

let bad = NSTextField(frame: NSRect(x: 0, y: 100, width: 800, height: 55))
bad.usesSingleLineMode = true
bad.font = NSFont(name: "HelveticaNeue-UltraLight", size: 24)
bad.stringValue = "Bad usesSingleLineMode true"
self.view.addSubview(bad)

let system = NSTextField(frame: NSRect(x: 0, y: 200, width: 800, height: 55))
system.usesSingleLineMode = true
system.font = NSFont.systemFontOfSize(24)
system.stringValue = "Good usesSingleLineMode true, System Font"
self.view.addSubview(system)

See the result

If I create the same bad NSTextField using Interface Builder in a storyboard, set the font in IB and check Uses Single Line Mode in IB it works fine! But, it would be impractical to build the overall view in IB, thus I want to programmatically create it.

Why is this happening? Have I missed some important setting (I've tried adjusting many NSTextField and NSTextFieldCell parameters to no avail? Is there a workaround?

like image 717
drootang Avatar asked Mar 23 '16 13:03

drootang


1 Answers

According to Apple themselves, this is correct and even desired behavior:

Engineering has determined that this issue behaves as intended based on the following information:

This behaves correctly according to the documentation for NSCell:

Cells in the single line mode use the fixed baseline layout. The text baseline position is determined solely by the control size regardless of content font style or size.

Source: http://www.openradar.me/13813516

What the documentation says is really correct but the important detail here is what the documentation does not say. It does say that the "text baseline position is determined solely by the control size" but it does not explain in detail how this is done. And it is a known fact, that the baseline always seems to fit correctly to the system font, yet it hardly ever fits to any other font on your system. The problem is that Apple speaks of "the fixed baseline layout", as if that would be something known and well documented, but it isn't. I haven't found any document, not even among the legacy ones, that would explain the fixed baseline layout.

like image 54
Mecki Avatar answered Sep 29 '22 11:09

Mecki