CPDFKeyboardToolbar.swift 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // CPDFKeyboardToolbar.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. public protocol CPDFKeyboardToolbarDelegate: AnyObject {
  14. func keyboardShouldDissmiss(_ toolbar: CPDFKeyboardToolbar)
  15. }
  16. public class CPDFKeyboardToolbar: UIView {
  17. public weak var delegate: CPDFKeyboardToolbarDelegate?
  18. var doneButton:UIButton?
  19. public override init(frame: CGRect) {
  20. super.init(frame: frame)
  21. self.doneButton = UIButton(type: .custom)
  22. self.doneButton?.setTitle(NSLocalizedString("Done", comment: ""), for: .normal)
  23. self.doneButton?.setTitleColor(UIColor(red: 0, green: 122.0/255.0, blue: 1.0, alpha: 1.0), for: .normal)
  24. self.doneButton?.setTitleColor(UIColor.lightGray, for: .highlighted)
  25. self.doneButton?.sizeToFit()
  26. self.doneButton?.addTarget(self, action: #selector(buttonItemClick_done(_:)), for: .touchUpInside)
  27. if doneButton != nil {
  28. self.addSubview(self.doneButton!)
  29. }
  30. self.backgroundColor = CPDFColorUtils.CPDFKeyboardToolbarColor()
  31. }
  32. public override func layoutSubviews() {
  33. if #available(iOS 11.0, *) {
  34. self.doneButton?.frame = CGRect(x: self.frame.size.width - (self.superview?.safeAreaInsets.right ?? 0) - 60, y: 0, width: 50, height: self.frame.size.height)
  35. } else {
  36. self.doneButton?.frame = CGRect(x: self.frame.size.width - 70, y: 0, width: 50, height: self.frame.size.height)
  37. }
  38. }
  39. required init?(coder: NSCoder) {
  40. fatalError("init(coder:) has not been implemented")
  41. }
  42. public func bindToTextView(_ textView: UITextView) {
  43. textView.inputAccessoryView = self
  44. }
  45. public func bindToTextField(_ textField: UITextField) {
  46. textField.inputAccessoryView = self
  47. }
  48. @objc func buttonItemClick_done(_ sender: Any) {
  49. self.delegate?.keyboardShouldDissmiss(self)
  50. }
  51. }