使用 NSTextField 创建标签是模糊的

发布于 2024-09-27 19:31:17 字数 653 浏览 9 评论 0原文

我正在尝试使用 NSTextField 以编程方式创建标签,但结果很模糊: screenshot

到目前为止,这是我的代码:

NSTextfield *textfield = [[NSTextField alloc] initWithFrame:NSMakeRect(5,5,150,20)];
[texField setStringValue:@"some text here"];
[textField setEditable:NO];
[textField setSelectable:NO];
[textField setBordered:NO]
[textField setDrawsBackground:NO]

我已将问题追溯到 setDrawsBackground 行。我也尝试过使用 [textField setBackgroundColor:[NSColor clearColor] ,但没有运气。

顺便说一句,我已将 textField 添加到作为滚动视图子视图的视图的子视图中。我还在所有视图级别上使用了 isOpaque ,但再次没有运气。

非常感谢任何帮助。

I'm trying to create a label programmatically using NSTextField, but it comes out blurry: screenshot

This is my code so far:

NSTextfield *textfield = [[NSTextField alloc] initWithFrame:NSMakeRect(5,5,150,20)];
[texField setStringValue:@"some text here"];
[textField setEditable:NO];
[textField setSelectable:NO];
[textField setBordered:NO]
[textField setDrawsBackground:NO]

I've traced the problem down to the setDrawsBackground line. I've also tried using [textField setBackgroundColor:[NSColor clearColor] as well, but no luck.

By the way, I've adding to a textField to the subview of a view that is a subview of a scrollview. I've also playing with isOpaque on all the view levels, but no luck there again.

Any help is greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

夜雨飘雪 2024-10-04 19:31:17

如果您没有背景(包括透明背景)并且您的文本是任何图层支持的超级视图的子视图(您已在代码或 IB 中打开“想要图层”以允许动画/过渡),您将得到模糊的文本。您必须选择没有图层支持的视图或具有纯背景色的标签。

If you have no background (including clear) and your text is a subview of any layer-backed superview (you've turned on "wants layer" in code or in IB to allow animations/transitions), you'll get blurry text. You have to choose either no layer backed view or a label with a solid background color.

画离情绘悲伤 2024-10-04 19:31:17

我遇到了同样的问题,但我已经通过以下方式解决了它:

textfield.canDrawSubviewsIntoLayer = true

I had the same problem, but I have solved it by:

textfield.canDrawSubviewsIntoLayer = true
卖梦商人 2024-10-04 19:31:17

确保将 NSTextField 的框架设置为包含所有整数值

如有必要,请使用roundf()。

我得到了一个模糊的 NSTextField,并且添加纯色背景或从视图层次结构中删除核心动画层都不是我的选择。我注意到我将此文本字段的框架设置为 Y 值为 4.5,因此以下更改解决了我的问题:

模糊标签:

_label.frame = NSOffsetRect(_labelFrame,
                            -0.5 * (someRect.size.width + someConstant),
                            0.0);

无模糊:(

_label.frame = NSOffsetRect(_labelFrame,
                            roundf(-0.5 * (someRect.size.width + someConstant)),
                            0.0);

在上面的示例中,_labelFramesomeRectNSRect,而 someConstantCGFloat 正如你所看到的,我正在做的计算。第一个示例的第二行将非整数值传递给 NSOffsetRect )。

Make sure you're setting the frame of your NSTextField to something with all integer values.

Use roundf() if necessary.

I was getting a blurry NSTextField, and neither adding a solid background nor removing Core Animation layers from my view hierarchy were options for me. I noticed I was setting the frame of this text field to something with a Y value of 4.5, so the following changes fixed the issue for me:

Blurry label:

_label.frame = NSOffsetRect(_labelFrame,
                            -0.5 * (someRect.size.width + someConstant),
                            0.0);

No blur:

_label.frame = NSOffsetRect(_labelFrame,
                            roundf(-0.5 * (someRect.size.width + someConstant)),
                            0.0);

(In the above examples, _labelFrame and someRect are NSRects, and someConstant is a CGFloat. As you can see, the calculation I was doing in the second line of the first example was passing a non-integer value to NSOffsetRect).

忘羡 2024-10-04 19:31:17

由于这是我第一次子类化 NSView,因此我将上面的代码放在 drawRect 方法中,而不是 initWithFrame 方法中。我这样做是因为我正在关注 Apple 开发站点上的示例应用程序之一。

这也导致我在滚动时 CPU 使用率飙升

Since this was my first time subclassing NSView, I had put the above code in the drawRect method instead of the initWithFrame method. I did this because I was following one of the sample applications from Apple's Dev site.

This was also causing my CPU usage to spike when I was scrolling

小兔几 2024-10-04 19:31:17

如果您通过 XIB 创建它并且它很模糊,我找到了修复方法:

[textField setStringValue:@""];

如果我注释掉它,它会变得模糊;如果放回去,它就晶莹剔透了。

If you created it via an XIB and it is blurry, I found the fix:

[textField setStringValue:@""];

If I comment this out it goes blurry; if put back it's crystal clear.

谎言 2024-10-04 19:31:17

试试下面这个:

textField.drawsBackground = true
textField.backgroundColor = NSColor.white.withAlphaComponent(.leastNormalMagnitude)

Try this below:

textField.drawsBackground = true
textField.backgroundColor = NSColor.white.withAlphaComponent(.leastNormalMagnitude)
埖埖迣鎅 2024-10-04 19:31:17

开箱即用,但值得一提。我浪费了一整天的时间来调试这个问题。我使用的是非苹果外接显示器,这就是问题所在。当我在 Mac Book Pro 中打开该应用程序时,一切都很好。
因此,我使用的三星显示器可能是非视网膜显示器。

Out of the box, but worth to mention. I have wasted by entire day debugging the issue. I am using a non-apple external monitor and this is where the issue is identified. Once i open the app in Mac book pro, it is perfectly fine.
So, the Samsung monitor which i am using might be non-retina.

峩卟喜欢 2024-10-04 19:31:17

简单地,

CanDrawConcurrently = true 

从 InterfaceBuilder 添加属性

Simply, add

CanDrawConcurrently = true 

property from InterfaceBuilder

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