我想在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]);
}
此代码适用于我(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发生变化,您可能需要更改一些常量。
基于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