<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Truncate a String and Append an Ellipsis, Respecting the Font Size</title>
	<atom:link href="http://MobileDeveloperTips.com/cocoa/truncate-an-nsstring-and-append-an-ellipsis-respecting-the-font-size.html/feed" rel="self" type="application/rss+xml" />
	<link>http://MobileDeveloperTips.com/cocoa/truncate-an-nsstring-and-append-an-ellipsis-respecting-the-font-size.html</link>
	<description>iOS and Objective-C Tips, Tricks and Tutorials.</description>
	<lastBuildDate>Sat, 19 May 2012 05:15:17 -0500</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: Jackson</title>
		<link>http://MobileDeveloperTips.com/cocoa/truncate-an-nsstring-and-append-an-ellipsis-respecting-the-font-size.html#comment-69751</link>
		<dc:creator>Jackson</dc:creator>
		<pubDate>Wed, 18 Apr 2012 12:17:07 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6737#comment-69751</guid>
		<description>Thanks for posting your snazzy code. You saved me a good half hour.</description>
		<content:encoded><![CDATA[<p>Thanks for posting your snazzy code. You saved me a good half hour.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Charton Sapinoso</title>
		<link>http://MobileDeveloperTips.com/cocoa/truncate-an-nsstring-and-append-an-ellipsis-respecting-the-font-size.html#comment-68692</link>
		<dc:creator>Charton Sapinoso</dc:creator>
		<pubDate>Wed, 21 Mar 2012 00:42:16 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6737#comment-68692</guid>
		<description>Thanks John, this was so useful.

In my case, when we build projects we normally have this utility class. Instead of creating this new class, I added the methods above as a static (+) method
header file:

+ (NSString *)stringByTruncatingToWidth:(CGFloat)width withFont:(UIFont *)font withString:(NSString *)string;

implementation file:
+ (NSString *)stringByTruncatingToWidth:(CGFloat)width withFont:(UIFont *)font withString:(NSString *)string {
    // Create copy that will be the returned result
    NSMutableString *truncatedString = [[string mutableCopy] autorelease];
    
    // Make sure string is longer than requested width
    if ([string sizeWithFont:font].width &gt; width)
    {
        // Accommodate for ellipsis we&#039;ll tack on the end
        width -= [ellipsis sizeWithFont:font].width;
        
        // Get range for last character in string
        NSRange range = {truncatedString.length - 1, 1};
        
        // Loop, deleting characters until string fits within width
        while ([truncatedString sizeWithFont:font].width &gt; width) 
        {
            // Delete character at end
            [truncatedString deleteCharactersInRange:range];
            
            // Move back another character
            range.location--;
        }
        
        // Append ellipsis
        [truncatedString replaceCharactersInRange:range withString:ellipsis];
    }
    
    return truncatedString;
}

usage: 
[DeviceUtility stringByTruncatingToWidth:myTextLabel.frame.size.width withFont:myFont withString:myString];

Hope this helps someone too.</description>
		<content:encoded><![CDATA[<p>Thanks John, this was so useful.</p>
<p>In my case, when we build projects we normally have this utility class. Instead of creating this new class, I added the methods above as a static (+) method<br />
header file:</p>
<p>+ (NSString *)stringByTruncatingToWidth:(CGFloat)width withFont:(UIFont *)font withString:(NSString *)string;</p>
<p>implementation file:<br />
+ (NSString *)stringByTruncatingToWidth:(CGFloat)width withFont:(UIFont *)font withString:(NSString *)string {<br />
    // Create copy that will be the returned result<br />
    NSMutableString *truncatedString = [[string mutableCopy] autorelease];</p>
<p>    // Make sure string is longer than requested width<br />
    if ([string sizeWithFont:font].width &gt; width)<br />
    {<br />
        // Accommodate for ellipsis we&#8217;ll tack on the end<br />
        width -= [ellipsis sizeWithFont:font].width;</p>
<p>        // Get range for last character in string<br />
        NSRange range = {truncatedString.length &#8211; 1, 1};</p>
<p>        // Loop, deleting characters until string fits within width<br />
        while ([truncatedString sizeWithFont:font].width &gt; width)<br />
        {<br />
            // Delete character at end<br />
            [truncatedString deleteCharactersInRange:range];</p>
<p>            // Move back another character<br />
            range.location&#8211;;<br />
        }</p>
<p>        // Append ellipsis<br />
        [truncatedString replaceCharactersInRange:range withString:ellipsis];<br />
    }</p>
<p>    return truncatedString;<br />
}</p>
<p>usage:<br />
[DeviceUtility stringByTruncatingToWidth:myTextLabel.frame.size.width withFont:myFont withString:myString];</p>
<p>Hope this helps someone too.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: phait</title>
		<link>http://MobileDeveloperTips.com/cocoa/truncate-an-nsstring-and-append-an-ellipsis-respecting-the-font-size.html#comment-38584</link>
		<dc:creator>phait</dc:creator>
		<pubDate>Tue, 25 Jan 2011 06:27:56 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6737#comment-38584</guid>
		<description>how would you do the same thing for multiple lines where the ellipsis is only added to the last line?</description>
		<content:encoded><![CDATA[<p>how would you do the same thing for multiple lines where the ellipsis is only added to the last line?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: berec</title>
		<link>http://MobileDeveloperTips.com/cocoa/truncate-an-nsstring-and-append-an-ellipsis-respecting-the-font-size.html#comment-31026</link>
		<dc:creator>berec</dc:creator>
		<pubDate>Wed, 10 Nov 2010 14:01:32 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6737#comment-31026</guid>
		<description>Hello. Thank you for that. Is there any way to truncate multiline text? I mean  adding to your category method like - (NSString*)stringByTruncatingToSize:(CGSize)size withFont:(UIFont *)font; Is it possible?
Thanks in advance.</description>
		<content:encoded><![CDATA[<p>Hello. Thank you for that. Is there any way to truncate multiline text? I mean  adding to your category method like &#8211; (NSString*)stringByTruncatingToSize:(CGSize)size withFont:(UIFont *)font; Is it possible?<br />
Thanks in advance.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Krille</title>
		<link>http://MobileDeveloperTips.com/cocoa/truncate-an-nsstring-and-append-an-ellipsis-respecting-the-font-size.html#comment-21375</link>
		<dc:creator>Krille</dc:creator>
		<pubDate>Tue, 27 Jul 2010 12:18:06 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6737#comment-21375</guid>
		<description>Great - thank you very much, everytime I learn something new from your articles!</description>
		<content:encoded><![CDATA[<p>Great &#8211; thank you very much, everytime I learn something new from your articles!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Muchow</title>
		<link>http://MobileDeveloperTips.com/cocoa/truncate-an-nsstring-and-append-an-ellipsis-respecting-the-font-size.html#comment-21374</link>
		<dc:creator>John Muchow</dc:creator>
		<pubDate>Tue, 27 Jul 2010 12:01:00 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6737#comment-21374</guid>
		<description>Krille, thanks for pointing that out. I&#039;ve updated the code to check for the string length up front.</description>
		<content:encoded><![CDATA[<p>Krille, thanks for pointing that out. I&#8217;ve updated the code to check for the string length up front.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Krille</title>
		<link>http://MobileDeveloperTips.com/cocoa/truncate-an-nsstring-and-append-an-ellipsis-respecting-the-font-size.html#comment-21369</link>
		<dc:creator>Krille</dc:creator>
		<pubDate>Tue, 27 Jul 2010 10:10:18 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6737#comment-21369</guid>
		<description>Thank you for this post!
I have a problem with this implementation: Strings are cut, even if they are narrower
than the defined maximum width:

Example:
cell.textLabel.text = [news stringByTruncatingToWidth:240 withFont:[UIFont boldSystemFontOfSize:15]];

So every String is at least missing the last character and gets the ellipsis appended...

&quot;Here is a superlong Title to...&quot; -&gt; correct
&quot;Short Titl...&quot; -&gt; wrong

Cheers
Krille</description>
		<content:encoded><![CDATA[<p>Thank you for this post!<br />
I have a problem with this implementation: Strings are cut, even if they are narrower<br />
than the defined maximum width:</p>
<p>Example:<br />
cell.textLabel.text = [news stringByTruncatingToWidth:240 withFont:[UIFont boldSystemFontOfSize:15]];</p>
<p>So every String is at least missing the last character and gets the ellipsis appended&#8230;</p>
<p>&#8220;Here is a superlong Title to&#8230;&#8221; -&gt; correct<br />
&#8220;Short Titl&#8230;&#8221; -&gt; wrong</p>
<p>Cheers<br />
Krille</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Muchow</title>
		<link>http://MobileDeveloperTips.com/cocoa/truncate-an-nsstring-and-append-an-ellipsis-respecting-the-font-size.html#comment-21283</link>
		<dc:creator>John Muchow</dc:creator>
		<pubDate>Mon, 26 Jul 2010 14:27:52 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6737#comment-21283</guid>
		<description>Michelle, the docs state &quot;If you are using managed memory (not garbage collection), this method retains the new object before returning it.&quot; so the autorelease should do the trick...</description>
		<content:encoded><![CDATA[<p>Michelle, the docs state &#8220;If you are using managed memory (not garbage collection), this method retains the new object before returning it.&#8221; so the autorelease should do the trick&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michelle</title>
		<link>http://MobileDeveloperTips.com/cocoa/truncate-an-nsstring-and-append-an-ellipsis-respecting-the-font-size.html#comment-21282</link>
		<dc:creator>Michelle</dc:creator>
		<pubDate>Mon, 26 Jul 2010 14:20:48 +0000</pubDate>
		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6737#comment-21282</guid>
		<description>Does NSString mutableCopy really return a string that needs to be released?  By standard naming conventions, it should not.

You might want to generalize the string to be appended.

- (NSString*)stringByTruncatingStringToWidth:(CGFloat)width withFont:(UIFont *)font endingWith:(NSString*)endString {  /* your implementation but using endString */ }

and implement your function as

- (NSString*)stringByTruncatingStringToWidth:(CGFloat)width withFont:(UIFont *)font
{
   [self stringByTruncatingStringToWidth: width withFont: font endingWith: ellipsis];
}

But that&#039;s just a small tweak.</description>
		<content:encoded><![CDATA[<p>Does NSString mutableCopy really return a string that needs to be released?  By standard naming conventions, it should not.</p>
<p>You might want to generalize the string to be appended.</p>
<p>- (NSString*)stringByTruncatingStringToWidth:(CGFloat)width withFont:(UIFont *)font endingWith:(NSString*)endString {  /* your implementation but using endString */ }</p>
<p>and implement your function as</p>
<p>- (NSString*)stringByTruncatingStringToWidth:(CGFloat)width withFont:(UIFont *)font<br />
{<br />
   [self stringByTruncatingStringToWidth: width withFont: font endingWith: ellipsis];<br />
}</p>
<p>But that&#8217;s just a small tweak.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

