Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 20, 2006, 05:14 PM   #1
dharma
macrumors newbie
 
Join Date: Feb 2006
Inserting an image & text in NSTableView

Hi Folks,

I'd like to insert image and text in the same cell in NSTableview, and the images are different in the different rows. What I know is to use a delegate method

-(void)tableView: WillDisplayCell: forTableColumn: row:
{

}

But I've no idea how I should implement the method. Would anyone help me please. I'm really new in Cocoa, so your detailed explanation is very helpful to me.

Thanks
dharma is offline   0 Reply With Quote
Old Feb 21, 2006, 02:40 AM   #2
caveman_uk
Guest
 
caveman_uk's Avatar
 
Join Date: Feb 2003
Location: Hitchin, Herts, UK
From borkware http://www.borkware.com/quickies/one?topic=NSString
Quote:
Putting an image into an attributed string
You'll need to use a text attachment.

Code:
- (NSAttributedString *) prettyName
{
    NSTextAttachment *attachment;
    attachment = [[[NSTextAttachment alloc] init] autorelease];
    NSCell *cell = [attachment attachmentCell];

    NSImage *icon = [self icon]; // or wherever you are getting your image
    [cell setImage: icon];

    NSString *name = [self name];
    NSAttributedString *attrname;
    attrname = [[NSAttributedString alloc] initWithString: name];

    NSMutableAttributedString *prettyName;
    prettyName = (id)[NSMutableAttributedString attributedStringWithAttachment:
                                                attachment]; // cast to quiet compiler warning
    [prettyName appendAttributedString: attrname];

    return (prettyName);

} // prettyName

This puts the image at the front of the string. To put the image in the middle of the string, you'll need to create an attributedstring with attachment, and then append that to your final attributed string.

This puts the image at the front of the string. To put the image in the middle of the string, you'll need to create an attributedstring with attachment, and then append that to your final attributed string.
I'd probably cache the attributed string somewhere so you don't have to generate everytime the tableview gets updated
caveman_uk is offline   0 Reply With Quote
Old Feb 21, 2006, 02:52 AM   #3
caveman_uk
Guest
 
caveman_uk's Avatar
 
Join Date: Feb 2003
Location: Hitchin, Herts, UK
Is that enough detail or do you need help on how to actually write the datasource/delegate methods? In this case I don't think you'd need to use the willDisplayCell method as I think you're OK just returning the NSAttributedString in the objectValueForTableColumn: row: datasource method. I've not actually done this though so I might be wrong. I have used willDisplayCell before for more complicated things with custom cells but as I said I don't think you need that here.
caveman_uk is offline   0 Reply With Quote
Old Feb 21, 2006, 08:00 AM   #4
dharma
Thread Starter
macrumors newbie
 
Join Date: Feb 2006
Thanks caveman_uk for your help. It works, but the text sits at the bottom of the icon's height. It'll be perfect if the text is at the center height.
dharma is offline   0 Reply With Quote
Old Feb 21, 2006, 08:55 AM   #5
caveman_uk
Guest
 
caveman_uk's Avatar
 
Join Date: Feb 2003
Location: Hitchin, Herts, UK
Ahhh, your icon is a lot bigger than the text size then. Do you want it that big? If not, resize it using NSImage's -setSize. Otherwise you'll need to create a custom NSCell class and handle all the drawing yourself. There's plenty of gotchas that await there including the 'Thou must implement copyWithZone: properly' gotcha.

You could always cheat and actually show the icon and text in different table columns - just hide the column borders.
caveman_uk is offline   0 Reply With Quote
Old Feb 21, 2006, 09:03 AM   #6
caveman_uk
Guest
 
caveman_uk's Avatar
 
Join Date: Feb 2003
Location: Hitchin, Herts, UK
Incidently, I've just noticed the code I posted earlier has a memory leak - attrname needs to be released before the return.
caveman_uk is offline   0 Reply With Quote
Old Feb 21, 2006, 09:10 AM   #7
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
I think there might be another way. Can you not alter the baseline for the text part of the attributed string to move it up a bit?
robbieduncan is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
How to use an image's title as text in the photo - Template? snberk103 Digital Photography 4 Feb 26, 2014 11:13 AM
All iPads: Why does my iPad insist on inserting the wrong text? Lucille Carter iPad 6 Jan 13, 2014 05:06 AM
Text editing & cleaning unwanted text K3it4r0 Mac Applications and Mac App Store 4 Dec 2, 2013 09:32 AM
iPhone: Any way to change Carrier image/text without jailbreak? plainwhitetay iOS 6 25 Jan 9, 2013 06:38 PM
Editing an image. (Add text and border) Abrexas iPhone and iPod touch Apps 0 Jul 18, 2012 11:46 AM

Forum Jump

All times are GMT -5. The time now is 03:48 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC