CPDFFontStyleTableView.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // CPDFFontStyleTableView.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. @objc protocol CPDFFontStyleTableViewDelegate: AnyObject {
  15. @objc optional func fontStyleTableView(_ fontStyleTableView: CPDFFontStyleTableView, fontName: String,isFontStyle:Bool)
  16. }
  17. class CPDFFontStyleTableView: UIView, UITableViewDelegate, UITableViewDataSource {
  18. weak var delegate: CPDFFontStyleTableViewDelegate?
  19. var backBtn:UIButton?
  20. var titleLabel:UILabel?
  21. var tableView:UITableView?
  22. var colorSlider:UIView?
  23. var headerView:UIView?
  24. var datasArray: [String] = []
  25. var isFontStyle = false
  26. var familyName:String = "Helvetica"
  27. var styleName:String = ""
  28. override init(frame: CGRect) {
  29. super.init(frame: frame)
  30. datasArray = CPDFFont.familyNames
  31. configSubView()
  32. }
  33. init(frame: CGRect,familyNames:String,styleName:String,isFontStyle:Bool) {
  34. super.init(frame: frame)
  35. self.isFontStyle = isFontStyle
  36. familyName = familyNames
  37. self.styleName = styleName
  38. if(isFontStyle) {
  39. datasArray = CPDFFont.fontNames(forFamilyName: familyNames)
  40. if(datasArray.count < 1) {
  41. datasArray.append("Regular")
  42. }
  43. } else {
  44. datasArray = CPDFFont.familyNames
  45. }
  46. if self.styleName.count < 1 {
  47. self.styleName = "Regular"
  48. }
  49. configSubView()
  50. if isFontStyle == false && datasArray.contains(familyNames) {
  51. guard let index = datasArray.firstIndex(of: familyNames) else { return }
  52. let indexPath = IndexPath(row: index, section: 0)
  53. tableView?.scrollToRow(at: indexPath, at: .top, animated: true)
  54. }
  55. }
  56. func configSubView () {
  57. headerView = UIView(frame: CGRect(x: 0, y: 0, width: bounds.size.width, height: 50))
  58. headerView?.layer.borderColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1).cgColor
  59. headerView?.layer.borderWidth = 1.0
  60. headerView?.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  61. addSubview(headerView!)
  62. titleLabel = UILabel(frame: CGRect(x: (frame.size.width - 120)/2, y: 0, width: 120, height: 50))
  63. titleLabel?.text = NSLocalizedString("Font Style", comment: "")
  64. if(isFontStyle) {
  65. titleLabel?.text = NSLocalizedString("Font Style", comment: "")
  66. } else {
  67. titleLabel?.text = NSLocalizedString("Font List", comment: "")
  68. }
  69. titleLabel?.textAlignment = .center
  70. headerView?.addSubview(titleLabel!)
  71. backBtn = UIButton(frame: CGRect(x: 10, y: 0, width: 40, height: 50))
  72. backBtn?.autoresizingMask = .flexibleHeight
  73. backBtn?.setImage(UIImage(named: "CPFFormBack", in: Bundle(for: type(of: self)), compatibleWith: nil), for: .normal)
  74. backBtn?.addTarget(self, action: #selector(buttonItemClicked_back(_:)), for: .touchUpInside)
  75. headerView?.addSubview(backBtn!)
  76. tableView = UITableView(frame: CGRect(x: 0, y: 50, width: bounds.size.width, height: bounds.size.height), style: .plain)
  77. tableView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  78. tableView?.delegate = self
  79. tableView?.dataSource = self
  80. tableView?.backgroundColor = CPDFColorUtils.CAnnotationSampleBackgoundColor()
  81. addSubview(tableView!)
  82. backgroundColor = CPDFColorUtils.CAnnotationSampleBackgoundColor()
  83. }
  84. required init?(coder: NSCoder) {
  85. fatalError("init(coder:) has not been implemented")
  86. }
  87. // MARK: - Action
  88. @objc func buttonItemClicked_back(_ button: UIButton) {
  89. if self.superview != nil {
  90. self.removeFromSuperview()
  91. }
  92. }
  93. // MARK: - UITableViewDataSource
  94. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  95. return datasArray.count
  96. }
  97. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  98. let cell = tableView.dequeueReusableCell(withIdentifier: "fontName") ?? UITableViewCell(style: .default, reuseIdentifier: "fontName")
  99. let fontName = datasArray[indexPath.row]
  100. if self.isFontStyle {
  101. let cFont = CPDFFont(familyName: familyName, fontStyle: fontName )
  102. var font = UIFont.init(name: CPDFFont.convertAppleFont(cFont) ?? "Helvetica", size: 16.0)
  103. if font == nil {
  104. font = UIFont(name: "Helvetica-Oblique", size: 16.0)
  105. }
  106. if(styleName == fontName) {
  107. cell.accessoryType = .checkmark
  108. } else {
  109. cell.accessoryType = .none
  110. }
  111. cell.textLabel?.font = font
  112. } else {
  113. let cFont = CPDFFont(familyName: fontName, fontStyle: "" )
  114. var font = UIFont.init(name: CPDFFont.convertAppleFont(cFont) ?? "Helvetica", size: 16.0)
  115. if font == nil {
  116. font = UIFont(name: "Helvetica-Oblique", size: 16.0)
  117. }
  118. if(familyName == fontName) {
  119. cell.accessoryType = .checkmark
  120. } else {
  121. cell.accessoryType = .none
  122. }
  123. cell.textLabel?.font = font
  124. }
  125. cell.textLabel?.text = fontName
  126. return cell
  127. }
  128. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  129. self.delegate?.fontStyleTableView?(self, fontName: datasArray[indexPath.row],isFontStyle: isFontStyle)
  130. if self.superview != nil {
  131. self.removeFromSuperview()
  132. }
  133. }
  134. }