gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:14:19 25 4
gpt4 key购买 nike

我想向 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com