Text smoothing in CALayer with an opaque background

As others did, I'm trying to get decent text on a CALayer background. The most suitable stream I found is Faking Subpixel Antialiasing on Text with Core Animation .

At the beginning of this post, he stated that "Now, for people who can set opaque backgrounds for their text (either by calling setBackgroundColor: or an equivalent parameter in Interface Builder), this question is missing too big a problem." However, when I set the background of the text field to IB and draw it and do the same with my cell, I still get the same problem (no smoothing when using layers). They relate to NSBox, which also uses its own background.

Any idea on what I should do, but I don't? Thanks

+5
source share
2 answers

Not sure if this will solve your problem, but when you do your own drawing in a layer, you should use CGContextFillRect () with an opaque color and then draw a line on top of that rectangle to get sub-pixel antialialization.

Setting layer.backgroundColor is not good enough, perhaps because layer.backgroundColor can be changed without re-drawing the contents of the layer.

+3
source

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)
0

All Articles