Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

cocoa - How to implement HUD-style window like Address Book's "Show in Large Type"

Several apps, including the built-in Address Book use a HUD window that is semi-transparent, with large shadowed text. I'd like to implement a similar window in my Cocoa Mac app.

screenshot

Is there a free implementation of this kind of window somewhere?

If not, what is the best way to implement it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's a sample project that shows how to do it:

http://github.com/NSGod/BlackBorderlessWindow

Basically, you need to create a borderless NSWindow subclass. The easiest way to do this is to set your window size and arrangement in the nib file, and then set its class to be your custom subclass. So while it will still look like a normal window in Interface Builder, at runtime it will appear as you need it to.

@implementation MDBorderlessWindow

- (id)initWithContentRect:(NSRect)contentRect
            styleMask:(NSUInteger)windowStyle
              backing:(NSBackingStoreType)bufferingType
                defer:(BOOL)deferCreation {

    if (self = [super initWithContentRect:contentRect
                            styleMask:NSBorderlessWindowMask
                              backing:NSBackingStoreBuffered defer:deferCreation]) {
            [self setAlphaValue:0.75];
            [self setOpaque:NO];
            [self setExcludedFromWindowsMenu:NO];
    }
    return self;
}

The alpha value will make the window semi-transparent.

Also, you can create a custom NSView subclass that will draw a round rectangle:

@implementation MDBlackTransparentView

- (id)initWithFrame:(NSRect)frame {
    if (self = [super initWithFrame:frame]) {

    }
    return self;
}

- (void)drawRect:(NSRect)frame {
    NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:frame
                                    xRadius:6.0 yRadius:6.0];
    [[NSColor blackColor] set];
    [path fill];
}

@end

Like with the window, you simply set the class of the window's contentView to be your custom NSView subclass. (Use outline view mode and click the disclosure triangle to show the nested NSView inside the icon of the window in the nib file). Again, while the view will look ordinary in Interface Builder, it will look okay at runtime.

Then just place an NSTextField on top of view and set the text accordingly.

Note that, in general, borderless windows aren't easy to work with (for example, if you want to be able to drag the window around, you'll need to add that functionality back yourself). Apple has some sample code on how to allow dragging, for instance.

alt text


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...