Created
September 9, 2019 15:11
-
-
Save gali8/45f5ce25ca111f549960e6b79f7ba2c1 to your computer and use it in GitHub Desktop.
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 String { | |
func html(font: UIFont) -> NSAttributedString? { | |
do { | |
let modifiedText = String(format: "<span style=\"font-family: \(font.fontName); font-size: \(font.pointSize)\">%@</span>", self) as String | |
let attrStr = try NSAttributedString( | |
data: modifiedText.data(using: String.Encoding.unicode, allowLossyConversion: true)!, | |
options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], | |
documentAttributes: nil) | |
return attrStr | |
} catch let error { | |
debugPrint("label body error: \(error)") | |
return nil | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment