Created
September 30, 2021 08:08
-
-
Save jerrypm/ae86079a116a5b7f18d13d95fa9f31b5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.swift | |
| // 100DayOfSwift | |
| // | |
| // Created by Jerry Purnama Maulid on 17/06/20. | |
| // Copyright © 2020 Jeri Purnama. All rights reserved. | |
| // | |
| /* | |
| Extension String part I | |
| 1. Convert to html string to attribute string | |
| 2. Check Email Validation | |
| 3. String to Data | |
| */ | |
| extension String { | |
| // MARK: To HTML | |
| func convertToAttributedFromHTML() -> NSAttributedString? { | |
| var attributedText: NSAttributedString? | |
| let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue] | |
| if let data = data(using: .unicode, allowLossyConversion: true), let attrStr = try? NSAttributedString(data: data, options: options, documentAttributes: nil) { | |
| attributedText = attrStr | |
| } | |
| return attributedText | |
| } | |
| // MARK: Check Valid Email | |
| func isValidEmail() -> Bool { | |
| let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}" | |
| let emailPred = NSPredicate(format: "SELF MATCHES %@", emailRegEx) | |
| return emailPred.evaluate(with: self) | |
| } | |
| // MARK: TO Data | |
| func toData() -> Data? { | |
| if let data = self.description.data(using: .utf8, allowLossyConversion: true) { | |
| return data | |
| } | |
| return nil | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment