CTextWatermarkPreView.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // CWatermarkPreView.swift
  3. // PDFViewer-Swift
  4. //
  5. // Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. // This notice may not be removed from this file.
  11. //
  12. import UIKit
  13. @objc protocol CTextWatermarkPreViewDelegate: AnyObject {
  14. @objc optional func textWatermarkPreViewRotate(_ textWatermarkPreView: CTextWatermarkPreView)
  15. }
  16. class CTextWatermarkPreView: UIView {
  17. var documentSize: CGSize?
  18. var documentView: UIImageView?
  19. var textTileView: CTileWatermarkPreView?
  20. var preLabel: UILabel?
  21. var rotationBtn: UIButton?
  22. // MARK: - Action
  23. init(frame: CGRect, Image image: UIImage?) {
  24. super.init(frame: frame)
  25. documentView = UIImageView(image: image)
  26. if documentView != nil {
  27. addSubview(documentView!)
  28. }
  29. textTileView = CTileWatermarkPreView(frame: .zero)
  30. textTileView?.backgroundColor = .clear
  31. textTileView?.isUserInteractionEnabled = false
  32. textTileView?.isHidden = true
  33. if textTileView != nil {
  34. documentView?.addSubview(textTileView!)
  35. }
  36. rotationBtn = UIButton(type: .custom)
  37. rotationBtn?.translatesAutoresizingMaskIntoConstraints = false
  38. rotationBtn?.setImage(UIImage(named: "CWatermarkPreViewRatoteIamge", in: Bundle(for: self.classForCoder), compatibleWith: nil), for: .normal)
  39. rotationBtn?.addTarget(self, action: #selector(buttonItemClicked_rotate), for: .touchUpInside)
  40. preLabel = UILabel()
  41. preLabel?.textAlignment = .center
  42. preLabel?.layer.borderWidth = 1.0
  43. preLabel?.layer.borderColor = UIColor.blue.cgColor
  44. preLabel?.font = UIFont.systemFont(ofSize: 24)
  45. preLabel?.text = NSLocalizedString("Watermark", comment: "")
  46. preLabel?.textColor = .black
  47. preLabel?.sizeToFit()
  48. preLabel?.backgroundColor = UIColor.white
  49. textTileView?.waterString = preLabel?.text
  50. textTileView?.setNeedsDisplay()
  51. if preLabel != nil {
  52. documentView?.addSubview(preLabel!)
  53. }
  54. if rotationBtn != nil {
  55. documentView?.addSubview(rotationBtn!)
  56. }
  57. backgroundColor = CPDFColorUtils.CAnnotationSampleBackgoundColor()
  58. }
  59. required init?(coder: NSCoder) {
  60. fatalError("init(coder:) has not been implemented")
  61. }
  62. override func layoutSubviews() {
  63. super.layoutSubviews()
  64. let width = (bounds.size.width - (documentSize?.width ?? 0))/2
  65. let height = (bounds.size.height - (documentSize?.height ?? 0))/2
  66. documentView?.frame = CGRect(x: width, y: height, width: documentSize?.width ?? 0, height: (documentSize?.height ?? 0) - 20)
  67. let x = Int((documentView?.bounds.width ?? 100))/2
  68. let y = Int((documentView?.bounds.width ?? 50))/4
  69. preLabel?.center = CGPoint(x: x, y: y)
  70. rotationBtn?.size = CGSize(width: 20, height: 20)
  71. textTileView?.frame = documentView?.bounds ?? .zero
  72. textTileView?.centerPoint = preLabel?.center ?? .zero
  73. textTileView?.setNeedsDisplay()
  74. }
  75. override func draw(_ rect: CGRect) {
  76. super.draw(rect)
  77. rotationBtn?.center = leftBottom(view: preLabel!)
  78. }
  79. // MARK: - Action
  80. @objc func buttonItemClicked_rotate(_ sender: UIButton) {
  81. }
  82. func leftBottom(view: UIView) -> CGPoint {
  83. let transform = view.transform
  84. let scale = sqrt(transform.a * transform.a + transform.c * transform.c) // scaleX
  85. let angle = atan2(transform.b, transform.a)
  86. let originVector = CGPoint(x: view.bounds.size.width / 2, y: view.bounds.size.height / 2)
  87. let rotateVector = CGPoint(x: originVector.x * cos(angle) + originVector.y * sin(angle), y: originVector.y * cos(angle) - originVector.x * sin(angle))
  88. let scaleVector = CGPoint(x: rotateVector.x * scale, y: rotateVector.y * scale)
  89. return CGPoint(x: view.center.x - scaleVector.x, y: view.center.y + scaleVector.y)
  90. }
  91. }