我在 NSMatrix 中显示按钮。
我的要求是:
当满足特定条件时,更改按钮标题的颜色并将图像放置在标题的开头。
为此,我使用了以下代码:
// setting attributed text
NSAttributedString *selectedCellAttribute;
NSFont *selectedCellFont = [NSFont fontWithName:@"Lucida Grande" size:11];
NSColor *selectedCellColor = [NSColor redColor];
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setAlignment:NSCenterTextAlignment];
// setting image
NSTextAttachment *imageAttachment = [[NSTextAttachment alloc] init];
NSCell *cell = [imageAttachment attachmentCell];
[cell setImage:[NSImage imageNamed:@"caution_small.png"]];
NSDictionary *selectedCellDictionary = [NSDictionary dictionaryWithObjectsAndKeys:imageAttachment,NSAttachmentAttributeName,selectedCellFont,NSFontAttributeName,selectedCellColor,NSForegroundColorAttributeName,style,NSParagraphStyleAttributeName,nil];
// recognizing cell
NSButtonCell *associatedCell = [associatesMatrix cellAtRow:0 column:2];
selectedCellAttribute = [[NSAttributedString alloc] initWithString:[associatedCell title] attributes:selectedCellDictionary];
[associatedCell setAttributedTitle:selectedCellAttribute];
尽管上面的代码显示了标题颜色的变化,但它没有显示标题开头的图像:(
任何人都可以建议我在哪里可能出错或其他一些方法来实现我的要求吗?
编辑:
在线:
NSCell *cell = [imageAttachment attachmentCell];
它在编译时给出这个警告:
type 'id <NSTextAttachmentCell>' does not conform to 'NSCopying" protocol.
谢谢,
米拉杰