Creating a label using NSTextField is blurry

后端 未结 8 1163
后悔当初
后悔当初 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).

    0 讨论(0)
  • 2021-01-06 03:25

    I had the same problem, but I have solved it by:

    textfield.canDrawSubviewsIntoLayer = true
    
    0 讨论(0)
  • 2021-01-06 03:26

    If you created it via an XIB and it is blurry, I found the fix:

    [textField setStringValue:@""];
    

    If I comment this out it goes blurry; if put back it's crystal clear.

    0 讨论(0)
  • 2021-01-06 03:27

    Simply, add

    CanDrawConcurrently = true 
    

    property from InterfaceBuilder

    0 讨论(0)
  • 2021-01-06 03:28

    Out of the box, but worth to mention. I have wasted by entire day debugging the issue. I am using a non-apple external monitor and this is where the issue is identified. Once i open the app in Mac book pro, it is perfectly fine. So, the Samsung monitor which i am using might be non-retina.

    0 讨论(0)
  • 2021-01-06 03:34

    Since this was my first time subclassing NSView, I had put the above code in the drawRect method instead of the initWithFrame method. I did this because I was following one of the sample applications from Apple's Dev site.

    This was also causing my CPU usage to spike when I was scrolling

    0 讨论(0)
提交回复
热议问题