macos NSTextField 透明背景

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11120654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 09:16:21  来源:igfitidea点击:

NSTextField transparent background

macoscocoanstextfield

提问by pawelropa

I create transparentNSTextField

我创造透明NSTextField

self.myTextField = [[NSTextField alloc] initWithFrame:CGRectMake(backgroundView.frame.origin.x + backgroundView.frame.size.width + 20, self.projectTitle.frame.origin.y - 30.0, 100, 20)];
self.myTextField.editable = NO;
self.myTextField.bezeled = NO;
self.myTextField.drawsBackground = YES;
self.myTextField.backgroundColor = [NSColor clearColor];
self.myTextField.selectable = NO;
self.myTextField.font = [NSFont fontWithName:@"Helvetica Neue" size:16];

    [self addSubview:self.compressingTime];

And as a result text look bad. enter image description hereIf I set background color

结果文本看起来很糟糕。在此处输入图片说明如果我设置背景颜色

    self.myTextField.backgroundColor = [NSColor colorWithCalibratedRed:0.85 green:0.85 blue:0.85 alpha:1.0];

everything looks okenter image description hereI have also tried with drawsBackground = NO;Do you guys know how to fix this?

一切看起来都不错在此处输入图片说明我也试过drawsBackground = NO;你们知道如何解决这个问题吗?

采纳答案by pawelropa

I ended up using CATextLayerinstead NSTextField.

我最终CATextLayer改为使用NSTextField.

回答by Alex Gray

The secret is setting ALL THREE of these properties on the NSTextField...

秘诀是在NSTextField...上设置所有三个这些属性

myTextField.bezeled         = NO;
myTextField.editable        = NO;
myTextField.drawsBackground = NO;

回答by Gamma-Point

There is a property in the .xib file, on the interface builder window for the text field, under attribute inspector

.xib 文件中有一个属性,在文本字段的界面构建器窗口中,在属性检查器下

  1. Check the Display Draws Background
  2. Select a background color. Select clear color for transparent background.
  1. 检查显示绘制背景
  2. 选择一种背景颜色。为透明背景选择清晰的颜色。

enter image description here

在此处输入图片说明

回答by Mark Bridges

As of 10.12 you can just do:

从 10.12 开始,您可以执行以下操作:

let label = NSTextField(labelWithString: "HELLO")

回答by Symanski

Came here looking for this too, and have got the background to give me a transparent grey. Key is to not have a bezel. My code below:

来这里也是为了找这个,并且有背景给我一个透明的灰色。关键是没有挡板。我的代码如下:

NSTextField *yourLabel = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, width , height * 1.0/3.0)];
yourLabel.editable = false;
yourLabel.bezeled = false;
[yourLabel setTextColor:[NSColor blackColor]];
[yourLabel setBackgroundColor:[NSColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.1]];

For completeness I had got the width and height earlier because they get used many times for layout:

为了完整起见,我之前得到了宽度和高度,因为它们被多次用于布局:

height = self.window.frame.size.height;
width = self.window.frame.size.width;

回答by joerick

I had this problem just now. I fixed it by removing a property named backgroundColorfrom the NSTextField's superview.

我刚才有这个问题。我通过删除backgroundColor从 NSTextField 的超级视图中命名的属性来修复它。

I was using backgroundColorjust as a convenience getter/setter for the CALayer properties on an NSView subclass. Although this property isn't documented on NSView, it looks like I had accidentally overridden a property on NSView.

backgroundColor只是将其用作 NSView 子类上 CALayer 属性的便利 getter/setter。虽然这个属性没有记录在 NSView 上,但看起来我不小心覆盖了 NSView 上的一个属性。

Yay for subclassing!

是的子类化!

回答by Kakey

The clear color will make the current view (ie)NSTextView's background as transparent hence the color of NSView which holds the NSTextView is visible.

清晰的颜色将使当前视图(即)NSTextView 的背景透明,因此保存 NSTextView 的 NSView 的颜色是可见的。