NCRichWorkspace.swift 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // NCRichWorkspace.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 09/01/2020.
  6. // Copyright © 2020 TWS. All rights reserved.
  7. //
  8. import Foundation
  9. @objc class NCViewRichWorkspace: UIView {
  10. @IBOutlet weak var webView: WKWebView!
  11. var richWorkspace: String = ""
  12. required init?(coder: NSCoder) {
  13. super.init(coder: coder)
  14. NotificationCenter.default.addObserver(self, selector: #selector(self.changeTheming), name: NSNotification.Name(rawValue: "changeTheming"), object: nil)
  15. self.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
  16. }
  17. @objc func changeTheming() {
  18. self.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
  19. setRichWorkspaceText(richWorkspace)
  20. }
  21. @objc func setRichWorkspaceText(_ richWorkspace: String) {
  22. var color = "#000000"
  23. if CCUtility.getDarkMode() {
  24. color = "#FFFFFF"
  25. }
  26. self.richWorkspace = richWorkspace
  27. var richWorkspaceHtml = ""
  28. let richWorkspaceArray = richWorkspace.components(separatedBy: "\n")
  29. for string in richWorkspaceArray {
  30. if string.hasPrefix("# ") {
  31. richWorkspaceHtml = richWorkspaceHtml + "<h1><span style=\"color: \(color);\">" + string.replacingOccurrences(of: "# ", with: "") + "</span></h1>"
  32. } else if string.hasPrefix("## ") {
  33. richWorkspaceHtml = richWorkspaceHtml + "<h2><span style=\"color: \(color);\">" + string.replacingOccurrences(of: "## ", with: "") + "</span></h2>"
  34. } else if string.hasPrefix("### ") {
  35. richWorkspaceHtml = richWorkspaceHtml + "<h3><span style=\"color: \(color);\">" + string.replacingOccurrences(of: "### ", with: "") + "</span></h3>"
  36. } else if string.hasPrefix("#### ") {
  37. richWorkspaceHtml = richWorkspaceHtml + "<h4><span style=\"color: \(color);\">" + string.replacingOccurrences(of: "#### ", with: "") + "</span></h4>"
  38. } else if string.hasPrefix("##### ") {
  39. richWorkspaceHtml = richWorkspaceHtml + "<h5><span style=\"color: \(color);\">" + string.replacingOccurrences(of: "##### ", with: "") + "</span></h5>"
  40. } else if string.hasPrefix("###### ") {
  41. richWorkspaceHtml = richWorkspaceHtml + "<h6><span style=\"color: \(color);\">" + string.replacingOccurrences(of: "###### ", with: "") + "</span></h6>"
  42. } else {
  43. richWorkspaceHtml = richWorkspaceHtml + "<span style=\"color: \(color);\">" + string + "</span>"
  44. }
  45. richWorkspaceHtml = richWorkspaceHtml + "<br>"
  46. }
  47. richWorkspaceHtml = "<!DOCTYPE html><html><body>" + richWorkspaceHtml + "</body></html>"
  48. webView.loadHTMLString(richWorkspaceHtml, baseURL: Bundle.main.bundleURL)
  49. webView.isUserInteractionEnabled = false
  50. webView.isOpaque = false
  51. }
  52. }
  53. extension UIColor {
  54. public convenience init?(hex: String) {
  55. let r, g, b, a: CGFloat
  56. if hex.hasPrefix("#") {
  57. let start = hex.index(hex.startIndex, offsetBy: 1)
  58. let hexColor = String(hex[start...])
  59. if hexColor.count == 8 {
  60. let scanner = Scanner(string: hexColor)
  61. var hexNumber: UInt64 = 0
  62. if scanner.scanHexInt64(&hexNumber) {
  63. r = CGFloat((hexNumber & 0xff000000) >> 24) / 255
  64. g = CGFloat((hexNumber & 0x00ff0000) >> 16) / 255
  65. b = CGFloat((hexNumber & 0x0000ff00) >> 8) / 255
  66. a = CGFloat(hexNumber & 0x000000ff) / 255
  67. self.init(red: r, green: g, blue: b, alpha: a)
  68. return
  69. }
  70. }
  71. }
  72. return nil
  73. }
  74. }