Re: How to obtain icon displayed by Finder for a file
Re: How to obtain icon displayed by Finder for a file
- Subject: Re: How to obtain icon displayed by Finder for a file
- From: David <email@hidden>
- Date: Wed, 7 Jan 2009 23:51:18 -0500
Amazingly difficult, but I have it working almost 2 different ways. I
created my own ImageAndTextCell class and handle my own drawing based on
some code from Dave Blanton.
I've also tried the NSAttributedString since that sounded potentially
cleaner, more powerful and generally useful.
Argh.
So I go through some amazingly convoluted steps to create the string, partly
because of inconsistency of when you can or cannot append a string.
Now I appear to be having a problem with the baseline. By default, the icon
looks fine, but the text is a few pixels too low and the bottom of letters
gets cut off. If I try to raise it by messing with the
NSBaselineOffsetAttributeName, then the icon gets off center vertically. I
tried setting ranges on the attribute specific to the string without the
attachment but it didn't work. Changes are always propogated to the icon, or
at least I can't figure out how to isolate it.
Here's my code so far. This is in my datasource delegate where I return a
value for a cell.
NSTextAttachment *ta = [[NSTextAttachment alloc]init];
NSTextAttachmentCell *tac = [[NSTextAttachmentCell alloc]init];
NSImage *image = [[NSWorkspace sharedWorkspace]iconForFile:[node pathName]];
[image setScalesWhenResized:TRUE];
NSSize mySize;
mySize.width=16;
mySize.height=16;
[image setSize:mySize];
[tac setImage:image];
[ta setAttachmentCell:tac];
NSAttributedString *as = [NSAttributedString attributedStringWithAttachment
:ta];
NSMutableAttributedString *mas = [[NSMutableAttributedString alloc]
initWithAttributedString:as];
NSString *name = [@" " stringByAppendingString:[node name]];
NSMutableAttributedString *attribName = [[NSMutableAttributedString alloc]
initWithString:name];
[mas appendAttributedString:attribName];
NSRange range;
range.location = 0;
range.length = [mas length];
[mas addAttribute:NSBaselineOffsetAttributeName value:[NSNumber
numberWithFloat: -4.0] range:range];
result = mas;
Thanks
On Wed, Jan 7, 2009 at 9:03 PM, Michael Ash <email@hidden> wrote:
> On Wed, Jan 7, 2009 at 7:39 PM, David <email@hidden> wrote:
> > I must complain again. Cocoa does not have a clean well designed object
> > model. It seems haphazard. Someone needed a function here, but not there.
> > Apple doesn't go back and look at making general purpose classes and
> > methods.
> > Some of the cell classes seem very specialized. I had assumed that
> > NSTextFieldCell could display an icon. Frequently there are outline views
> of
> > files which display icons and names, yet there is no simple class which
> > implements this functionality. Instead everyone needs to write their own
> > with inconsistent behavior.
> >
> > I've been able to get NSBrowserCell to work somewhat by setting
> > setLeaf:TRUE. However, now I get the icon smashed up right next to the
> > disclosure triangle. There again, there are no basic controls to add some
> > spacing. Argh.
> >
> > Thanks for the responses. I guess I'll have to bail on the NSBrowserCell
> and
> > write my own NSCell subclass unless I can come up with a way to add some
> > spacing.
>
> Cocoa can't very well do *everything* for you. If it did, we'd all be
> out of a job, for one.... Personally I find that Cocoa provides plenty
> of facilities and building blocks for more. Just because it doesn't do
> what you want in this one particular case certainly doesn't make it
> generally deficient.
>
> However in this case Cocoa is able to do what you want, using built-in
> features and nothing more. Simply create an NSAttributedString
> containing your image and your text, and using the available
> attributes to control horizontal and vertical spacing. A plain
> NSTextFieldCell can display an NSAttributedString with no
> customization.
>
> Mike
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden