如果您只是想在不透明的背景上看到清晰的文字并使用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