当useSingleLineMode为true时,NSTextField非系统字体内容被剪切 [英] NSTextField non-system-font content clipped when usesSingleLineMode is true

查看:100
本文介绍了当useSingleLineMode为true时,NSTextField非系统字体内容被剪切的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于非系统字体,将usingSingleLineMode设置为true会导致文本顶部被剪切.

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

我创建了3个非常简单的测试用例来说明这一点:

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

  • good :非系统字体,usesSingleLineMode = false.效果很好.
  • :usesSingleLineMode = true的非系统字体.不起作用.
  • system :具有usesSingleLineMode = true的系统字体.效果很好.
  • 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.

将以下内容添加到新的Cocoa OSX应用程序的viewDidLoad()方法中:

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)

如果我在情节提要中使用Interface Builder创建相同的bad NSTextField,请在IB中设置字体,然后选中在IB中使用单行模式",效果很好!但是,在IB中构建整体视图是不切实际的,因此我想以编程方式创建它.

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.

为什么会这样?我是否错过了一些重要的设置(我尝试过将许多NSTextField和NSTextFieldCell参数调整为无济于事吗?是否有解决方法?

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?

推荐答案

根据Apple本身,这是正确的,甚至是预期的行为:

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:

根据NSCell的文档,这可以正常运行:

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.

来源: 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.

这篇关于当useSingleLineMode为true时,NSTextField非系统字体内容被剪切的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆