0

I am displaying buttons in NSMatrix.

My requirement is:

to change color of button title and place an image at beginning of title, when certain condition is satisfied.

To do so, I used following code:

// 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];

Although above code is showing change in color of title, it is showing no image placed in beginning of title :(

Can anyone suggest me where I may be wrong or some other method to implement my requirements?

EDIT:

At line:

NSCell *cell = [imageAttachment attachmentCell];

it is giving this warning when compiled:

type 'id <NSTextAttachmentCell>' does not conform to 'NSCopying" protocol.

Thanks,

Miraaj

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
Devarshi
  • 16,440
  • 13
  • 72
  • 125
  • Don't forget to release the mutable paragraph style, the attachment, and the attributed string, if you're not using GC. – Peter Hosey Aug 23 '10 at 06:59
  • thanx for your reply... the application is GCC enabled! – Devarshi Aug 23 '10 at 07:00
  • Did you mean GC? GCC is the GNU Compiler Collection, and what compiler you're using is irrelevant. GC is Garbage Collection, under which sending Objective-C `release` messages is not strictly necessary (but not doing so is a good way to get gentle reminders of “you need to release those things if you're not using GC” when you post the code on Stack Overflow ☺). – Peter Hosey Aug 23 '10 at 07:10

1 Answers1

1

You've set the attachment for the entire string. What you need to do is prefix the string with NSAttachmentCharacter, and set the attachment only for that section of the string.

You may want to put a space between the NSAttachmentCharacter and your actual text. Only the NSAttachmentCharacter should have the attachment attribute.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSMutableAttributedString_Class/ – Peter Hosey Aug 23 '10 at 12:37
  • I have looked at class reference of NSAttributedString but in it I am getting no clue about how to use NSAttachmentCharacter :( – Devarshi Aug 23 '10 at 12:51
  • You need to insert it at the start of the string, and apply the attachment attribute to it. – Peter Hosey Aug 23 '10 at 13:22