首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >cocoa :如何在cocoa中使用drawlabel:inRect方法向NSTabViewItem添加图标?

cocoa :如何在cocoa中使用drawlabel:inRect方法向NSTabViewItem添加图标?
EN

Stack Overflow用户
提问于 2012-07-17 18:50:04
回答 3查看 1.1K关注 0票数 1

我想在NSTabViewItem中添加一个带有文本的图标。

请帮我看一下drawLabel:inRect:方法的代码。

代码语言:javascript
复制
- (id)initWithCoder:(NSCoder *)decoder
{
[super initWithCoder:decoder];

tabCell = [[NSBrowserCell alloc] initImageCell:[NSImage 
imageNamed:@"xyz"]];

[tabCell setLeaf:YES];
[tabCell setFont:[[self tabView] font]];
[tabCell setStringValue: [self label]];

return self;
}

- (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect
{
{ //  modify the rect a tad so the cell draws properly..
    tabRect.origin.y += 2;
    tabRect.size.width += 16;
}

[tabCell drawWithFrame:tabRect inView:[self tabView]];
}


- (NSSize)sizeOfLabel:(BOOL)shouldTruncateLabel
{
NSSize superSize = [super sizeOfLabel:shouldTruncateLabel];
NSImage *icon = [tabCell image];

superSize.width += [icon size].width-4;

return superSize;
}

我可以添加一个图标到NSTabViewItem,但图标是从标签出来的,因为它的大尺寸。如何保持图标的大小以保留在TabViewItem

EN

回答 3

Stack Overflow用户

发布于 2015-10-29 18:31:49

不确定,如果你的问题得到解决,我有类似的用例,我在其中使用drawLabel和附加图像,

参考代码片段,

代码语言:javascript
复制
- (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect{


    NSImage *pImage = [self getImage];

    [[NSGraphicsContext currentContext] saveGraphicsState];
    NSAffineTransform* xform = [NSAffineTransform transform];
    [xform translateXBy:0.0 yBy: tabRect.size.height];
    [xform scaleXBy:1.0 yBy:-1.0];
    [xform concat]; 


    if(pImage){
        [pImage drawInRect:NSMakeRect(tabRect.origin.x-8,-6,16, 16)fromRect:NSZeroRect
                 operation:NSCompositeSourceOver
                  fraction:opacity];
    }
     [[NSGraphicsContext currentContext] restoreGraphicsState];
    [super drawLabel:shouldTruncateLabel inRect:tabRect];
    NSLog(@" Inside drawRect text (%@)",[self labeltitle]);

}
票数 1
EN

Stack Overflow用户

发布于 2018-10-02 03:24:24

这段代码适用于我(Swift 3):

代码语言:javascript
复制
import Foundation
import Cocoa

class MyTabViewItem : NSTabViewItem
{
    let iconWidth:CGFloat = 16

    override func sizeOfLabel(_ shouldTruncateLabel: Bool) -> NSSize
    {
        var superSize: NSSize = super.sizeOfLabel(shouldTruncateLabel)
        superSize.width += iconWidth
        return superSize
    }

    override func drawLabel(_ shouldTruncateLabel: Bool, in tabRect: NSRect)
    {
        let icon: NSImage? = NSImage(named:"Eye")
        if icon != nil
        {
            let opacity:CGFloat = 0.5
            icon?.draw(in: NSMakeRect(tabRect.origin.x + tabRect.width - iconWidth + 4, 8, iconWidth, iconWidth), from: NSZeroRect, operation: NSCompositeSourceOver, fraction: opacity)

        }
        super.drawLabel(shouldTruncateLabel, in: tabRect)  
    }

}

如果没有完全测试,如果iconWidth发生变化,您可能需要更改一些常量。

票数 0
EN

Stack Overflow用户

发布于 2019-02-04 16:15:51

基于Amitg2k12的示例,并添加了一些内容:

代码语言:javascript
复制
- (id)initWithCoder:(NSCoder *)decoder {
    self = [super initWithCoder:decoder];

    if (self) {
        [self setToolTip:[self label]];
        [self setLabel:@" "];
    }

    return self;
}

- (NSSize)sizeOfLabel:(BOOL)computeMin {
    return NSMakeSize(16, 18);
}

- (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect {
    NSImage *image = [self image];

    NSRect destRect = NSMakeRect(tabRect.origin.x, tabRect.origin.y + 2, 16, 16);

    [[NSGraphicsContext currentContext] saveGraphicsState];
    NSAffineTransform *affineTransform = [NSAffineTransform transform];
    [affineTransform translateXBy:NSMaxX(destRect) yBy:NSMinY(destRect)];
    [affineTransform scaleXBy:1.0 yBy:-1.0];
    [affineTransform concat];

    if(image) {
        [image drawInRect:NSMakeRect(-NSWidth(destRect), -NSHeight(destRect), 16, 16) fromRect:NSZeroRect
                operation:NSCompositeSourceOver
                 fraction:1.0f];
    }

    [[NSGraphicsContext currentContext] restoreGraphicsState];
    [super drawLabel:shouldTruncateLabel inRect:tabRect];
}

@end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11520961

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档