【问题标题】:NSTabView Item adding an Icon in TabViewItemNSTabView 项在 TabViewItem 中添加一个图标
【发布时间】:2011-03-28 07:44:15
【问题描述】:
我将 NSTabView 子类化并添加了 5 个 TabViewItem,现在我想在 NSTabViewItem 中添加一个图标以及标题,
谁能建议我如何开始,除了,我没有得到任何文档,
- (void)drawLabel:(BOOL)shouldTruncateLabel inRect:(NSRect)tabRect
这是否意味着,如果我重写此方法,我需要自己绘制图标和字符串,
为了设置标题,我使用以下方法,
[pTabViewItem setLabel:pLabelTitle];
亲切的问候
罗汉
【问题讨论】:
标签:
objective-c
cocoa
macos
nstabview
【解决方案1】:
没关系,
以下代码对我有用,
- (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];
}
为什么要转型:
图像显示倒置,所以我需要变换,
为什么要偏移:
即使在转换之后,我也需要调整位置,使其看起来就在标题之前,
伙计们,我在设置标题时,
附加一个空格,因此标题不会与图像重叠,我知道这是丑陋的方法,但无法获得任何其他快速方法来做到这一点,如果我自己绘制文本,那么我还需要注意截断,
感谢那些看问题和回答的人
亲切的问候
罗汉