OK, I have spent the last night by digging into the cocoa text system and this is what I could come up with. I'm sure this is not the best way to do it, but the result is very close to what I want to achieve. Basically I use the NSLayoutmanager to find how it would lay out the first line of the text and then do the drawing myself.

Any comment on this?
I really want to do this nicer..

Here is the code:

-(void) drawLabel:(NSString *)label inRect:(NSRect)rect
{
NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:label];

NSMutableParagraphStyle *globalStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
        [globalStyle setLineBreakMode:NSLineBreakByWordWrapping];
        [globalStyle setAlignment:NSCenterTextAlignment];
[textStorage addAttribute:NSParagraphStyleAttributeName value:globalStyle range:NSMakeRange(0, [label length])];
        
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithContainerSize:rect.size];
        
        NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
        [layoutManager addTextContainer:textContainer];
        [textStorage addLayoutManager:layoutManager];

        int glyphIndex = 0;
        NSRange range;

NSRange glyphRange = [layoutManager glyphRangeForTextContainer:textContainer];

        int lines = 1;
        
        while (YES)
        {
NSRect lineRect = [layoutManager lineFragmentUsedRectForGlyphAtIndex:glyphIndex effectiveRange:&range];
                lineRect.origin.x += rect.origin.x;
lineRect.origin.y = (rect.origin.y + rect.size.height) - (lineRect.size.height * lines);
                
                NSString *s = nil;
                
                if (lines == 1)
                {
                        s = [label substringWithRange:range];
                }
                else
                {
                        s = [label substringFromIndex:range.location];
                }
                
                
                [[NSColor redColor] set];
                NSRectFill(lineRect);
                
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
                [style setLineBreakMode:NSLineBreakByTruncatingMiddle];
                [style setAlignment:NSCenterTextAlignment];
[s drawInRect:lineRect withAttributes:[NSDictionary dictionaryWithObject:style forKey:NSParagraphStyleAttributeName]];

                if (NSMaxRange(range) >= NSMaxRange(glyphRange))
                {
                        break;
                }
                
                glyphIndex = NSMaxRange(range);
                lines++;
        }       

        
        [textStorage release];
        [textContainer release];
        [layoutManager release];
}





On Nov 1, 2008, at 2:39 AM, Mudi Dandan wrote:

Hi,

I'm needing a hand with this. I'm trying to emulate the wrapping and truncation for icon labels in Finder's icon view.

My first question is: is there any method around for making a string to line wrap, and to truncate the second line if it is too long (like on the right hand image in the screenshot). Is this supported in an API?

The second questions is: can the NSAttributedString render the background for the text like the ones on the screenshots or is it something I have to draw myself?

here's the screenshot:
http://www.binarynights.com/images/Screenshot.png
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/mudi%40binarynights.com

This email sent to [EMAIL PROTECTED]

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to