2012-07-17 49 views
1

我想用一些文字将图标添加到NSTabViewItemcocoa:如何使用方法drawlabel:inRect in cocoa将图标添加到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

回答

0

不知道,如果你的问题得到了解决,我也有类似的使用情况,以及我使用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]); 

}