我想在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中
发布于 2015-10-29 18:31:49
不确定,如果你的问题得到解决,我有类似的用例,我在其中使用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]);
}发布于 2018-10-02 03:24:24
这段代码适用于我(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发生变化,您可能需要更改一些常量。
发布于 2019-02-04 16:15:51
基于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];
}
@endhttps://stackoverflow.com/questions/11520961
复制相似问题