WaterMarkView.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // WaterMarkView.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2022/3/30.
  6. // Copyright © 2022 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. ///
  11. /// 水印
  12. /// 这个view 是一个很大的正方形,目前是SCREEN_HEIGHT的3倍宽高
  13. /// 里面放一个tableview 把水印文字放入到tableview中
  14. /// 最后把这个水印的整个view放到页面上,并且旋转角度 并裁减超出部分
  15. ///使用: let waterView = WaterMarkView.addWaterMarkView(waterMarkText: "你的姓名")
  16. /// self.view.addSubview(waterView)
  17. /// self.view.layer.masksToBounds = true // 裁剪 因为水印是一个很大的view
  18. class WaterMarkView: UIView {
  19. // private var waterMarkView: WaterMarkView?
  20. //
  21. // open func addWaterMark(view: UIView, waterMarkText: String) {
  22. //
  23. // if waterMarkView == nil {
  24. // waterMarkView = WaterMarkView()
  25. // waterMarkView?.frame = view.bounds
  26. // if let wlayer = waterMarkView?.layer {
  27. // view.layer.addSublayer(wlayer)
  28. // }
  29. // waterMarkView?.isUserInteractionEnabled = false
  30. // }
  31. // }
  32. //
  33. // // 添加文本水印
  34. // private func watemarkWithText(waterMarkText: String) -> UIImage? {
  35. // let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14.0)]
  36. // let size = waterMarkText.getSize(with: 14.0)
  37. // UIGraphicsBeginImageContextWithOptions(size, false, 0)
  38. // waterMarkText.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height), withAttributes: attributes)
  39. // let image = UIGraphicsGetImageFromCurrentImageContext()
  40. // UIGraphicsEndImageContext()
  41. // return image
  42. // }
  43. private var tableView: UITableView? = nil
  44. private var waterMarkText: String = ""
  45. private var dataSource: [String] = []
  46. public static let lineHeight:Int = 48 // 行高
  47. let cellIdentifier = "WaterMarkTableViewCell"
  48. static func addWaterMarkView(waterMarkText: String) -> WaterMarkView {
  49. let watermarkView = WaterMarkView(frame: CGRect(x: -SCREEN_HEIGHT, y: -SCREEN_HEIGHT, width: SCREEN_HEIGHT*3, height: SCREEN_HEIGHT*3), waterMarkText: waterMarkText)
  50. watermarkView.transform = CGAffineTransform(rotationAngle: -Double.pi*0.25)
  51. watermarkView.isUserInteractionEnabled = false
  52. return watermarkView
  53. }
  54. init(frame: CGRect, waterMarkText: String) {
  55. super.init(frame: frame)
  56. self.backgroundColor = .clear
  57. self.waterMarkText = waterMarkText
  58. self.setupUI()
  59. }
  60. required init?(coder: NSCoder) {
  61. fatalError("init(coder:) has not been implemented")
  62. }
  63. override func layoutSubviews() {
  64. super.layoutSubviews()
  65. self.tableView?.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height)
  66. }
  67. private func setupUI() {
  68. if self.tableView == nil {
  69. self.tableView = UITableView(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height), style: .grouped)
  70. self.addSubview(self.tableView!)
  71. self.tableView?.estimatedRowHeight = 0
  72. self.tableView?.estimatedSectionHeaderHeight = 0
  73. self.tableView?.estimatedSectionFooterHeight = 0
  74. self.tableView?.showsVerticalScrollIndicator = false
  75. self.tableView?.separatorStyle = .none
  76. self.tableView?.backgroundColor = .clear
  77. self.tableView?.isUserInteractionEnabled = false
  78. }
  79. self.tableView?.register(WaterMarkTableViewCell.self, forCellReuseIdentifier: cellIdentifier)
  80. self.tableView?.delegate = self
  81. self.tableView?.dataSource = self
  82. self.loadDataSource()
  83. DDLogDebug("setup ui .......\(self.tableView == nil)")
  84. }
  85. private func loadDataSource() {
  86. for i in 0...Int(Int(Float(SCREEN_HEIGHT))*3/WaterMarkView.lineHeight) {
  87. var str = ""
  88. for _ in 0...50 {
  89. str += " \(self.waterMarkText)"
  90. }
  91. if (i % 2 == 0) {
  92. self.dataSource.append(str)
  93. } else {
  94. self.dataSource.append(" \(str)")
  95. }
  96. }
  97. self.tableView?.reloadData()
  98. }
  99. }
  100. extension WaterMarkView: UITableViewDelegate, UITableViewDataSource {
  101. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  102. return self.dataSource.count
  103. }
  104. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  105. DDLogDebug("cell...........")
  106. if let c = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? WaterMarkTableViewCell {
  107. c.setText(text: self.dataSource[indexPath.row])
  108. c.selectionStyle = .none
  109. c.isUserInteractionEnabled = false
  110. return c
  111. }
  112. return UITableViewCell()
  113. }
  114. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  115. return CGFloat(WaterMarkView.lineHeight)
  116. }
  117. }