Skip to content

Instantly share code, notes, and snippets.

@khanhldt
Created April 16, 2016 09:18
Show Gist options
  • Save khanhldt/815194d4451fcdff904c909f5e259617 to your computer and use it in GitHub Desktop.
Save khanhldt/815194d4451fcdff904c909f5e259617 to your computer and use it in GitHub Desktop.
Utility function to get the needed height for a string to be displayed in a UIKit component
public static func heightNeeded(text:String, font:UIFont, width:CGFloat) -> CGFloat{
let label:UILabel = UILabel(frame: CGRectMake(0, 0, width, CGFloat.max))
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.ByWordWrapping
label.font = font
label.text = text
label.sizeToFit()
return label.frame.height
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment