如何修复使用 CALayer 时对焦环高光被剪切的问题?
窗口的 contentView 有 NSTextFields。 (MacOS)
我正在使用界面生成器来设置窗口及其功能,因此 awakeFromNib 中除了以下内容之外什么都没有:
[[self.window contentView] setWantsLayer:YES];
CALayer *layer = [[self.window contentView] layer];
CGColorRef lightGray = CGColorCreateGenericGray(0.93, 1.0);
[layer setBackgroundColor:lightGray];
CGColorRelease(lightGray);
但是,文本字段的焦点环在文本字段的边界之外不可见。
带层:
没有图层:
我认为这个显示问题比这更大,但我可以找不到这个问题的例子(和解决方案)。
有人吗?
The window's contentView has NSTextFields. (MacOS)
i'm using interface builder to setup the window and its features, so nothing in awakeFromNib except this:
[[self.window contentView] setWantsLayer:YES];
CALayer *layer = [[self.window contentView] layer];
CGColorRef lightGray = CGColorCreateGenericGray(0.93, 1.0);
[layer setBackgroundColor:lightGray];
CGColorRelease(lightGray);
However, the textField's focus ring is not visible outside the bounds of the textField.
With layer:
Without a layer:
i'm thinking that this display issue is something bigger than this, but i can't find an example of this problem, (and solution).
anyone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个长期已知的问题 - 有一个 早在 2008 年就在 Cocoa-dev 邮件列表中提及。聚焦环实际上是在其视图框架之外绘制的。 NSView 绘图可以执行此操作,但它不适用于 CALayers。尽管 Leopard AppKit 发行说明中充满希望称为视图、聚焦环和绘图性能,这一点没有改变。
理论上,可以调整文本字段的图层框架并添加一些填充以允许聚焦环完全显示。然而,我刚刚尝试过这个,似乎至少对于 NSTextField 来说,它不会在更改后的框架内整齐地绘制。不过,请尝试一下——我并没有为此大惊小怪太久。 (对焦环在每个方向上从框架中伸出 4 个像素。)
请注意,您在技术上违反了图层支持的规则,这可能是您遇到困难的部分原因。来自
NSView setWantsLayer:
文档:您正在使用图层支持的视图并更改其背景。您可以通过创建自己的图层来解决这个问题,但是:
这排除了在视图中放置文本字段的可能性。
(如果需要,请参阅此问题:层支持视图和层托管视图之间的区别?)
This is a long-known issue -- there's a mention on the Cocoa-dev mailing list back in 2008. The focus ring actually gets drawn outside its view's frame. NSView drawing is allowed to do this, but it doesn't work with CALayers. Despite a hopeful note in the Leopard AppKit release notes called Views, Focus Rings, and Drawing Performance, this has not changed.
In theory, it's possible to monkey around with the text fields' layers' frames and add some padding to allow the focus ring to show fully. However, I've just tried this and it seems that, for NSTextField at least, it won't draw neatly inside a changed frame. Give it a try, though -- I didn't fuss with it for too long. (The focus ring extends 4 pixels out from the frame in each direction.)
Just as a note, you are technically breaking the layer-backed rules, which may be part of why you are having difficulty. From
NSView setWantsLayer:
docs:You're using a layer-backed view and changing its background. You could get around this by making your own layer, but:
which rules out putting text fields in the view.
(Refer to this SO question if needed: Difference Between Layer-Backed and Layer-Hosting Views?)