CSearchContentView.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // CSearchContentView.swift
  3. // ComPDFKit_Tools
  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. import ComPDFKit
  14. public class CSearchContentView: UIView {
  15. public weak var pdflistView: CPDFListView?
  16. private var replaceButton = UIButton()
  17. private var replaceBounds:CGRect = CGRect.zero
  18. private var orgSelection:CPDFSelection?
  19. public var callback: (() -> Void)?
  20. init(pdfView: CPDFListView) {
  21. self.pdflistView = pdfView
  22. super.init(frame: .zero)
  23. self.commonInit()
  24. }
  25. required init?(coder: NSCoder) {
  26. fatalError("init(coder:) has not been implemented")
  27. }
  28. open override func draw(_ rect: CGRect) {
  29. super.draw(rect)
  30. guard let context = UIGraphicsGetCurrentContext() else { return }
  31. let drawRect = replaceBounds
  32. if(self.replaceButton.isHidden == true) {
  33. return
  34. }
  35. var x = drawRect.origin.x + drawRect.size.width/2 - 40
  36. if(x < 0) {
  37. x = 0
  38. }
  39. var y = drawRect.origin.y - 45
  40. if(y < 0) {
  41. y = drawRect.origin.y + 10 + replaceBounds.size.height
  42. let buttonRect = CGRect(x: x, y: y, width: self.replaceButton.frame.size.width, height: self.replaceButton.frame.size.height)
  43. let offset = 10.0
  44. context.beginPath()
  45. context.move(to: CGPoint(x: buttonRect.midX + offset, y: buttonRect.minY))
  46. context.addLine(to: CGPoint(x: buttonRect.midX, y: buttonRect.minY - offset))
  47. context.addLine(to: CGPoint(x: buttonRect.midX - offset, y: buttonRect.minY))
  48. context.closePath()
  49. context.setFillColor(UIColor.init(red: 28.0/255.0, green: 28.0/255.0, blue: 28.0/255.0, alpha: 1.0).cgColor)
  50. context.fillPath()
  51. } else {
  52. let buttonRect = CGRect(x: x, y: y, width: self.replaceButton.frame.size.width, height: self.replaceButton.frame.size.height)
  53. let offset = 10.0
  54. context.beginPath()
  55. context.move(to: CGPoint(x: buttonRect.midX + offset, y: buttonRect.maxY))
  56. context.addLine(to: CGPoint(x: buttonRect.midX, y: buttonRect.maxY + offset))
  57. context.addLine(to: CGPoint(x: buttonRect.midX - offset, y: buttonRect.maxY))
  58. context.closePath()
  59. context.setFillColor(UIColor.init(red: 28.0/255.0, green: 28.0/255.0, blue: 28.0/255.0, alpha: 1.0).cgColor)
  60. context.fillPath()
  61. }
  62. }
  63. public override func layoutSubviews() {
  64. super.layoutSubviews()
  65. if(self.pdflistView != nil && self.superview != nil && self.orgSelection != nil) {
  66. let pageBounds = self.pdflistView!.convert(self.orgSelection!.bounds, from: self.orgSelection!.page)
  67. let pdfviewBounds = self.superview!.convert(pageBounds, from: self.pdflistView)
  68. let convertRect:CGRect = self.convert(pdfviewBounds, from: self.superview)
  69. var x = convertRect.origin.x + convertRect.size.width/2 - 40
  70. if(x < 0) {
  71. x = 0
  72. }
  73. var y = convertRect.origin.y - 35 - 10
  74. if(y < 0) {
  75. y = convertRect.origin.y + convertRect.size.height + 10
  76. }
  77. replaceBounds = convertRect
  78. replaceButton.frame = CGRect(x: x, y: y, width: 80, height: 35)
  79. setNeedsDisplay()
  80. }
  81. }
  82. open func updateSelection(_ selection: CPDFSelection?) {
  83. self.orgSelection = selection
  84. if(self.pdflistView != nil && self.superview != nil && self.orgSelection != nil) {
  85. replaceButton.isHidden = false
  86. let pageBounds = self.pdflistView!.convert(self.orgSelection!.bounds, from: self.orgSelection!.page)
  87. let pdfviewBounds = self.superview!.convert(pageBounds, from: self.pdflistView)
  88. let convertRect:CGRect = self.convert(pdfviewBounds, from: self.superview)
  89. var x = convertRect.origin.x + convertRect.size.width/2 - 40
  90. if(x < 0) {
  91. x = 0
  92. }
  93. var y = convertRect.origin.y - 35 - 10
  94. if(y < 0) {
  95. y = convertRect.origin.y + 10 + convertRect.size.height
  96. }
  97. replaceBounds = convertRect
  98. replaceButton.frame = CGRect(x: x, y: y, width: 80, height: 35)
  99. } else {
  100. replaceButton.isHidden = true
  101. replaceBounds = CGRect.zero
  102. }
  103. setNeedsDisplay()
  104. }
  105. func commonInit() {
  106. self.backgroundColor = UIColor.clear
  107. addSubview(replaceButton)
  108. replaceButton.backgroundColor = UIColor.init(red: 28.0/255.0, green: 28.0/255.0, blue: 28.0/255.0, alpha: 1.0)
  109. replaceButton.layer.cornerRadius = 5
  110. replaceButton.frame = CGRect(x: 0, y: 0, width: 80, height: 35)
  111. replaceButton.autoresizingMask = [.flexibleWidth,.flexibleHeight]
  112. replaceButton.setTitle(NSLocalizedString("Replace", comment: ""), for: .normal)
  113. replaceButton.titleLabel?.adjustsFontSizeToFitWidth = true
  114. replaceButton.setTitleColor(UIColor.white, for: .normal)
  115. replaceButton.addTarget(self, action: #selector(buttonItemClicked_Replace(_:)), for: .touchUpInside)
  116. }
  117. // MARK: - Action
  118. @objc func buttonItemClicked_Replace(_ sender: Any) {
  119. callback?()
  120. }
  121. }