CPageRangeSelectView.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // CPageRangeSelectView.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 CPageRangeSelectViewDelegate: AnyObject {
  14. @objc optional func pageRangeSelectView(_ pageRangeSelectView: CPageRangeSelectView, pageRange: String)
  15. }
  16. class CPageRangeSelectView: UIView {
  17. weak var delegate: CPageRangeSelectViewDelegate?
  18. var parentVC: UIViewController?
  19. private var titleLabel: UILabel?
  20. var pageRangeView: UIView?
  21. var pageRangeLabel: UILabel?
  22. var pageRangeButton: UIButton?
  23. var pageRangeIcon: UIImageView?
  24. // MARK: - Initializers
  25. override init(frame: CGRect) {
  26. super.init(frame: frame)
  27. titleLabel = UILabel()
  28. titleLabel?.autoresizingMask = .flexibleRightMargin
  29. titleLabel?.text = NSLocalizedString("Page Range", comment: "")
  30. titleLabel?.textColor = .gray
  31. titleLabel?.font = UIFont.systemFont(ofSize: 12.0)
  32. if titleLabel != nil {
  33. addSubview(titleLabel!)
  34. }
  35. pageRangeView = UIView()
  36. pageRangeView?.autoresizingMask = .flexibleLeftMargin
  37. pageRangeView?.layer.borderColor = UIColor.gray.cgColor
  38. pageRangeView?.layer.borderWidth = 0.5
  39. pageRangeView?.layer.cornerRadius = 5
  40. pageRangeView?.layer.masksToBounds = true
  41. if pageRangeView != nil {
  42. addSubview(pageRangeView!)
  43. }
  44. pageRangeLabel = UILabel()
  45. pageRangeLabel?.text = NSLocalizedString("All Pages", comment: "")
  46. pageRangeLabel?.font = UIFont.systemFont(ofSize: 14)
  47. pageRangeLabel?.textColor = CPDFColorUtils.CPageEditToolbarFontColor()
  48. pageRangeLabel?.textAlignment = .left
  49. if pageRangeLabel != nil {
  50. pageRangeView?.addSubview(pageRangeLabel!)
  51. }
  52. pageRangeIcon = UIImageView(image: UIImage(named: "CWatermarkPreImage", in: Bundle(for: self.classForCoder), compatibleWith: nil))
  53. if pageRangeIcon != nil {
  54. pageRangeView?.addSubview(pageRangeIcon!)
  55. }
  56. pageRangeButton = UIButton()
  57. pageRangeButton?.backgroundColor = .clear
  58. pageRangeButton?.addTarget(self, action: #selector(buttonItemClicked_range), for: .touchUpInside)
  59. if pageRangeButton != nil {
  60. pageRangeView?.addSubview(pageRangeButton!)
  61. }
  62. }
  63. override func layoutSubviews() {
  64. super.layoutSubviews()
  65. titleLabel?.frame = CGRect(x: 20, y: 0, width: 100, height: 30)
  66. pageRangeView?.frame = CGRect(x: bounds.size.width - 200, y: 0, width: 180, height: 30)
  67. pageRangeLabel?.frame = CGRect(x: 5, y: 0, width: 145, height: 30)
  68. pageRangeIcon?.frame = CGRect(x: 150, y: 5, width: 20, height: 20)
  69. pageRangeButton?.frame = pageRangeView?.bounds ?? .zero
  70. }
  71. required init?(coder: NSCoder) {
  72. fatalError("init(coder:) has not been implemented")
  73. }
  74. // MARK: - Action
  75. @objc func buttonItemClicked_range(_ sender: UIButton) {
  76. let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
  77. if UI_USER_INTERFACE_IDIOM() == .pad {
  78. alertController.popoverPresentationController?.sourceView = self.pageRangeButton
  79. alertController.popoverPresentationController?.sourceRect = ((self.pageRangeButton)?.bounds) ?? .zero
  80. }
  81. let defaultRange = UIAlertAction(title: NSLocalizedString("All Pages", comment: ""), style: .default) { (action) in
  82. self.pageRangeLabel?.text = NSLocalizedString("All Pages", comment: "")
  83. self.delegate?.pageRangeSelectView?(self, pageRange: "")
  84. }
  85. let customRange = UIAlertAction(title: NSLocalizedString("Current Page", comment: ""), style: .default) { (action) in
  86. self.pageRangeLabel?.text = NSLocalizedString("Current Page", comment: "")
  87. self.delegate?.pageRangeSelectView?(self, pageRange: "0")
  88. }
  89. let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .cancel) { (action) in
  90. }
  91. alertController.addAction(defaultRange)
  92. alertController.addAction(customRange)
  93. alertController.addAction(cancelAction)
  94. parentVC?.present(alertController, animated: true, completion: nil)
  95. }
  96. }