Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 为NSTableView单元格绘制带边框和背景的文本_Objective C_Macos_Cocoa_Nstableview - Fatal编程技术网

Objective c 为NSTableView单元格绘制带边框和背景的文本

Objective c 为NSTableView单元格绘制带边框和背景的文本,objective-c,macos,cocoa,nstableview,Objective C,Macos,Cocoa,Nstableview,我有一个基于单元格的tableview,我想在这个tableview中显示某种标记,最好不用使用基于视图的tableview 有没有一种优雅的方法可以实现像这里的示例(HTML)这样的东西,最好是使用背景色 如果您想坚持使用基于单元格的表视图,可以将NSCell子类化并覆盖: - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView { NSRect insetRect = NSInsetRect(cellFr

我有一个基于单元格的tableview,我想在这个tableview中显示某种标记,最好不用使用基于视图的tableview

有没有一种优雅的方法可以实现像这里的示例(HTML)这样的东西,最好是使用背景色


如果您想坚持使用基于单元格的表视图,可以将
NSCell子类化并覆盖:

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView
{
    NSRect insetRect = NSInsetRect(cellFrame, 2.0, 2.0);
    NSBezierPath* path = [NSBezierPath bezierPathWithRoundedRect:insetRect xRadius:3.0 yRadius:3.0];
    [path stroke];
    [[NSColor whiteColor] setFill];
    [path fill];
    [[NSColor brownColor] setStroke];
    [path stroke];
    NSMutableAttributedString* content = [[NSMutableAttributedString alloc] initWithString:@"DUPLICATE"];
    NSFontManager* fontManager = [NSFontManager sharedFontManager];
    NSFont* font = [fontManager fontWithFamily:@"Verdana"
                                              traits:NSBoldFontMask
                                              weight:0
                                                size:13.0];
    NSDictionary* attributes = @{NSFontAttributeName:font,
                                 NSForegroundColorAttributeName:[NSColor brownColor]};
    [content setAttributes:attributes range:NSMakeRange(0, [content length])];
    [content setAlignment:NSCenterTextAlignment range:NSMakeRange(0, [content length])];
    [content drawInRect:cellFrame];
}
上面的代码生成了一个与您的按钮模糊不清的单元格(您必须自己调整字体、颜色、线条样式等):

我还通过提供以下内容调整了表视图代理中的行高度:

- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
{
    return 24.0;
}

你能在“绘制时间”知道单元格的大小并调整边框的大小吗?单元格的框矩形通过cellFrame传入。如果要测量文本,可以使用NSAttributedString的boundingRectWithSize:选项: