You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.5 KiB
45 lines
1.5 KiB
//
|
|
// String+HtmlAttributed.swift
|
|
// elpha-ios
|
|
//
|
|
// Created by Dwayne Harris on 11/6/18.
|
|
// Copyright © 2018 Elpha. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
extension String {
|
|
func htmlAttributed(size: CGFloat, centered: Bool = false, color: UIColor? = nil) -> NSAttributedString? {
|
|
let color = color ?? UIColor(named: "Text")!
|
|
|
|
do {
|
|
let htmlString = """
|
|
<style>
|
|
html * {
|
|
font-family: -apple-system !important;
|
|
font-size: \(size)px !important;
|
|
color: \(color.hexString()) !important;
|
|
text-align: \(centered ? "center" : "initial") !important;
|
|
}
|
|
p:last-child {
|
|
margin-bottom: 0px !important;
|
|
}
|
|
</style>
|
|
\(self)
|
|
"""
|
|
|
|
guard let data = htmlString.data(using: String.Encoding.utf16) else {
|
|
return nil
|
|
}
|
|
|
|
return try NSAttributedString(
|
|
data: data,
|
|
options: [.documentType: NSAttributedString.DocumentType.html],
|
|
documentAttributes: nil
|
|
)
|
|
} catch {
|
|
print("\(error)")
|
|
return nil
|
|
}
|
|
}
|
|
}
|