Xcode NSTextField文本不显示
我试图用背景色和边框半径绘制文本,但文本不显示 我使用了Xcode NSTextField文本不显示,xcode,macos,cocoa,Xcode,Macos,Cocoa,我试图用背景色和边框半径绘制文本,但文本不显示 我使用了sizeToFit使文本字段适合其内容 将以下代码添加到其superview时,将显示以下内容: 如您所见,即使视图已调整自身以适应文本,文本仍丢失。我尝试过设置一个巨大的框架,但它是相同的问题 let text = NSTextField(frame: NSRect(x: 0, y: 0, width: 0, height: 0)) text.stringValue = "ABC" text.sizeToFit(
sizeToFit
使文本字段适合其内容
将以下代码添加到其superview时,将显示以下内容:
如您所见,即使视图已调整自身以适应文本,文本仍丢失。我尝试过设置一个巨大的框架,但它是相同的问题
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
解决方案是删除
textFieldLayer
。谢谢@Willeke 可能是@Willeke的副本是的。在我删除textFieldLayer后工作。