I am trying to draw a text with background color and border radius, but the text does not show.
I've used sizeToFit
to make the textfield fit its content.
When the below code is added to its superview, this is what is shown:
As you can see, the text is missing even though the view has adjusted itself to fit the text. I have tried setting a huge frame but its the same issue.
let text = NSTextField(frame: NSRect(x: 0, y: 0, width: 0, height: 0))
text.stringValue = "ABC"
text.sizeToFit()
text.wantsLayer = true
text.isBordered = true
text.drawsBackground = true
let textFieldLayer = CALayer()
text.layer = textFieldLayer
text.backgroundColor = NSColor.blue
text.layer?.backgroundColor = NSColor.blue.cgColor
text.layer?.borderColor = NSColor.red.cgColor
text.layer?.borderWidth = 1
text.layer?.cornerRadius = 5
text.textColor = NSColor.red
let positionRelativeToScreen = toOrigin(point: positionFlipped, size: text.frame.size)
let positionRelativeToWindow = borderWindow.convertPoint(fromScreen: positionRelativeToScreen)
text.frame.origin = positionRelativeToWindow
return text