NSTextField奇怪的左边距 [英] NSTextField weird left margins

查看:137
本文介绍了NSTextField奇怪的左边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整天都在寻找解决方案.

我有4个NSTextField(实际上是为一些自定义操作子类化的),它们全部共享相同的X位置.

问题在于,某些样式(浅色,常规,粗体)可能会有所不同,并且尺寸可能会有所不同.

发生的事情是,即使X的原点相同,第一个字母也总是有一点(始终不同)的左边距.

请参见图片: https://dl.dropbox.com/u/1977230/Screen%20Shot%202012-12-11%20at%2017.55.58.png

我要确保所有行都从同一点开始,例如从左侧开始100px.

有什么主意如何覆盖那些奇怪的填充物?

欢呼

解决方案

您肯定在说的余量是NSTextField使用的NSTextContainer上的lineFragmentPadding.

请参阅NSTextContainer参考:

http://developer.apple.com/library/Mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSTextContainer_Class/Reference/Reference.html

这是有关文本布局的教程中的页面:

https://developer.apple .com/library/mac/#documentation/Cocoa/Conceptual/TextLayout/Concepts/CalcTextLayout.html

该文章指出:

排字机在实际适合文本时进行最后的调整 进入矩形.此调整是由 NSTextContainer对象,称为行片段填充,其中 定义线段矩形左端各部分 空白的.文本在线段矩形内插入了此数量 (矩形本身不受影响).填充允许小规模 调整文本容器的边缘和周围的区域 孔并防止文本直接邻接任何其他图形 显示在该区域附近.您可以更改其默认填充 setLineFragmentPadding:方法的值.注意行片段 填充不是表达边距的合适方法;你应该设置 NSTextView对象的位置和大小(用于文档边距)或 文本页边距的段落边距属性.

不幸的是,看起来NSTextFieldNSTextContainerNSLayoutManager是私有且不可访问的,但是看来它们可以在NSTextView中访问:

https://dl.dropbox.com/u/1977230/Screen%20Shot%202012-12-11%20at%2017.55.58.png

I want to make sure that all lines start exactly at the same point, say 100px from the left.

Any idea how to override that weird padding?

Cheers

解决方案

The margin you're talking about I'm pretty sure is the lineFragmentPadding on the NSTextContainer that is used by the NSTextField.

See the NSTextContainer reference:

http://developer.apple.com/library/Mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSTextContainer_Class/Reference/Reference.html

And here's a page from the tutorial on Text Layout:

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/TextLayout/Concepts/CalcTextLayout.html

It states in that article:

The typesetter makes one final adjustment when it actually fits text into the rectangle. This adjustment is a small amount fixed by the NSTextContainer object, called the line fragment padding, which defines the portion on each end of the line fragment rectangle left blank. Text is inset within the line fragment rectangle by this amount (the rectangle itself is unaffected). Padding allows for small-scale adjustment of the text container’s region at the edges and around any holes and keeps text from directly abutting any other graphics displayed near the region. You can change the padding from its default value with the setLineFragmentPadding: method. Note that line fragment padding isn’t a suitable means for expressing margins; you should set the NSTextView object’s position and size for document margins or the paragraph margin attributes for text margins.

Unfortunately, it looks like NSTextField's NSTextContainer and NSLayoutManager are private and inaccessible, but it appears they are accessible in an NSTextView:

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSTextView_Class/Reference/Reference.html#//apple_ref/occ/cl/NSTextView

So that may be the class you need to subclass if you want to have minute control over this kind of functionality.

这篇关于NSTextField奇怪的左边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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