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付け、文字列のそのセクションにのみ添付ファイルを設定することです。

NSAttachmentCharacterと実際のテキストの間にスペースを入れたい場合があります。のみがNSAttachmentCharacter添付属性を持つ必要があります。

于 2010-08-23T07:11:05.513 に答える