0

我在 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.

谢谢,

米拉杰

4

1 回答 1

1

您已经为整个字符串设置了附件。您需要做的是为字符串添加前缀NSAttachmentCharacter,并仅为字符串的该部分设置附件。

您可能希望在 theNSAttachmentCharacter和实际文本之间放置一个空格。只有NSAttachmentCharacter应该具有附件属性。

于 2010-08-23T07:11:05.513 回答