Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't think there is any silver bullet for this. </p> <p>Here is a category method that seems to work for the few basic test cases I threw at it. No guarantees it won't break with something complex!</p> <p>The way it works is to move through the string testing to see if a range of words fits in the width of the label. When it calculates that the current range is too wide it records the last-fitting range as a line.</p> <p>I don't claim this is efficient. A better way may just to be to implement your own UILabel...</p> <pre><code>@interface UILabel (Extensions) - (NSArray*) lines; @end @implementation UILabel (Extensions) - (NSArray*) lines { if ( self.lineBreakMode != UILineBreakModeWordWrap ) { return nil; } NSMutableArray* lines = [NSMutableArray arrayWithCapacity:10]; NSCharacterSet* wordSeparators = [NSCharacterSet whitespaceAndNewlineCharacterSet]; NSString* currentLine = self.text; int textLength = [self.text length]; NSRange rCurrentLine = NSMakeRange(0, textLength); NSRange rWhitespace = NSMakeRange(0,0); NSRange rRemainingText = NSMakeRange(0, textLength); BOOL done = NO; while ( !done ) { // determine the next whitespace word separator position rWhitespace.location = rWhitespace.location + rWhitespace.length; rWhitespace.length = textLength - rWhitespace.location; rWhitespace = [self.text rangeOfCharacterFromSet: wordSeparators options: NSCaseInsensitiveSearch range: rWhitespace]; if ( rWhitespace.location == NSNotFound ) { rWhitespace.location = textLength; done = YES; } NSRange rTest = NSMakeRange(rRemainingText.location, rWhitespace.location-rRemainingText.location); NSString* textTest = [self.text substringWithRange: rTest]; CGSize sizeTest = [textTest sizeWithFont: self.font forWidth: 1024.0 lineBreakMode: UILineBreakModeWordWrap]; if ( sizeTest.width &gt; self.bounds.size.width ) { [lines addObject: [currentLine stringByTrimmingCharactersInSet:wordSeparators]]; rRemainingText.location = rCurrentLine.location + rCurrentLine.length; rRemainingText.length = textLength-rRemainingText.location; continue; } rCurrentLine = rTest; currentLine = textTest; } [lines addObject: [currentLine stringByTrimmingCharactersInSet:wordSeparators]]; return lines; } @end </code></pre> <p>use like this:</p> <pre><code>NSArray* lines = [_theLabel lines]; int count = [lines count]; </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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.
 

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