Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTextField like safari address bar

Whats the best way to go about building an address field like the one in safari?

Needs to have editable text, and determinate progress indicator background.

like image 799
Arlen Anderson Avatar asked Jan 13 '11 03:01

Arlen Anderson


1 Answers

You could just subclass NSTextField and override the -drawRect: method to "fill" the appropriate percentage of the entire width with some color or gradient (or whatever) for the progress. If I'm understanding your question right.

-(void)drawRect:(NSRect)dirtyRect {
    CGFloat progress = 0.33;

    NSRect progressRect = [self bounds];
    progressRect.size.width *= progress;

    [[NSColor colorWithCalibratedRed:0.0 green:0.0 blue:1.0 alpha:0.4] set];
    NSRectFillUsingOperation(progressRect, NSCompositeSourceOver);

    [super drawRect:dirtyRect];
}

Obviously "progress" would come from a property you declare and be updated according to the model. You'd need to make sure setDrawsBackground: is turned off, or the background is set to [NSColor clearColor]; in order for your custom drawing to be seen.

This is a shot from the code above.

Tested Theory

like image 97
d11wtq Avatar answered Sep 18 '22 15:09

d11wtq