Creating a label using NSTextField is blurry

后端 未结 8 1248
后悔当初
后悔当初 2021-01-06 03:05

I\'m trying to create a label programmatically using NSTextField, but it comes out blurry: screenshot

This is my code so far:

NSTextfield *textfield          


        
8条回答
  •  盖世英雄少女心
    2021-01-06 03:16

    Make sure you're setting the frame of your NSTextField to something with all integer values.

    Use roundf() if necessary.

    I was getting a blurry NSTextField, and neither adding a solid background nor removing Core Animation layers from my view hierarchy were options for me. I noticed I was setting the frame of this text field to something with a Y value of 4.5, so the following changes fixed the issue for me:

    Blurry label:

    _label.frame = NSOffsetRect(_labelFrame,
                                -0.5 * (someRect.size.width + someConstant),
                                0.0);
    

    No blur:

    _label.frame = NSOffsetRect(_labelFrame,
                                roundf(-0.5 * (someRect.size.width + someConstant)),
                                0.0);
    

    (In the above examples, _labelFrame and someRect are NSRects, and someConstant is a CGFloat. As you can see, the calculation I was doing in the second line of the first example was passing a non-integer value to NSOffsetRect).

提交回复
热议问题