Last active
April 29, 2016 08:56
-
-
Save quangtqag/e399e895419555d2be42 to your computer and use it in GitHub Desktop.
Get height of text block with given width and font
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
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