Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Original Answer <em>(iOS 6 and below)</em></h2> <p>Sadly, <code>UILabel</code> with <code>numberOfLines</code> wont do it if you need the view to be editable. Or you want <code>UITextView</code>'s (native) vertical alignment.</p> <p>Here's an <code>NSString</code> category that deletes words from a string, according to it's size in a rect:</p> <pre><code>@interface NSString (StringThatFits) - (NSString *)stringByDeletingWordsFromStringToFit:(CGRect)rect withInset:(CGFloat)inset usingFont:(UIFont *)font @end @implementation NSString (StringThatFits) - (NSString *)stringByDeletingWordsFromStringToFit:(CGRect)rect withInset:(CGFloat)inset usingFont:(UIFont *)font { NSString *result = [self copy]; CGSize maxSize = CGSizeMake(rect.size.width - (inset * 2), FLT_MAX); CGSize size = [result sizeWithFont:font constrainedToSize:maxSize lineBreakMode:UILineBreakModeWordWrap]; NSRange range; if (rect.size.height &lt; size.height) while (rect.size.height &lt; size.height) { range = [result rangeOfString:@" " options:NSBackwardsSearch]; if (range.location != NSNotFound &amp;&amp; range.location &gt; 0 ) { result = [result substringToIndex:range.location]; } else { result = [result substringToIndex:result.length - 1]; } size = [result sizeWithFont:font constrainedToSize:maxSize lineBreakMode:UILineBreakModeWordWrap]; } return result; } @end </code></pre> <p>For a <code>UITextView</code>, use an inset of 8 to account for the way UIKit draws them:</p> <pre><code>CGRect rect = aTextView.frame; NSString *truncatedString = [theString stringByDeletingWordsFromStringToFit:rect withInset:8.f usingFont:theTextView.font]; </code></pre> <h2>Updated Answer <em>(iOS 7)</em></h2> <p>Now <code>UITextView</code> uses TextKit internally, it's much easier.</p> <p>Rather than truncating the actual string, set the <code>text</code> (or <code>attributedText</code>) property to the whole string and truncate the amount of text <em>displayed</em> in the container (just like we do with <code>UILabel</code>):</p> <pre><code>self.textView.scrollEnabled = NO; self.textView.textContainer.maximumNumberOfLines = 0; self.textView.textContainer.lineBreakMode = NSLineBreakByTruncatingTail; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload