Faking Subpixel Antialiasing on Text with Core Animation

前端 未结 5 1147
半阙折子戏
半阙折子戏 2020-12-08 02:58

For anyone working on a project with Core Animation layer-backed views, it\'s unfortunately obvious that subpixel antialiasing (text smoothing) is disabled for text not rend

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-08 03:21

    I'm not really sure I have grasped the question, so this is a real punt.

    But I noticed LaC's answer depends on the pixels being written on top of each other in the normal way. In the manner you would in Photoshop call the "Normal Blend Mode".

    You can merge two layers in lots of other ways including "Multiply":

    enter image description here

    This individually multiplies the R, G, and B of each pixel. So if you had the text on a white background, you could get a further-back layer to show through by setting the top layer to "Multiply" which causes both layers to burn into each other like so.

    This should work for your use case too:

    enter image description here

    Both text layers in this shot have an opaque white background, but the one on the right has the blend mode set to "Multiply".

    With "Multiply":

    • white pixels in the top layer multiply lower layers by 1, leaving them unchanged
    • black pixels in the top layer multiply lower layers by 0, reducing them to black
    • shades in between darken the lower layers proportionally

    In other words, the same result that you would have got just laying the text directly onto the background.

    I haven't subpixel antialiased the text in this screenshot but it would work equally well with differing R, G, and B values.


    I'm not remotely familiar with Mac's API, but according to this link, Core Animation does have this capability, which you get by writing:

    myLayer.compositingFilter = [CIFilter filterWithName:@"CIMultiplyBlendMode"];
    

    or

    myLayer.compositingFilter = [CIFilter filterWithName:@"CIMultiplyCompositing"];
    

    (I have no idea what "Blend Mode" vs "Compositing" relates to in Mac parlance so you'll have to try both!)


    EDIT: I'm not sure you ever specified your text was Black. If it's White, you can use a white-on-black layer set to a Blend Mode of "Screen".

提交回复
热议问题