NSTextField text blur solution canDrawSubviewsIntoLayer

Among developing pocketed input process macOS,I have been plagued by a strange question - text blurred。


No matter how I debug,Pocketed candidate text input method can not achieve the original text as clear and sharp。After a big ask God,The resulting experience is to see live a long time - upstreamSub-pixel rendering bug。

In many platforms have similar problems,macOS underlying rendering there such a mistake,Once your layout there was not an integer or decimal point,It will lead to blurred text rendering。

So how to solve it? As long as the position and size of the "positive" on it,Do not use direct Int(xxx) To convert your value,use roundf() ,but,For Swift is,We have a more convenient way,For example, this:

rounded() The method that we often say "rounding",If you convert the data type directly into plastic,Then the content directly after the decimal point will be discarded,Even if it is 0.99999 ,Will become 0 ,Obviously this is not what we want,If you use this function to convert,So you still get CGFloat ,But the value of the original has become a rounded "integer"。

Really a lot better,Not so blurred,But if you look carefully,Always feel where there are not the same。

Actually,The bug in the latest macOS 10.14 Mojave has been repaired。


Still others say,Because you use layer,This will lead to under performance,If the wantsLayer shut down,Or to a non-transparent background,It would be much better。But after all, is not an option,If you have to clear it? In short,Upon inquiry I found this method canDrawSubviewsIntoLayer

Apple's official documentation it is so described:

When the value of this property is true ,layer-- any child of any view that is implicitly created,By setting those you do not wantsLayer Property true layer created,The draw itself will be on the layer of the current view。In other words the child view no longer has its own layer。in contrast,They draw their content in view of the parent layer。All views are affected by the use of draw(_:) Method to draw content。They no longer use updateLayer() The method of updating the content layer,Even if wantsUpdateLayer Property to true

Use this property to a layer based on flattening and child views layer Inheritance。Flat layer integration reduces the number of layer succession you try (and possibly also reduce memory usage)。Reducing the number of layer can be more efficient in the case of large and child views content obvious changes。For example,,Time with which the composition decreased inherited flattened view。Do not flatten the view inherit if you are ready to add animation, then the child view。

When changing the value of the property,You must have a current view of the object layer。The default value of the property false

I translated the English,Actually,Photoshop can be understood to be in the "Merge Layers",If you do not do much to change the view portfolio,Then you can combine them to draw,What does not need to manage the transparency and gradients,Directly to the final results are plotted on the same layer。Greatly reducing the cost of rendering,This text no longer blurred,Become sharp。

Original article written by Gerber drop-off:R0uter's Blog » NSTextField text blur solution canDrawSubviewsIntoLayer

Reproduced Please keep the source and description link:https://www.logcg.com/archives/3083.html

About the Author


The non-declaration,I have written articles are original,Reproduced, please indicate the link on this page and my name。

Leave a Reply

Your email address will not be published. Required fields are marked *