NSString *文本到NSString *图标?

发布于 2024-11-14 01:54:37 字数 1856 浏览 3 评论 0原文

我正在制作一个应用程序,它是一个独立的菜单项,代码的基础是我在网站上找到的示例代码。示例代码使用数字作为菜单图标,但我想将其更改为图像。

我希望它像其他应用程序一样,在未单击时显示 icon.png ,在单击时显示 icon-active.png

当前的代码是这样的:

- (void)drawRect:(NSRect)rect {
// Draw background if appropriate.
if (clicked) {
    [[NSColor selectedMenuItemColor] set];
    NSRectFill(rect);
}

// Draw some text, just to show how it's done.
NSString *text = @"3"; // whatever you want

NSColor *textColor = [NSColor controlTextColor];
if (clicked) {
    textColor = [NSColor selectedMenuItemTextColor];
}

NSFont *msgFont = [NSFont menuBarFontOfSize:15.0];
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
[paraStyle setParagraphStyle:[NSParagraphStyle defaultParagraphStyle]];
[paraStyle setAlignment:NSCenterTextAlignment];
[paraStyle setLineBreakMode:NSLineBreakByTruncatingTail];
NSMutableDictionary *msgAttrs = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                 msgFont, NSFontAttributeName,
                                 textColor, NSForegroundColorAttributeName,
                                 paraStyle, NSParagraphStyleAttributeName,
                                 nil];
[paraStyle release];

NSSize msgSize = [text sizeWithAttributes:msgAttrs];
NSRect msgRect = NSMakeRect(0, 0, msgSize.width, msgSize.height);
msgRect.origin.x = ([self frame].size.width - msgSize.width) / 2.0;
msgRect.origin.y = ([self frame].size.height - msgSize.height) / 2.0;

[text drawInRect:msgRect withAttributes:msgAttrs];
}

另外,我找到了一篇描述如何执行此操作的方法的帖子,但它对我不起作用。其网址是这样的: http://mattgemmell.com/2008/03/04/using-maattachedwindow-with-an-nsstatusitem/comment-page-1#comment-46501

谢谢!

I am making an app that is a standalone menu item and the basis for the code is sample code I found on a website. The sample code uses a number as the menu icon, but I want to change it to an image.

I want it to be like other apps where it shows icon.png when not clicked and icon-active.png when clicked.

The current code is this:

- (void)drawRect:(NSRect)rect {
// Draw background if appropriate.
if (clicked) {
    [[NSColor selectedMenuItemColor] set];
    NSRectFill(rect);
}

// Draw some text, just to show how it's done.
NSString *text = @"3"; // whatever you want

NSColor *textColor = [NSColor controlTextColor];
if (clicked) {
    textColor = [NSColor selectedMenuItemTextColor];
}

NSFont *msgFont = [NSFont menuBarFontOfSize:15.0];
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
[paraStyle setParagraphStyle:[NSParagraphStyle defaultParagraphStyle]];
[paraStyle setAlignment:NSCenterTextAlignment];
[paraStyle setLineBreakMode:NSLineBreakByTruncatingTail];
NSMutableDictionary *msgAttrs = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                 msgFont, NSFontAttributeName,
                                 textColor, NSForegroundColorAttributeName,
                                 paraStyle, NSParagraphStyleAttributeName,
                                 nil];
[paraStyle release];

NSSize msgSize = [text sizeWithAttributes:msgAttrs];
NSRect msgRect = NSMakeRect(0, 0, msgSize.width, msgSize.height);
msgRect.origin.x = ([self frame].size.width - msgSize.width) / 2.0;
msgRect.origin.y = ([self frame].size.height - msgSize.height) / 2.0;

[text drawInRect:msgRect withAttributes:msgAttrs];
}

Also, I found a post describing a method on how to do this, but it did not work for me. The url to that is this: http://mattgemmell.com/2008/03/04/using-maattachedwindow-with-an-nsstatusitem/comment-page-1#comment-46501.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凑诗 2024-11-21 01:54:37

使用 NSImage 并将其绘制在需要的位置。例如:

NSString *name = clicked? @"icon-active" : @"icon";
NSImage *image = [NSImage imageNamed:name];
NSPoint p = [self bounds].origin;
[image drawAtPoint:p fromRect:NSZeroRect
         operation:NSCompositeSourceOver fraction:1.0];

Use an NSImage and draw it where desired. For example:

NSString *name = clicked? @"icon-active" : @"icon";
NSImage *image = [NSImage imageNamed:name];
NSPoint p = [self bounds].origin;
[image drawAtPoint:p fromRect:NSZeroRect
         operation:NSCompositeSourceOver fraction:1.0];
意中人 2024-11-21 01:54:37

如果这是针对状态项的,并且您只需要一个没有编程绘图的图标,请放下视图并设置状态项的 imagealternateImage。前者是状态项通常使用的;当用户打开菜单时,状态项会切换到备用图像(如果有)。

If this is for a status item and you just want an icon with no programmatic drawing, drop the view and set the status item's image and alternateImage. The former is what the status item uses normally; the status item switches to the alternate image (if it has one) when the user opens its menu.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文