views:

51

answers:

2

Hi,

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

This is my code so far:

NSTextfield *textfield = [[NSTextField alloc] initWithFrame:NSMakeRect(5,5,150,20)];
[texField setStringValue:@"some text here"];
[textField setEditable:NO];
[textField setSelectable:NO];
[textField setBordered:NO]
[textField setDrawsBackground:NO]

I've traced the problem down to the setDrawsBackground line. I've also tried using [textField setBackgroundColor:[NSColor clearColor] as well, but no luck.

By the way, I've adding to a textField to the subview of a view that is a subview of a scrollview. I've also playing with isOpaque on all the view levels, but no luck there again.

Any help is greatly appreciated.

A: 

If you have no background (including clear) and your text is a subview of any layer-backed superview (you've turned on "wants layer" in code or in IB to allow animations/transitions), you'll get blurry text. You have to choose either no layer backed view or a label with a solid background color.

Joshua Nozzi
Thanks for the response. I originally thought of that, but I was hoping to find a better solution. I tried setting wantsLayer to NO on the views, but still no luck.
IEb
I've never seen this problem caused by anything other than -wantsLayers being YES. Are you sure you've climbed the entire view hierarchy and made sure it was all turned off? How did you do this, in IB or in code? If in code, where/how?
Joshua Nozzi
I'm doing this all through code. I have the following view setup:-NSScrollView which contains NSView 1.-NSView 1 contains NSView 2-NSView 2 is where I have the NSTextField inAre you saying to set wantsLayer to YES on all view levels?
IEb
I just tried setting wantsLayer:NO on all three levels, same results. Now my scrollview is also contained in a split view, should it also be set on that?
IEb
Are you sure that object is there only once? It looks to me like there are multiple copies stacked atop each other. Just an idea....
Warren Burton
Warren Burton: That was my thought as well—it looks double-struck, not blurred. IEb: Have you checked whether your create-and-add-a-text-field code is being run only once or multiple times? I suspect it's the latter: You're calling it more than once and so adding a two or more text fields in a stack.
Peter Hosey
You guys got it. It was the fact that the text was being redrawn constantly. Thanks for the help!
IEb
+1  A: 

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

IEb