NCRichWorkspace.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. import SwiftRichString
  10. @objc class NCViewRichWorkspace: UIView {
  11. @IBOutlet weak var textView: UITextView!
  12. var richWorkspace: String = ""
  13. required init?(coder: NSCoder) {
  14. super.init(coder: coder)
  15. NotificationCenter.default.addObserver(self, selector: #selector(self.changeTheming), name: NSNotification.Name(rawValue: "changeTheming"), object: nil)
  16. self.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  17. }
  18. @objc func changeTheming() {
  19. self.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  20. setRichWorkspaceText(richWorkspace)
  21. }
  22. @objc func setRichWorkspaceText(_ richWorkspace: String) {
  23. let h1 = Style {
  24. $0.font = UIFont.systemFont(ofSize: 25, weight: .bold)
  25. $0.color = NCBrandColor.sharedInstance.textView
  26. }
  27. let h2 = Style {
  28. $0.font = UIFont.systemFont(ofSize: 23, weight: .bold)
  29. $0.color = NCBrandColor.sharedInstance.textView
  30. }
  31. let h3 = Style {
  32. $0.font = UIFont.systemFont(ofSize: 21, weight: .bold)
  33. $0.color = NCBrandColor.sharedInstance.textView
  34. }
  35. let h4 = Style {
  36. $0.font = UIFont.systemFont(ofSize: 19, weight: .bold)
  37. $0.color = NCBrandColor.sharedInstance.textView
  38. }
  39. let h5 = Style {
  40. $0.font = UIFont.systemFont(ofSize: 17, weight: .bold)
  41. $0.color = NCBrandColor.sharedInstance.textView
  42. }
  43. let h6 = Style {
  44. $0.font = UIFont.systemFont(ofSize: 15, weight: .bold)
  45. $0.color = NCBrandColor.sharedInstance.textView
  46. }
  47. let normal = Style {
  48. $0.font = UIFont.systemFont(ofSize: 15)
  49. $0.color = NCBrandColor.sharedInstance.textView
  50. }
  51. self.richWorkspace = richWorkspace
  52. var richWorkspaceStyling = ""
  53. let richWorkspaceArray = richWorkspace.components(separatedBy: "\n")
  54. for string in richWorkspaceArray {
  55. if string.hasPrefix("# ") {
  56. richWorkspaceStyling = richWorkspaceStyling + "<h1>" + string.replacingOccurrences(of: "# ", with: "") + "</h1>\r\n"
  57. } else if string.hasPrefix("## ") {
  58. richWorkspaceStyling = richWorkspaceStyling + "<h2>" + string.replacingOccurrences(of: "## ", with: "") + "</h2>\r\n"
  59. } else if string.hasPrefix("### ") {
  60. richWorkspaceStyling = richWorkspaceStyling + "<h3>" + string.replacingOccurrences(of: "### ", with: "") + "</h3>\r\n"
  61. } else if string.hasPrefix("#### ") {
  62. richWorkspaceStyling = richWorkspaceStyling + "<h4>" + string.replacingOccurrences(of: "#### ", with: "") + "</h4>\r\n"
  63. } else if string.hasPrefix("##### ") {
  64. richWorkspaceStyling = richWorkspaceStyling + "<h5>" + string.replacingOccurrences(of: "##### ", with: "") + "</h5>\r\n"
  65. } else if string.hasPrefix("###### ") {
  66. richWorkspaceStyling = richWorkspaceStyling + "<h6>" + string.replacingOccurrences(of: "###### ", with: "") + "</h6>\r\n"
  67. } else {
  68. richWorkspaceStyling = richWorkspaceStyling + string + "\r\n"
  69. }
  70. }
  71. textView.attributedText = richWorkspaceStyling.set(style: StyleGroup(base: normal, ["h1": h1, "h2": h2, "h3": h3, "h4": h4, "h5": h5, "h6": h6]))
  72. textView.isUserInteractionEnabled = false
  73. textView.sizeToFit()
  74. }
  75. }
  76. extension UIColor {
  77. public convenience init?(hex: String) {
  78. let r, g, b, a: CGFloat
  79. if hex.hasPrefix("#") {
  80. let start = hex.index(hex.startIndex, offsetBy: 1)
  81. let hexColor = String(hex[start...])
  82. if hexColor.count == 8 {
  83. let scanner = Scanner(string: hexColor)
  84. var hexNumber: UInt64 = 0
  85. if scanner.scanHexInt64(&hexNumber) {
  86. r = CGFloat((hexNumber & 0xff000000) >> 24) / 255
  87. g = CGFloat((hexNumber & 0x00ff0000) >> 16) / 255
  88. b = CGFloat((hexNumber & 0x0000ff00) >> 8) / 255
  89. a = CGFloat(hexNumber & 0x000000ff) / 255
  90. self.init(red: r, green: g, blue: b, alpha: a)
  91. return
  92. }
  93. }
  94. }
  95. return nil
  96. }
  97. }