Skip to content

Instantly share code, notes, and snippets.

@raaowx
Last active March 2, 2023 16:56
Show Gist options
  • Save raaowx/d1a8833166638cba9fa0a468099bdcf1 to your computer and use it in GitHub Desktop.
Save raaowx/d1a8833166638cba9fa0a468099bdcf1 to your computer and use it in GitHub Desktop.
Swift - UIKit - UILabel
import UIKit
extension UILabel {
/// The number of lines required by the containing text taking into account the font line height.
var lines: Int {
guard let text = self.text as? NSString else { return 0 }
let size = CGSize(width: self.bounds.width, height: CGFloat.greatestFiniteMagnitude)
let labelSize = text.boundingRect(
with: size,
options: .usesLineFragmentOrigin,
attributes: [
.font: self.font as Any
],
context: nil)
return Int(ceil(CGFloat(labelSize.height) / self.font.lineHeight))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment