【问题标题】:cocoa : how to add an icon to NSTabViewItem using the method drawlabel:inRect in cocoa?cocoa:如何使用可可中的 drawlabel:inRect 方法向 NSTabViewItem 添加图标?
【发布时间】:2012-07-17 10:50:04
【问题描述】:

我想在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 内?

【问题讨论】:

    标签: objective-c cocoa icons image-resizing nstabview


    【解决方案1】:

    不确定,如果您的问题得到解决,我有类似的用例,我正在使用 drawLabel 并在其中附加图像,

    参考代码sn-p,

    - (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]);
    
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        基于 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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-08-22
          • 2021-08-23
          • 1970-01-01
          • 2012-12-19
          • 1970-01-01
          • 2012-02-29
          相关资源
          最近更新 更多