Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to truncate NSStrings?



On Wed, Jul 24, 2002 at 05:28:45PM -0700, Simon Fraser wrote:
> How do I truncate an NSString in an international-friendly way?
> I want something like the QuickDraw TruncString(), which takes
> a param to truncate in the middle, or at the start or end.
>
> In Appearance.h there is TruncateThemeText(), but that might
> assume a QuickDraw drawing environment.

I currently use TruncateThemeText in my own application, as follows:

- (NSAttributedString *)asAttributedStringTruncatedToWidth:(float)width {
NSMutableString *text = [self mutableCopy];
NSAttributedString *s = [NSAttributedString alloc];
NSMutableParagraphStyle *ps = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
OSStatus err;
err = TruncateThemeText((CFMutableStringRef) text,
kThemeViewsFont, kThemeStateActive, width, truncMiddle, NULL);
if (err != noErr) NSLog(@"TruncateThemeText failed with error %d",
err);
// XXX Should be able to skip the above by using NSLineBreakByTruncatingMiddle,
// XXX but OS X 10.1 doesn't implement it yet.
[ps setLineBreakMode: NSLineBreakByClipping];
s = [s initWithString: text attributes:
[NSDictionary dictionaryWithObjectsAndKeys: ps,
NSParagraphStyleAttributeName, nil]];
[text release];
[ps release];
return [s autorelease];
}

The metrics seem to be close enough that I've never had a significant
over- or underrun (even wth international text). As you see there is
a documented way of doing it with Cocoa but it is not implemented in
10.1.

--
=Nicholas Riley <email@hidden> | <http://www.uiuc.edu/ph/www/njriley>
Pablo Research Group, Department of Computer Science and
Medical Scholars Program, University of Illinois at Urbana-Champaign
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >How to truncate NSStrings? (From: email@hidden (Simon Fraser))



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.