• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to truncate NSStrings?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to truncate NSStrings?


  • Subject: Re: How to truncate NSStrings?
  • From: Nicholas Riley <email@hidden>
  • Date: Thu, 25 Jul 2002 01:34:47 -0500
  • Mail-followup-to: Simon Fraser <email@hidden>, CocoaDev <email@hidden>

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))

  • Prev by Date: Burning a disk image with a fixed window size.
  • Next by Date: NSBeginAlertSheet and contextInfo
  • Previous by thread: Re: How to truncate NSStrings?
  • Next by thread: printing
  • Index(es):
    • Date
    • Thread