Last active
March 2, 2023 16:56
-
-
Save raaowx/d1a8833166638cba9fa0a468099bdcf1 to your computer and use it in GitHub Desktop.
Swift - UIKit - UILabel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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