NSTableView - 富有格式的文本和图像的表列 [英] NSTableView - Table Column with rich formatted text and image

查看:190
本文介绍了NSTableView - 富有格式的文本和图像的表列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的Cocoa和我想显示一列NSTableView与多线丰富格式(不同的字体和颜色在一个单元格内)和图像。 NSTableView是这个任务的好选择吗?

I am new with Cocoa and I want to display one-column NSTableView with multiline rich-formatted (different fonts and colors within one cell) and images. Is the NSTableView a good choice for this task ?

如果我能得到它?我需要使用NSCell控件吗?我会感谢良好的stating教程和示例。

If it how can i get it ? Do I need to use NSCell control ? I will be thankful for good stating tutorial and examples. I have tried to google it, but not find something useful.

推荐答案

基于单元格的表视图方法



对于基于单元格的表视图,您不能这样做,因为标准单元格NSImageCell和NSTextCell只能显示图像或文本。你可以让你的NSCell的子类做一个接受图像和文本的单元格,例如在NSTableView的同一单元格中显示图标和文本。要显示不同的字体和颜色,你需要使用一个 NSAttributedString 例如(没有检查这个代码Xcode,所以可能有一些bug,但它给你一个想法) ,

Cell-based table view approach

With a cell-based table view you can't do this because the standard cell NSImageCell and NSTextCell can only display an image or text. You could make your down subclass of NSCell to make a cell that accepts both a image and text, for example, Display icon and text in same cell of NSTableView. To display different fonts and colors you will need to use a NSAttributedString for example, (no checked this code Xcode so there could be a few bugs, but it gives you an idea),

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 。请注意,您必须使用属性字符串来获取不同的颜色和字体(见上文)。

View-based table view approach

View-based table view display NSTableCellView views in their the table cells. The standard NSTableCellView has a imageView and textField property so for your purpose this would seem like an ideal fit. Simply set the table view cell's image view to display your image, tableCellView.imageView.image = myImageset then set the text you want to display in the text field, tableCellView.textField.attributeStringValue = myAttributedString. Note you will have to use an attributed string to get the different colours and fonts (see above).

这篇关于NSTableView - 富有格式的文本和图像的表列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆