NSTextField like safari address bar

后端 未结 1 1560
渐次进展
渐次进展 2021-01-04 22:30

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条回答
  • 2021-01-04 23:24

    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

    0 讨论(0)
提交回复
热议问题