使用 NSTextField 创建标签是模糊的
我正在尝试使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您没有背景(包括透明背景)并且您的文本是任何图层支持的超级视图的子视图(您已在代码或 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.
我遇到了同样的问题,但我已经通过以下方式解决了它:
I had the same problem, but I have solved it by:
确保将
NSTextField
的框架设置为包含所有整数值。如有必要,请使用roundf()。
我得到了一个模糊的
NSTextField
,并且添加纯色背景或从视图层次结构中删除核心动画层都不是我的选择。我注意到我将此文本字段的框架设置为 Y 值为 4.5,因此以下更改解决了我的问题:模糊标签:
无模糊:(
在上面的示例中,
_labelFrame
和someRect
是NSRect
,而someConstant
是CGFloat
正如你所看到的,我正在做的计算。第一个示例的第二行将非整数值传递给 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:
No blur:
(In the above examples,
_labelFrame
andsomeRect
areNSRect
s, andsomeConstant
is aCGFloat
. As you can see, the calculation I was doing in the second line of the first example was passing a non-integer value toNSOffsetRect
).由于这是我第一次子类化 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
如果您通过 XIB 创建它并且它很模糊,我找到了修复方法:
如果我注释掉它,它会变得模糊;如果放回去,它就晶莹剔透了。
If you created it via an XIB and it is blurry, I found the fix:
If I comment this out it goes blurry; if put back it's crystal clear.
试试下面这个:
Try this below:
开箱即用,但值得一提。我浪费了一整天的时间来调试这个问题。我使用的是非苹果外接显示器,这就是问题所在。当我在 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.
简单地,
从 InterfaceBuilder 添加属性
Simply, add
property from InterfaceBuilder