Label no-transparent inside a NSView transparent

(Swift, macOS)


I have a label inside an NSView and a window that are transparents. I try to make the Window transparent, the NSView semi-transparent but keep the Label non-transparent.


This makes the NSView1 and the Label1 semi-transparent:


@IBOutlet weak var NSView1: NSView!

@IBOutlet weak var Label1: NSTextField!


@IBAction func button1(_ sender: NSButton) {

// window transparent:

view.window?.isOpaque = false

view.window?.backgroundColor = NSColor.clear


// NSView 50% transparent

NSView1.wantsLayer = true

NSView1.layer?.backgroundColor = NSColor.white.cgColor

NSView1.alphaValue = 0.5


// I tried a label non transparent, but I still see the label semi-transparent :

// Label1.backgroundColor = NSColor.black.withAlphaComponent(1)

// Label1.backgroundColor = NSColor.black

}

Accepted Reply

Just take the label out of the view (it will appear above NSView1 in the list of objects in the window

You can do it by dragging the Label1 in the list of objects in IB above the NSView1.

You will see the object move on the canvas.

but you can still set constraint so that it centers on NSView1.

Then call Editor > Resolve Layout to update frames on selected label.


PS: var names should begin by lowercase, as label1.

Replies

If you want the view transparent but not the label, set the view color to clear color and set the label background color to the color you want it to be.

I tried that before asking and it did not work. Now I see the problem is that the window also is transparent. It is the window and not the NSView that makes the label trans. So I update the question. How to make the label non transparent if the window is transparent?


view.window?.isOpaque = false

view.window?.backgroundColor = NSColor.clear

Just take the label out of the view (it will appear above NSView1 in the list of objects in the window

You can do it by dragging the Label1 in the list of objects in IB above the NSView1.

You will see the object move on the canvas.

but you can still set constraint so that it centers on NSView1.

Then call Editor > Resolve Layout to update frames on selected label.


PS: var names should begin by lowercase, as label1.