0

There seems to be a bug in NSTextField. When the application launches it all draws correctly. But as soon as I click in the textField the view gets all messed up. To more specific, whenever I type drawRect: gets called but with a smaller rect causing all the problems.

When I select the text it draws correctly again. The only solution is to set the FocusRingType to visible (example: NSFocusRingTypeDefault). But I would like to have it without the ring. Is this possible?

Here is the code I am using:

-(id)initWithFrame:(NSRect)frameRect
{
    self = [super initWithFrame:frameRect];
    if(self)
    {
        // Add a label
        NSTextField *textField = [[NSTextField alloc] initWithFrame:CGRectMake(0, 0, frameRect.size.width, frameRect.size.height)];
        [[textField cell] setPlaceholderString:@"URL or search term..."];
        [textField setTextColor:[NSColor greyColor]];
        [textField setBackgroundColor:[NSColor clearColor]];
        [textField setFont:[NSFont fontWithName:@"Open Sans" size:20]];
        [textField setDrawsBackground:FALSE];
        [textField setBordered:FALSE];
        [textField setFocusRingType:NSFocusRingTypeNone];
        [self addSubview:textField];
    }

    return self;
}

-(void)drawRect:(NSRect)dirtyRect
{
    NSInteger borderWdith =  2;

    // Create the path to the button
    NSBezierPath *aPath = [NSBezierPath bezierPathWithRoundedRect:CGRectMake(borderWdith, borderWdith,
                                                                             dirtyRect.size.width-(borderWdith*2),
                                                                             dirtyRect.size.height-(borderWdith*2))
                                                          xRadius:3 yRadius:3];

    // Fill the button with white
    [[NSColor whiteColor] set];
    [aPath fill];
}

Tricks like setting editable to TRUE/FALSE in drawRect did not work. Also setting to different focusRingTypes in the method failed.

Silicone
  • 643
  • 6
  • 19

1 Answers1

0

Okay, so I managed to sort of fix this drawing problem. In the view init method I set a global CGRect variable which was set to the initial frame.

Each time -(void)drawRect:(CGRect)dirtyRect was called, I overwrote dirtyRect with the global variable.

Hope this helps anyone. I guess this isn't the best solution, but works :)

Silicone
  • 643
  • 6
  • 19