Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xcode NSTextField文本不显示_Xcode_Macos_Cocoa - Fatal编程技术网

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后工作。