NCRichWorkspace.swift 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. required init?(coder: NSCoder) {
  12. super.init(coder: coder)
  13. NotificationCenter.default.addObserver(self, selector: #selector(self.changeTheming), name: NSNotification.Name(rawValue: "changeTheming"), object: nil)
  14. self.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
  15. }
  16. @objc func changeTheming() {
  17. self.backgroundColor = NCBrandColor.sharedInstance.backgroundView;
  18. }
  19. @objc func setRichWorkspaceText(_ richWorkspace: String) {
  20. var richWorkspaceHtml = ""
  21. let richWorkspaceArray = richWorkspace.components(separatedBy: "\n")
  22. for string in richWorkspaceArray {
  23. if string.hasPrefix("# ") {
  24. richWorkspaceHtml = richWorkspaceHtml + "<h1><span style=\"color: #000000;\">" + string.replacingOccurrences(of: "# ", with: "") + "</span></h1>"
  25. } else if string.hasPrefix("## ") {
  26. richWorkspaceHtml = richWorkspaceHtml + "<h2><span style=\"color: #000000;\">" + string.replacingOccurrences(of: "## ", with: "") + "</span></h2>"
  27. } else if string.hasPrefix("### ") {
  28. richWorkspaceHtml = richWorkspaceHtml + "<h3><span style=\"color: #000000;\">" + string.replacingOccurrences(of: "### ", with: "") + "</span></h3>"
  29. } else if string.hasPrefix("#### ") {
  30. richWorkspaceHtml = richWorkspaceHtml + "<h4><span style=\"color: #000000;\">" + string.replacingOccurrences(of: "#### ", with: "") + "</span></h4>"
  31. } else if string.hasPrefix("##### ") {
  32. richWorkspaceHtml = richWorkspaceHtml + "<h5><span style=\"color: #000000;\">" + string.replacingOccurrences(of: "##### ", with: "") + "</span></h5>"
  33. } else if string.hasPrefix("###### ") {
  34. richWorkspaceHtml = richWorkspaceHtml + "<h6><span style=\"color: #000000;\">" + string.replacingOccurrences(of: "###### ", with: "") + "</span></h6>"
  35. } else {
  36. richWorkspaceHtml = richWorkspaceHtml + "<span style=\"color: #000000;\">" + string + "</span>"
  37. }
  38. richWorkspaceHtml = richWorkspaceHtml + "<br>"
  39. }
  40. webView.loadHTMLString(richWorkspaceHtml, baseURL: Bundle.main.bundleURL)
  41. webView.isUserInteractionEnabled = false
  42. }
  43. }