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
NSTextField transparent background
提问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. If I set background color
结果文本看起来很糟糕。如果我设置背景颜色
self.myTextField.backgroundColor = [NSColor colorWithCalibratedRed:0.85 green:0.85 blue:0.85 alpha:1.0];
everything looks okI have also tried with
drawsBackground = NO;
Do you guys know how to fix this?
一切看起来都不错我也试过
drawsBackground = NO;
你们知道如何解决这个问题吗?
采纳答案by pawelropa
I ended up using CATextLayer
instead 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
回答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 backgroundColor
from the NSTextField's superview.
我刚才有这个问题。我通过删除backgroundColor
从 NSTextField 的超级视图中命名的属性来修复它。
I was using backgroundColor
just 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 的颜色是可见的。