Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Just call below method and pass either <code>UILabel</code> or <code>UITextView</code>:</p> <pre><code>-(NSInteger)getNumberOfLinesInLabelOrTextView:(id)obj { NSInteger lineCount = 0; if([obj isKindOfClass:[UILabel class]]) { UILabel *label = (UILabel *)obj; // This method is deprecated in iOS 7.0 or later // CGSize requiredSize = [label.text sizeWithFont:label.font constrainedToSize:label.frame.size lineBreakMode:label.lineBreakMode]; CGSize requiredSize = [label.text boundingRectWithSize:CGSizeMake(CGRectGetWidth(label.frame), CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:label.font} context:nil].size; int charSize = label.font.leading; int rHeight = requiredSize.height; lineCount = rHeight/charSize; } else if ([obj isKindOfClass:[UITextView class]]) { UITextView *textView = (UITextView *)obj; lineCount = textView.contentSize.height / textView.font.leading; } return lineCount; } </code></pre> <p><strong>Now call this method:-</strong></p> <pre><code>NSLog(@"%d",[self getNumberOfLinesInLabelOrTextView:label]); NSLog(@"%d",[self getNumberOfLinesInLabelOrTextView:textView]); </code></pre> <p><strong>UPDATED: SWIFT CODE</strong></p> <pre><code>func getNumberOfLinesInLabelOrTextView(obj:AnyObject) -&gt; NSInteger { var lineCount: NSInteger = 0 if (obj.isKindOfClass(UILabel)) { let label: UILabel = obj as! UILabel let requiredSize: CGSize = (label.text)!.boundingRectWithSize(CGSizeMake(CGRectGetWidth(label.frame), CGFloat.max), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: label.font], context: nil).size let charSize: CGFloat = label.font.leading let rHeight: CGFloat = requiredSize.height lineCount = (NSInteger)(rHeight/charSize) } else if (obj.isKindOfClass(UITextView)){ let textView: UITextView = obj as! UITextView lineCount = (NSInteger)(textView.contentSize.height / textView.font.leading) } return lineCount } </code></pre> <p><strong>Now call this method:-</strong></p> <pre><code>println("%d \(self.getNumberOfLinesInLabelOrTextView(textView))") println("%d \(self.getNumberOfLinesInLabelOrTextView(label))") </code></pre> <p><strong>Note: <code>leading</code> - use lineHeight. does not return actual leading. will be formally deprecated in future.</strong></p>
    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