1

Dock でアプリケーションにバッジを追加するのは簡単です。 を呼び出すだけ[NSDockTile setBadgeLabel]です。しかし、一般的な NSView にバッジを追加したいと考えています。これを行う方法はありますか?または、それができない場合、システム UI の残りの部分と一致するように、どのように描画しますか?

4

2 に答える 2

4

バッジを描画する組み込みメソッドはないNSViewので、自分で描画する必要があります。バッジを描画するコードは次のとおりです。

- (void)drawBadgeImageWithText:(NSString*)text atPoint:(NSPoint)point
{
    NSSize badgeSize = [self badgeSizeForString:text];
    NSRect badgeRect = NSMakeRect(point.x - badgeSize.width, point.y - badgeSize.height, badgeSize.width, badgeSize.height);

    [NSGraphicsContext saveGraphicsState];

    // Set a shadow
    NSShadow* shadow = [[NSShadow alloc] init];
    shadow.shadowColor = [[NSColor blackColor] colorWithAlphaComponent:0.4];
    shadow.shadowBlurRadius = 1;
    shadow.shadowOffset = NSMakeSize(0, -1);
    [shadow set];

    // Draw white border
    NSBezierPath* path = [NSBezierPath bezierPathWithRoundedRect:badgeRect xRadius:badgeSize.height / 2.0 yRadius:badgeSize.height / 2.0];
    [[NSColor whiteColor] setFill];
    [path fill];

    [NSGraphicsContext restoreGraphicsState];

    // Fill the background with red gradient 
    badgeRect = NSInflateRect(badgeRect, -1.5, -1.5);
    path = [NSBezierPath bezierPathWithRoundedRect:badgeRect xRadius:badgeSize.height / 2.0 yRadius:badgeSize.height / 2.0];
    [[NSColor colorWithCalibratedRed:192.0 / 255.0 green:0.0 blue:0.0 alpha:1.0] setFill];
    [path fill];

    NSGradient* gradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedRed:241.0 / 255.0 green:113.0 / 255.0 blue:115.0 / 255.0 alpha:1.0]
                                                         endingColor:[NSColor colorWithCalibratedRed:192.0 / 255.0 green:0.0 blue:0.0 alpha:1.0]];
    [gradient drawInBezierPath:path angle:-90.0];

    // Draw the text
    NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setAlignment:NSCenterTextAlignment];

    NSDictionary* textAttributes = @{NSForegroundColorAttributeName:[NSColor whiteColor], NSParagraphStyleAttributeName:paragraphStyle};

    [text drawInRect:badgeRect withAttributes:textAttributes];
}

- (NSSize)badgeSizeForString:(NSString*)string
{
    NSDictionary* attributes = [NSDictionary dictionaryWithObject:[NSFont systemFontOfSize:13] forKey: NSFontAttributeName];
    NSSize size = [string sizeWithAttributes:attributes];

    // Paddings
    size.height += 2.0;
    size.width += 12.0;

    return size;
}
于 2013-11-07T08:24:06.270 に答える