Created
November 8, 2017 18:31
-
-
Save valvoline/b9a939e29910665c0396f7082d2a91d6 to your computer and use it in GitHub Desktop.
A swift string extension to deal with HTML
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
// | |
// String+HTML.swift | |
// AttributedString | |
// | |
// Created by Costantino Pistagna on 08/11/2017. | |
// Copyright © 2017 sofapps.it All rights reserved. | |
// | |
import UIKit | |
import Foundation | |
extension UIColor { | |
var hexString:String? { | |
if let components = self.cgColor.components { | |
let r = components[0] | |
let g = components[1] | |
let b = components[2] | |
return String(format: "%02X%02X%02X", (Int)(r * 255), (Int)(g * 255), (Int)(b * 255)) | |
} | |
return nil | |
} | |
} | |
extension String { | |
var html2Attributed: NSAttributedString? { | |
do { | |
guard let data = data(using: String.Encoding.utf8) else { | |
return nil | |
} | |
return try NSAttributedString(data: data, | |
options: [.documentType: NSAttributedString.DocumentType.html, | |
.characterEncoding: String.Encoding.utf8.rawValue], | |
documentAttributes: nil) | |
} catch { | |
print("error: ", error) | |
return nil | |
} | |
} | |
var htmlAttributed: (NSAttributedString?, NSDictionary?) { | |
do { | |
guard let data = data(using: String.Encoding.utf8) else { | |
return (nil, nil) | |
} | |
var dict:NSDictionary? | |
dict = NSMutableDictionary() | |
return try (NSAttributedString(data: data, | |
options: [.documentType: NSAttributedString.DocumentType.html, | |
.characterEncoding: String.Encoding.utf8.rawValue], | |
documentAttributes: &dict), dict) | |
} catch { | |
print("error: ", error) | |
return (nil, nil) | |
} | |
} | |
func htmlAttributed(using font: UIFont, color: UIColor) -> NSAttributedString? { | |
do { | |
let htmlCSSString = "<style>" + | |
"html *" + | |
"{" + | |
"font-size: \(font.pointSize)pt !important;" + | |
"color: #\(color.hexString!) !important;" + | |
"font-family: \(font.familyName), Helvetica !important;" + | |
"}</style> \(self)" | |
guard let data = htmlCSSString.data(using: String.Encoding.utf8) else { | |
return nil | |
} | |
return try NSAttributedString(data: data, | |
options: [.documentType: NSAttributedString.DocumentType.html, | |
.characterEncoding: String.Encoding.utf8.rawValue], | |
documentAttributes: nil) | |
} catch { | |
print("error: ", error) | |
return nil | |
} | |
} | |
func htmlAttributed(family: String?, size: CGFloat, color: UIColor) -> NSAttributedString? { | |
do { | |
let htmlCSSString = "<style>" + | |
"html *" + | |
"{" + | |
"font-size: \(size)pt !important;" + | |
"color: #\(color.hexString!) !important;" + | |
"font-family: \(family ?? "Helvetica"), Helvetica !important;" + | |
"}</style> \(self)" | |
guard let data = htmlCSSString.data(using: String.Encoding.utf8) else { | |
return nil | |
} | |
return try NSAttributedString(data: data, | |
options: [.documentType: NSAttributedString.DocumentType.html, | |
.characterEncoding: String.Encoding.utf8.rawValue], | |
documentAttributes: nil) | |
} catch { | |
print("error: ", error) | |
return nil | |
} | |
} | |
} |
"font-size: \(font.pointSize)pt !important;" +
"pt" should be replace to "px"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
try this