CATextLayer subpixel antialiasing

限于喜欢 提交于 2019-12-05 21:57:21

Is there any way that you can post a piece of sample code? I quickly mocked up an NSWindow, added an NSImageView, added a background-less NSTextField with setWantsLayer: set to YES. In my applicationDidFinishLaunching: I set a new rect on the NSTextField's Animator frame, but I didn't see any pixelation.

The problem is with positioning of the text layer. Let's presume you use left alignment. The text will look good if x and y coordinates of the layer's frame origin are rounded numbers. For example:

CGFloat x = 10.6;
CGFloat y = 10.3;

textLayer.frame = CGRectMake(x, y, width, height); // the text will be blur.
textLayer.frame = CGRectMake(round(x), round(y), width, height); // the text will not be blur.

So, the first problem may be that the coordinates you assign to the layer's frame are not rounded. The tricky part is that the edge of the layer may still be not aligned with pixels even if you think you passed rounded coordinates. This may happen if the anchorPoint of your layer is (0.5, 0.5), which is the default value. If you set:

textLayer.position = CGPointMake(10.0, 10.0);

you may think it should draw the text sharp. However, position point is in the center of the layer here, and depending on the layer's width and height the left and top edge's coordinates may be fractional numbers.

If you want to make a quick test do this.

  1. Use textLayer.frame = frame instead of using position and anchor point, it will assign the coordinates directly to the frame.
  2. Make sure the numbers you use in the frame are rounded.
  3. Do not mess with rendering mechanism, remove the code that changes shouldRasterize, antialiasing, etc.

If this makes the text sharp, you can start using the anchor point and position and to see how the result changes.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!