1

I want to add an icon to the NSTabViewItem with some text.

Please help me with the code in drawLabel:inRect: method.

- (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;
}

I am able to add an icon to the NSTabViewItem but the icon is coming out of the tab because of its big size. How can I maintain the size of icon to stay within the TabViewItem?

durron597
  • 31,968
  • 17
  • 99
  • 158
user1295948
  • 303
  • 1
  • 5
  • 15

3 Answers3

1

Not sure, if your problem got resolve, i had similar use case,and i am using drawLabel and appending image in that,

refer the code snippet,

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

}
Amitg2k12
  • 3,765
  • 10
  • 48
  • 97
0

This code works for me (Swift 3):

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)  
    }

}

Not fully tested, you may have to change some of the constants if the iconWidth changes.

Stiefel
  • 2,677
  • 3
  • 31
  • 42
0

Based on Amitg2k12's example with some additions:


- (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
headkaze
  • 469
  • 4
  • 11