我是Cocoa的新手,我希望显示单列NSTableView,其中包含多行丰富格式(一个单元格内的不同字体和颜色)和图像.NSTableView是否适合完成此任务?
如果它我怎么能得到它?我需要使用NSCell控件吗?我将感谢很好的说明教程和示例.我试图谷歌它,但没有找到有用的东西.
使用基于单元格的表视图时,您无法执行此操作,因为标准单元格NSImageCell和NSTextCell只能显示图像或文本.您可以创建NSCell的子类来创建一个接受图像和文本的单元格,例如,在NSTableView的同一单元格中显示图标和文本.要显示不同的字体和颜色,您需要使用NSAttributedString
例如,(没有检查此代码Xcode,因此可能会有一些错误,但它会给你一个想法),
NSString *redWord = @"Hello"; NSString *greenWord = @"World"; NSString *message = [NSString stringWithFormat:@"%@ %@", redWord, greenWord]; NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithAttributedString:message]; [text addAttribute:NSForegroundColorAttributeName value:[NSColor redColor] range:NSMakeRange(0, [redWord length])]; [text addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Arial Italic" size:12]; range:NSMakeRange(0, [redWord length])]; [text addAttribute:NSForegroundColorAttributeName value:[NSColor greenColor] range:NSMakeRange([redWord length] + 1, [greenWord length])]; [text addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Times Bold" size:12]; range:NSMakeRange([redWord length] + 1, [greenWord length])];
基于视图的表视图在其表格单元格中显示NSTableCellView视图.该标准NSTableCellView具有imageView
和textField
属性,因此你的目的,这将似乎是再合适不过的了.只需设置表格视图单元格的图像视图即可显示图像,tableCellView.imageView.image = myImageset
然后在文本字段中设置要显示的文本tableCellView.textField.attributeStringValue = myAttributedString
.请注意,您必须使用属性字符串来获取不同的颜色和字体(请参见上文).