Objective c 如何在NSView(Mac OS X)上显示徽章
要在dock中向应用程序添加徽章很简单,只需调用Objective c 如何在NSView(Mac OS X)上显示徽章,objective-c,macos,cocoa,badge,nsdocktile,Objective C,Macos,Cocoa,Badge,Nsdocktile,要在dock中向应用程序添加徽章很简单,只需调用[NSDockTile setBadgeLabel]。但我想在通用NSView中添加一个徽章。有没有办法做到这一点?或者,如果做不到这一点,您如何绘制一个徽章,使其与系统UI的其余部分保持一致?没有内置的方法在NSView上绘制徽章,因此您应该自己绘制徽章。 以下是我绘制徽章的代码: - (void)drawBadgeImageWithText:(NSString*)text atPoint:(NSPoint)point { NSSize
[NSDockTile setBadgeLabel]
。但我想在通用NSView中添加一个徽章。有没有办法做到这一点?或者,如果做不到这一点,您如何绘制一个徽章,使其与系统UI的其余部分保持一致?没有内置的方法在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;
}
没有内置的方法可以在
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;
}
我把斯威夫特的回答移植到了斯威夫特3号;在这里,以防它节省了其他人一些时间
func badgeSizeForString(string:String) -> NSSize {
var size = string.size(withAttributes: [NSFontAttributeName: NSFont.systemFont(ofSize: 13.0)])
// Paddings
size.height += 2.0
size.width += 12.0
return size
}
func drawBadgeImageWithText(text:NSString, atPoint point: NSPoint) -> Void {
let badgeSize = badgeSizeForString(string: text as String)
var badgeRect = NSMakeRect(point.x - badgeSize.width,
point.y - badgeSize.height,
badgeSize.width,
badgeSize.height)
NSGraphicsContext.saveGraphicsState()
// Set a shadow
let shadow = NSShadow()
shadow.shadowColor = NSColor.black.withAlphaComponent(0.4)
shadow.shadowBlurRadius = 1;
shadow.shadowOffset = NSMakeSize(0, -1);
shadow.set()
// Draw white border
var path = NSBezierPath(roundedRect: badgeRect,
xRadius:badgeSize.height / 2.0,
yRadius:badgeSize.height / 2.0)
NSColor.white.setFill()
path.fill()
NSGraphicsContext.restoreGraphicsState()
// Fill the background with red gradient
badgeRect = NSInsetRect(badgeRect, -1.5, -1.5)
path = NSBezierPath(roundedRect: badgeRect,
xRadius:badgeSize.height / 2.0,
yRadius:badgeSize.height / 2.0)
NSColor(calibratedRed:192.0 / 255.0,
green:0.0,
blue:0.0,
alpha:1.0).setFill()
path.fill()
let gradient = NSGradient(starting: NSColor(calibratedRed:241.0 / 255.0,
green:113.0 / 255.0,
blue:115.0 / 255.0,
alpha:1.0),
ending: NSColor(calibratedRed:192.0 / 255.0,
green:0.0,
blue:0.0,
alpha:1.0))
gradient?.draw(in: path, angle:-90.0)
// Draw the text
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let textAttributes = [ NSForegroundColorAttributeName: NSColor.white, NSParagraphStyleAttributeName: paragraphStyle ]
text.draw(in: badgeRect, withAttributes: textAttributes)
}
我把斯威夫特的回答移植到了斯威夫特3号;在这里,以防它节省了其他人一些时间
func badgeSizeForString(string:String) -> NSSize {
var size = string.size(withAttributes: [NSFontAttributeName: NSFont.systemFont(ofSize: 13.0)])
// Paddings
size.height += 2.0
size.width += 12.0
return size
}
func drawBadgeImageWithText(text:NSString, atPoint point: NSPoint) -> Void {
let badgeSize = badgeSizeForString(string: text as String)
var badgeRect = NSMakeRect(point.x - badgeSize.width,
point.y - badgeSize.height,
badgeSize.width,
badgeSize.height)
NSGraphicsContext.saveGraphicsState()
// Set a shadow
let shadow = NSShadow()
shadow.shadowColor = NSColor.black.withAlphaComponent(0.4)
shadow.shadowBlurRadius = 1;
shadow.shadowOffset = NSMakeSize(0, -1);
shadow.set()
// Draw white border
var path = NSBezierPath(roundedRect: badgeRect,
xRadius:badgeSize.height / 2.0,
yRadius:badgeSize.height / 2.0)
NSColor.white.setFill()
path.fill()
NSGraphicsContext.restoreGraphicsState()
// Fill the background with red gradient
badgeRect = NSInsetRect(badgeRect, -1.5, -1.5)
path = NSBezierPath(roundedRect: badgeRect,
xRadius:badgeSize.height / 2.0,
yRadius:badgeSize.height / 2.0)
NSColor(calibratedRed:192.0 / 255.0,
green:0.0,
blue:0.0,
alpha:1.0).setFill()
path.fill()
let gradient = NSGradient(starting: NSColor(calibratedRed:241.0 / 255.0,
green:113.0 / 255.0,
blue:115.0 / 255.0,
alpha:1.0),
ending: NSColor(calibratedRed:192.0 / 255.0,
green:0.0,
blue:0.0,
alpha:1.0))
gradient?.draw(in: path, angle:-90.0)
// Draw the text
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let textAttributes = [ NSForegroundColorAttributeName: NSColor.white, NSParagraphStyleAttributeName: paragraphStyle ]
text.draw(in: badgeRect, withAttributes: textAttributes)
}
很好,谢谢。只需将InflateRect更改为NSInflateRect即可编译代码。你是说NSInsetRect?效果很好,谢谢。只需将InflateRect更改为NSInflateRect即可编译代码。您是指NSInsetRect吗?对于未发布到mac应用商店的用户,请使用[NSImage\u徽标ForCount:1]进行图像处理。对于未发布到mac应用商店的用户,请使用[NSImage\u徽标ForCount:1]进行图像处理。