问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/4676555/nstextfield-like-safari-address-bar