Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's how I did it. First put your label's lines in NSArray, and then check width of last line. In viewDidLoad:</p> <pre><code>NSArray* lines = [self getSeparatedLinesFromLbl:srcLabel]; NSString *lastLine=[lines lastObject]; float lastLineWidth=[lastLine sizeWithFont:srcLabel.font constrainedToSize:boundingSize lineBreakMode:NSLineBreakByWordWrapping].width; </code></pre> <p>And getSeparatedLinesFromLbl:</p> <pre><code>-(NSArray*)getSeparatedLinesFromLbl:(UILabel*)lbl { if ( lbl.lineBreakMode != NSLineBreakByWordWrapping ) { return nil; } NSMutableArray* lines = [NSMutableArray arrayWithCapacity:10]; NSCharacterSet* wordSeparators = [NSCharacterSet whitespaceAndNewlineCharacterSet]; NSString* currentLine = lbl.text; int textLength = [lbl.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 = [lbl.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 = [lbl.text substringWithRange: rTest]; CGSize sizeTest = [textTest sizeWithFont: lbl.font forWidth: 1024.0 lineBreakMode: NSLineBreakByWordWrapping]; if ( sizeTest.width &gt; lbl.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; } </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. This table or related slice is empty.
    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