如果您只是想在不透明的背景上看到清晰的文字並使用CATextLayer將您的頭靠在牆上 - 放棄並使用NSTextField,並將插入禁用並將linefragmentpadding設置爲0.示例代碼很快,但您應該能夠輕鬆翻譯...
var testV = NSTextView()
testV.backgroundColor = NSColor(calibratedRed: 0.73, green: 0.84, blue: 0.89, alpha: 1)
testV.frame = CGRectMake(120.0, 100.0, 200.0, 30.0)
testV.string = "Hello World!"
testV.textContainerInset = NSZeroSize
testV.textContainer!.lineFragmentPadding = 0
self.addSubview(testV)
對我顯示的文字相當於:
var testL = CATextLayer()
testL.backgroundColor = NSColor(calibratedRed: 0.73, green: 0.84, blue: 0.89, alpha: 1).CGColor
testL.bounds = CGRectMake(0.0, 0.0, 200.0, 30.0)
testL.position = CGPointMake(100.0, 100.0)
testL.string = "Hello World!"
testL.fontSize = 14
testL.foregroundColor = NSColor.blackColor().CGColor
self.layer!.addSublayer(testL)
你有,你會如何繪製字符串,它不顯示效果不佳的例子 - 我的嘗試沒有很好地呈現。 – Sam 2015-02-12 16:43:56
@Sam確保將'layer.contentsScale'設置爲'[UIScreen mainScreen] .scale'。 – 2015-02-14 01:54:58
美麗。這工作完美。我一直將layer.backgroundColor設置爲完全不透明的顏色,但在繪製文本之前我沒有填充該顏色。一旦我做到了,文字就完美呈現了。謝謝! – Bryan 2016-03-18 04:52:10