如何修复使用 CALayer 时对焦环高光被剪切的问题?

发布于 2024-10-20 09:09:25 字数 632 浏览 3 评论 0原文

窗口的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自此以后,行同陌路 2024-10-27 09:09:25

这是一个长期已知的问题 - 有一个 早在 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:

When using layer-backed views you should never interact directly with the layer.

You're using a layer-backed view and changing its background. You could get around this by making your own layer, but:

When using a layer-hosting view you should not rely on the view for drawing, nor should you add subviews to the layer-hosting view.

which rules out putting text fields in the view.

(Refer to this SO question if needed: Difference Between Layer-Backed and Layer-Hosting Views?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文