NSTextField transparent background

只愿长相守 提交于 2019-11-29 20:53:43

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

myTextField.bezeled         = NO;
myTextField.editable        = NO;
myTextField.drawsBackground = NO;
Gamma-Point

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

  1. Check the Display Draws Background
  2. Select a background color. Select clear color for transparent background.

As of 10.12 you can just do:

let label = NSTextField(labelWithString: "HELLO")

I ended up using CATextLayer instead NSTextField.

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;

You don’t have to switch to CATextLayer to effectively tell your application to use layers for the text field. You can just use this instead:

textField.drawsBackground = NO;
textField.wantsLayer      = YES;

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

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.

Yay for subclassing! 😒

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!