[ABANDONED] Mastodon iOS client.
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

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. //
  2. // String+HtmlAttributed.swift
  3. // elpha-ios
  4. //
  5. // Created by Dwayne Harris on 11/6/18.
  6. // Copyright © 2018 Elpha. All rights reserved.
  7. //
  8. import UIKit
  9. extension String {
  10. func htmlAttributed(size: CGFloat, centered: Bool = false, color: UIColor? = nil) -> NSAttributedString? {
  11. let color = color ?? UIColor(named: "Text")!
  12. do {
  13. let htmlString = """
  14. <style>
  15. html * {
  16. font-family: -apple-system !important;
  17. font-size: \(size)px !important;
  18. color: \(color.hexString()) !important;
  19. text-align: \(centered ? "center" : "initial") !important;
  20. }
  21. p:last-child {
  22. margin-bottom: 0px !important;
  23. }
  24. </style>
  25. \(self)
  26. """
  27. guard let data = htmlString.data(using: String.Encoding.utf16) else {
  28. return nil
  29. }
  30. return try NSAttributedString(
  31. data: data,
  32. options: [.documentType: NSAttributedString.DocumentType.html],
  33. documentAttributes: nil
  34. )
  35. } catch {
  36. print("\(error)")
  37. return nil
  38. }
  39. }
  40. }