Non-system font NSTextField content trimmed using SingleLineMode is true

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

I created 3 very simple test cases that illustrate this:

  • good: non-system font using SingleLineMode = false. It works great.
  • bad: non-system font with usesSingleLineMode = true. Does not work.
  • system: system font with usesSingleLineMode = true. It works great.

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

// 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 the storyboard, set the font in IB and check the Single Line Mode in IB, it works great! But it would be impractical to build a general view in IB, so I want to programmatically create it.

Why is this happening? I missed some important parameter (I tried setting many of the NSTextField and NSTextFieldCell parameters to no avail? Is there a workaround?

+5
source share
2 answers

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

Engineering has determined that this problem behaves as expected based on the following information:

This behaves correctly according to the documentation for NSCell:

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

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

What the documentation says is really correct, but the important detail here is that the documentation does not say. He says that “the basic position of the text is determined solely by the size of the control”, but does not explain in detail how this is done. And it is a well-known fact that the basic level always seems to be correct for the system font, but it is hardly ever suitable for any other font on your system. The problem is that Apple speaks of a “fixed base layout” as if it would be something well known and well documented, but it is not. I did not find a single document, even among outdated ones, which would explain the fixed source layout.

+1
source

I have the same problem and I solved it by setting usesSingleLineMode=false

-1
source

Source: https://habr.com/ru/post/1245624/


All Articles