2012-07-17 51 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]); 

}