Skip to content

Instantly share code, notes, and snippets.

@quangtqag
Last active April 29, 2016 08:56
Show Gist options
  • Save quangtqag/e399e895419555d2be42 to your computer and use it in GitHub Desktop.
Save quangtqag/e399e895419555d2be42 to your computer and use it in GitHub Desktop.
Get height of text block with given width and font
extension String {
func heightWithConstrainedWidth(width: CGFloat, font: UIFont) -> CGFloat {
let constraintRect = CGSize(width: width, height: CGFloat.max)
let boundingBox = self.boundingRectWithSize(constraintRect, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
return boundingBox.height
}
func isEmail() -> Bool {
let regex = try? NSRegularExpression(pattern: "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$", options: .CaseInsensitive)
return regex?.firstMatchInString(self, options: [], range: NSMakeRange(0, self.characters.count)) != nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment