Set maximum number of lines for UILabel

label.numberOfLines = 3;
CGSize size = [label.text sizeWithFont:label.font
                     constrainedToSize:CGSizeMake(SOME_WIDTH,
                                                  3 * label.font.lineHeight)
                         lineBreakMode:UILineBreakModeWordWrap];
label.bounds = CGRectMake(0, 0,
                          size.width,
                          size.height);

I thought I would give it a try:

func heightForStringDrawing(myString: String, myFont:UIFont, myWidth:CGFloat) -> (CGFloat, Int) {
    var tc = NSTextContainer(size: CGSize(width: myWidth, height:CGFloat(FLT_MAX)))
    var ts = NSTextStorage(string: myString)
    var lm = NSLayoutManager()
    lm.addTextContainer(tc)
    ts.addLayoutManager(lm)
    ts.addAttribute(NSFontAttributeName, value: myFont, range: NSMakeRange(0, ts.length))
    tc.lineFragmentPadding = 0.0
    lm.glyphRangeForTextContainer(tc)
    let height = lm.usedRectForTextContainer(tc).height
    let lines = Int(Float(height / myFont.lineHeight))
    return (height, lines)
}

Comments are certainly welcome! I adopted this from: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/TextLayout/Tasks/StringHeight.html


Just set label.numberOfLines = 3;.

It is somewhat mislabeled, as it actually holds the maximum amount of lines to display.