NSTextField like safari address bar

旧时模样 提交于 2019-11-30 03:30:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!