NSTabView Item 在 TabViewItem 中添加图标

发布于 2024-10-26 21:16:45 字数 350 浏览 1 评论 0原文

我对 NSTabView 进行了子类化并添加了 5 个 TabViewItem,现在我想在 NSTabViewItem 中添加一个图标和标题, 任何人都可以建议我如何开始,我没有得到任何文档,除了,

- (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect

这是否意味着,如果我重写此方法,我需要绘制自己的图标和字符串,

为了设置标题,我正在使用以下方法,

[pTabViewItem setLabel:pLabelTitle];

亲切的问候
罗汉

I subclassed NSTabView and added 5 TabViewItem, now i wanted to add an Icon along with the title in the NSTabViewItem,
Can anyone suggest me how to start, i am not getting any documentation except,

- (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect

Does that mean, if i override this method, i need to draw Icon and string my own,

For setting up the title , i am using following method,

[pTabViewItem setLabel:pLabelTitle];

Kind Regards
Rohan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沫雨熙 2024-11-02 21:16:45

没关系,
以下代码对我有用,

- (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect{

    // do we have an image to draw
    NSImage *pImage = [pDelegate imageForCell];

    [[NSGraphicsContext currentContext] saveGraphicsState];
    NSAffineTransform* xform = [NSAffineTransform transform];
    [xform translateXBy:0.0 yBy: tabRect.size.height];
    [xform scaleXBy:1.0 yBy:-1.0];
    [xform concat]; 


    CGFloat x_Offset =0;
    if(pImage){
        [pImage drawInRect:NSMakeRect(tabRect.origin.x-8,-6,16, 16)fromRect:NSZeroRect
                 operation:NSCompositeSourceOver
                  fraction:1.0];
        x_Offset =  16;
    }
     [[NSGraphicsContext currentContext] restoreGraphicsState];

    [super drawLabel:shouldTruncateLabel inRect:tabRect];
}

为什么要进行转换:
图像显示倒置,所以我需要变换,

为什么偏移:
即使在转换之后,我也需要调整位置,使其看起来就在标题之前,

伙计们,我在设置标题时,

附加一个空格,这样标题就不会与图像重叠,我知道这是丑陋的方法,但不能没有任何其他快速方法来做到这一点,如果我自己绘制文本那么我还需要注意截断,

感谢那些查看问题和答案的人
亲切的问候
罗汉

Never Mind,
Following Code works for me,

- (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect{

    // do we have an image to draw
    NSImage *pImage = [pDelegate imageForCell];

    [[NSGraphicsContext currentContext] saveGraphicsState];
    NSAffineTransform* xform = [NSAffineTransform transform];
    [xform translateXBy:0.0 yBy: tabRect.size.height];
    [xform scaleXBy:1.0 yBy:-1.0];
    [xform concat]; 


    CGFloat x_Offset =0;
    if(pImage){
        [pImage drawInRect:NSMakeRect(tabRect.origin.x-8,-6,16, 16)fromRect:NSZeroRect
                 operation:NSCompositeSourceOver
                  fraction:1.0];
        x_Offset =  16;
    }
     [[NSGraphicsContext currentContext] restoreGraphicsState];

    [super drawLabel:shouldTruncateLabel inRect:tabRect];
}

Why transformation:
Image was showing inverted, so i need to transform,

Why offset:
Even after transformation, i need to adjust, the position so that it looks just before the title,

and guys, i while setting the title,

append a Space, so title will not overlap the image, i know this is ugly approach, but couldn’t get any other quick way to do it, if i draw the text myself then i need to take care of truncating also,

Thanks to those who looked at questions and answer
Kind Regards
Rohan

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文