1

我想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?

4

3 回答 3

1

不确定,如果您的问题得到解决,我有类似的用例,我正在使用 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]);

}
于 2015-10-29T10:31:49.130 回答
0

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


- (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
于 2019-02-04T08:15:50.573 回答
0

此代码适用于我(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)  
    }

}

未经过全面测试,如果 iconWidth 发生变化,您可能必须更改一些常量。

于 2018-10-01T19:24:23.837 回答