objective-c - cocoa : how to add an icon to NSTabViewItem using the method drawlabel:inRect in cocoa?

标签 objective-c cocoa icons image-resizing nstabview

我想向 NSTabViewItem 添加一个带有一些文本的图标。

请帮我解决 drawLabel:inRect: 方法中的代码。

- (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 内?

最佳答案

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

引用代码片段,

- (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]);

}

关于objective-c - cocoa : how to add an icon to NSTabViewItem using the method drawlabel:inRect in cocoa?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11520961/

相关文章:

iphone - SQLite 中的 BLOB?

html - stringByReplacingOccurrencesOfString 无法正常运行

iOS UIView - 4 个角各有不同的圆角半径

java - jmenubar 下方的 Eclipse 图标

.net - 如何设置任务栏分组图标

html - Favicon 不适用于 Edge

objective-c - 单击按钮时重置/重新初始化/重新启动 UIViewController View 的最简单(正确)方法是什么?

macos - 我的 NSTableView 表中的选择没有突出显示

objective-c - 监听来自其他 Cocoa 应用程序的事件

iphone - 使用 NSURLConnection 缓存和查询 HTTP header 的问题