CPDFFreeTextViewController.swift 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. //
  2. // CPDFFreeTextViewController.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 CPDFFreeTextViewControllerDelegate: AnyObject {
  15. @objc optional func freeTextViewController(_ freeTextViewController: CPDFFreeTextViewController, annotStyle: CAnnotStyle)
  16. }
  17. class CPDFFreeTextViewController: CPDFAnnotationBaseViewController, CPDFThicknessSliderViewDelegate, CPDFFontStyleTableViewDelegate {
  18. weak var delegate: CPDFFreeTextViewControllerDelegate?
  19. var pdfListView: CPDFListView?
  20. private var alignmentLabel: UILabel?
  21. private var leftBtn: UIButton?
  22. private var centerBtn: UIButton?
  23. private var rightBtn: UIButton?
  24. private var fontsizeSliderView: CPDFThicknessSliderView?
  25. private var baseName:String = "Helvetica"
  26. private var baseStyleName:String = ""
  27. private var fontStyleTableView: CPDFFontStyleTableView?
  28. private var dropMenuView: UIView?
  29. private var splitView: UIView?
  30. private var fontSelectBtn: UIButton?
  31. private var dropDownIcon: UIImageView?
  32. private var fontNameLabel: UILabel?
  33. private var fontNameSelectLabel: UILabel?
  34. private var dropStyleMenuView: UIView?
  35. private var splitStyleView: UIView?
  36. private var fontStyleSelectBtn: UIButton?
  37. private var dropStyleDownIcon: UIImageView?
  38. private var fontStyleNameLabel: UILabel?
  39. private var fontStyleNameSelectLabel: UILabel?
  40. // MARK: - Initializers
  41. override init(annotStyle: CAnnotStyle) {
  42. super.init(annotStyle: annotStyle)
  43. self.annotStyle = annotStyle
  44. }
  45. required init?(coder: NSCoder) {
  46. fatalError("init(coder:) has not been implemented")
  47. }
  48. // MARK: - ViewController Methods
  49. override func viewDidLoad() {
  50. super.viewDidLoad()
  51. self.fontNameLabel = UILabel()
  52. self.fontNameLabel?.text = NSLocalizedString("Font", comment: "")
  53. self.fontNameLabel?.font = UIFont.systemFont(ofSize: 12)
  54. self.fontNameLabel?.textColor = UIColor.gray
  55. if fontNameLabel != nil {
  56. self.scrcollView?.addSubview(self.fontNameLabel!)
  57. }
  58. self.dropMenuView = UIView()
  59. if dropMenuView != nil {
  60. self.scrcollView?.addSubview(self.dropMenuView!)
  61. }
  62. self.splitView = UIView()
  63. self.splitView?.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.2)
  64. if splitView != nil {
  65. self.dropMenuView?.addSubview(self.splitView!)
  66. }
  67. self.dropDownIcon = UIImageView()
  68. self.dropDownIcon?.image = UIImage(named: "CPDFEditArrow", in: Bundle(for: self.classForCoder), compatibleWith: nil)
  69. if dropDownIcon != nil {
  70. self.dropMenuView?.addSubview(self.dropDownIcon!)
  71. }
  72. self.fontNameSelectLabel = UILabel()
  73. self.fontNameSelectLabel?.font = UIFont.systemFont(ofSize: 12)
  74. if fontNameSelectLabel != nil {
  75. self.dropMenuView?.addSubview(self.fontNameSelectLabel!)
  76. }
  77. self.fontSelectBtn = UIButton(type: .custom)
  78. self.fontSelectBtn?.backgroundColor = UIColor.clear
  79. self.fontSelectBtn?.addTarget(self, action: #selector(buttonItemClicked_FontStyle), for: .touchUpInside)
  80. if fontSelectBtn != nil {
  81. self.dropMenuView?.addSubview(self.fontSelectBtn!)
  82. }
  83. self.dropStyleMenuView = UIView()
  84. if dropStyleMenuView != nil {
  85. self.scrcollView?.addSubview(self.dropStyleMenuView!)
  86. }
  87. self.splitStyleView = UIView()
  88. self.splitStyleView?.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.2)
  89. if splitView != nil {
  90. self.dropStyleMenuView?.addSubview(self.splitStyleView!)
  91. }
  92. self.dropStyleDownIcon = UIImageView()
  93. self.dropStyleDownIcon?.image = UIImage(named: "CPDFEditArrow", in: Bundle(for: self.classForCoder), compatibleWith: nil)
  94. if dropStyleDownIcon != nil {
  95. self.dropStyleMenuView?.addSubview(self.dropStyleDownIcon!)
  96. }
  97. self.fontStyleNameSelectLabel = UILabel()
  98. self.fontStyleNameSelectLabel?.font = UIFont.systemFont(ofSize: 12)
  99. if fontStyleNameSelectLabel != nil {
  100. self.dropStyleMenuView?.addSubview(self.fontStyleNameSelectLabel!)
  101. }
  102. self.fontStyleSelectBtn = UIButton(type: .custom)
  103. self.fontStyleSelectBtn?.backgroundColor = UIColor.clear
  104. self.fontStyleSelectBtn?.addTarget(self, action: #selector(buttonItemClicked_StyleName), for: .touchUpInside)
  105. if fontStyleSelectBtn != nil {
  106. self.dropStyleMenuView?.addSubview(self.fontStyleSelectBtn!)
  107. }
  108. self.alignmentLabel = UILabel()
  109. self.alignmentLabel?.text = NSLocalizedString("Alignment", comment: "")
  110. self.alignmentLabel?.textColor = UIColor.gray
  111. self.alignmentLabel?.font = UIFont.systemFont(ofSize: 12.0)
  112. if alignmentLabel != nil {
  113. self.scrcollView?.addSubview(self.alignmentLabel!)
  114. }
  115. self.leftBtn = UIButton()
  116. self.leftBtn?.setImage(UIImage(named: "CPDFFreeTextImageLeft", in: Bundle(for: self.classForCoder), compatibleWith: nil), for: .normal)
  117. self.leftBtn?.addTarget(self, action: #selector(buttonItemClicked_Left), for: .touchUpInside)
  118. if leftBtn != nil {
  119. self.scrcollView?.addSubview(self.leftBtn!)
  120. }
  121. self.centerBtn = UIButton()
  122. self.centerBtn?.setImage(UIImage(named: "CPDFFreeTextImageCenter", in: Bundle(for: self.classForCoder), compatibleWith: nil), for: .normal)
  123. self.centerBtn?.addTarget(self, action: #selector(buttonItemClicked_Center), for: .touchUpInside)
  124. self.scrcollView?.addSubview(self.centerBtn!)
  125. self.rightBtn = UIButton()
  126. self.rightBtn?.setImage(UIImage(named: "CPDFFreeTextImageRight", in: Bundle(for: self.classForCoder), compatibleWith: nil), for: .normal)
  127. self.rightBtn?.addTarget(self, action: #selector(buttonItemClicked_Right), for: .touchUpInside)
  128. if rightBtn != nil {
  129. self.scrcollView?.addSubview(self.rightBtn!)
  130. }
  131. self.fontsizeSliderView = CPDFThicknessSliderView()
  132. self.fontsizeSliderView?.thicknessSlider?.value = 20
  133. self.fontsizeSliderView?.thicknessSlider?.minimumValue = 1
  134. self.fontsizeSliderView?.thicknessSlider?.maximumValue = 100
  135. self.fontsizeSliderView?.titleLabel?.text = NSLocalizedString("Font Size", comment: "")
  136. self.fontsizeSliderView?.startLabel?.text = "1"
  137. self.fontsizeSliderView?.delegate = self
  138. self.fontsizeSliderView?.autoresizingMask = .flexibleWidth
  139. if fontsizeSliderView != nil {
  140. self.scrcollView?.addSubview(self.fontsizeSliderView!)
  141. }
  142. self.view.backgroundColor = CPDFColorUtils.CAnnotationSampleBackgoundColor()
  143. }
  144. override func viewWillLayoutSubviews() {
  145. super.viewWillLayoutSubviews()
  146. self.scrcollView?.frame = CGRect(x: 0, y: 170, width: self.view.frame.size.width, height: self.view.frame.size.height-170)
  147. self.scrcollView?.contentSize = CGSize(width: self.view.frame.size.width, height: 470)
  148. if #available(iOS 11.0, *) {
  149. self.backBtn?.frame = CGRect(x: self.view.frame.size.width - 60 - self.view.safeAreaInsets.right, y: 5, width: 50, height: 50)
  150. self.fontNameLabel?.frame = CGRect(x: self.view.safeAreaInsets.left+20, y: 195, width: 30, height: 30)
  151. self.dropMenuView?.frame = CGRect(x: self.view.safeAreaInsets.left+60, y: 195, width: (self.view.frame.size.width - 60 - self.view.safeAreaInsets.right-self.view.safeAreaInsets.left - 10)/5 * 3, height: 30)
  152. self.dropDownIcon?.frame = CGRect(x: (self.dropMenuView?.bounds.size.width ?? 0) - 24 - 5, y: 3, width: 24, height: 24)
  153. self.fontNameSelectLabel?.frame = CGRect(x: 10, y: 0, width: (self.dropMenuView?.bounds.size.width ?? 0) - 40, height: 29)
  154. self.fontSelectBtn?.frame = self.dropMenuView?.bounds ?? CGRect.zero
  155. self.splitView?.frame = CGRect(x: 0, y: 29, width: self.dropMenuView?.bounds.size.width ?? 0, height: 1)
  156. self.dropStyleMenuView?.frame = CGRect(x: (self.dropMenuView?.frame.maxX ?? 0) + 10, y: self.dropMenuView?.frame.minY ?? 0, width:(self.view.frame.size.width - 60 - self.view.safeAreaInsets.right-self.view.safeAreaInsets.left - 10)/5 * 2, height: self.dropMenuView?.frame.size.height ?? 0)
  157. self.dropStyleDownIcon?.frame = CGRect(x: (self.dropStyleMenuView?.bounds.size.width ?? 0) - 24 - 5, y: 3, width: 24, height: 24)
  158. self.fontStyleNameSelectLabel?.frame = CGRect(x: 10, y: 0, width: (self.dropStyleMenuView?.bounds.size.width ?? 0) - 40, height: 29)
  159. self.fontStyleSelectBtn?.frame = self.dropStyleMenuView?.bounds ?? CGRect.zero
  160. self.splitStyleView?.frame = CGRect(x: 0, y: 29, width: (self.dropStyleMenuView?.bounds.size.width ?? 0) - 24 - 5, height: 1)
  161. self.alignmentLabel?.frame = CGRect(x: self.view.safeAreaInsets.left+20, y: 225, width: 120, height: 45)
  162. self.leftBtn?.frame = CGRect(x: self.view.frame.size.width - 130 - self.view.safeAreaInsets.right, y: 240, width: 30, height: 30)
  163. self.centerBtn?.frame = CGRect(x: self.view.frame.size.width - 90 - self.view.safeAreaInsets.right, y: 240, width: 30, height: 30)
  164. self.rightBtn?.frame = CGRect(x: self.view.frame.size.width - 50, y: 240, width: 30, height: 30)
  165. self.fontsizeSliderView?.frame = CGRect(x: self.view.safeAreaInsets.left, y: 280, width: self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, height: 90)
  166. } else {
  167. self.backBtn?.frame = CGRect(x: self.view.frame.size.width - 60, y: 5, width: 50, height: 50)
  168. self.alignmentLabel?.frame = CGRect(x: 20, y: 225, width: 120, height: 45)
  169. self.leftBtn?.frame = CGRect(x: self.view.frame.size.width - 110, y: 240, width: 30, height: 30)
  170. self.centerBtn?.frame = CGRect(x: self.view.frame.size.width - 80, y: 240, width: 30, height: 30)
  171. self.rightBtn?.frame = CGRect(x: self.view.frame.size.width - 50, y: 240, width: 30, height: 30)
  172. self.fontsizeSliderView?.frame = CGRect(x: 0, y: 280, width: self.view.frame.size.width, height: 90)
  173. self.fontNameLabel?.frame = CGRect(x: 20, y: 195, width: 30, height: 30)
  174. self.dropMenuView?.frame = CGRect(x: 60, y: 195, width: (self.view.frame.size.width - 60 - 10)/5 * 3, height: 30)
  175. self.dropDownIcon?.frame = CGRect(x: (self.dropMenuView?.bounds.size.width ?? 0) - 24 - 5, y: 3, width: 24, height: 24)
  176. self.fontNameSelectLabel?.frame = CGRect(x: 10, y: 0, width: (self.dropMenuView?.bounds.size.width ?? 0)-40, height: 29)
  177. self.fontSelectBtn?.frame = self.dropMenuView?.bounds ?? CGRect.zero
  178. self.splitView?.frame = CGRect(x: 0, y: 29, width: (self.dropStyleMenuView?.bounds.size.width ?? 0) - 24 - 5, height: 1)
  179. self.dropStyleMenuView?.frame = CGRect(x: 60, y: 195, width: (self.view.frame.size.width - 60 - 10)/5 * 2, height: 30)
  180. self.dropStyleDownIcon?.frame = CGRect(x: (self.dropStyleMenuView?.bounds.size.width ?? 0) - 24 - 5, y: 3, width: 24, height: 24)
  181. self.fontStyleNameSelectLabel?.frame = CGRect(x: 10, y: 0, width: (self.dropStyleMenuView?.bounds.size.width ?? 0)-40, height: 29)
  182. self.fontStyleSelectBtn?.frame = self.dropStyleMenuView?.bounds ?? CGRect.zero
  183. self.splitStyleView?.frame = CGRect(x: 0, y: 29, width: self.dropStyleMenuView?.bounds.size.width ?? 0, height: 1)
  184. }
  185. }
  186. // MARK: - Protect Mehtods
  187. override func commomInitTitle() {
  188. self.titleLabel?.text = NSLocalizedString("FreeText", comment: "")
  189. self.colorView?.colorLabel?.text = NSLocalizedString("Font Color", comment: "")
  190. self.colorView?.selectedColor = self.annotStyle?.fontColor ?? UIColor.clear
  191. self.sampleView?.selecIndex = .freeText
  192. self.sampleView?.color = UIColor.blue
  193. }
  194. override func updatePreferredContentSizeWithTraitCollection(traitCollection: UITraitCollection) {
  195. self.preferredContentSize = CGSize(width: self.view.bounds.size.width, height: traitCollection.verticalSizeClass == .compact ? 350 : 600)
  196. }
  197. override func commomInitFromAnnotStyle() {
  198. self.opacitySliderView?.opacitySlider?.value = Float(self.annotStyle?.opacity ?? 0)
  199. self.opacitySliderView?.startLabel?.text = "\(Int(((self.opacitySliderView?.opacitySlider?.value ?? 0)/1)*100))%"
  200. self.fontsizeSliderView?.thicknessSlider?.value = Float(self.annotStyle?.fontSize ?? 0)
  201. self.fontsizeSliderView?.startLabel?.text = "\(Int(self.annotStyle?.fontSize ?? 0))"
  202. let cFont = self.annotStyle?.newCFont
  203. let familyName = cFont?.familyName ?? "Helvetica"
  204. var styleName = cFont?.styleName
  205. if(styleName?.count == 0) {
  206. let datasArray:[String] = CPDFFont.fontNames(forFamilyName: familyName)
  207. styleName = datasArray.first ?? ""
  208. }
  209. self.fontNameSelectLabel?.text = familyName
  210. self.fontStyleNameSelectLabel?.text = styleName
  211. baseName = familyName
  212. baseStyleName = styleName ?? ""
  213. self.analyzeAlignment(self.annotStyle?.alignment ?? .left)
  214. self.sampleView?.color = self.annotStyle?.fontColor ?? UIColor.clear
  215. self.sampleView?.opcity = self.annotStyle?.opacity ?? 0
  216. self.sampleView?.thickness = self.annotStyle?.fontSize ?? 0
  217. self.sampleView?.familyName = baseName
  218. self.sampleView?.styleName = baseStyleName
  219. self.sampleView?.textAlignment = self.annotStyle?.alignment ?? .left
  220. self.sampleView?.setNeedsDisplay()
  221. }
  222. // MARK: - Private Mehtods
  223. func analyzeAlignment(_ alignment: NSTextAlignment) {
  224. switch alignment {
  225. case .left:
  226. self.leftBtn?.backgroundColor = CPDFColorUtils.CAnnotationBarSelectBackgroundColor()
  227. case .center:
  228. self.centerBtn?.backgroundColor = CPDFColorUtils.CAnnotationBarSelectBackgroundColor()
  229. case .right:
  230. self.rightBtn?.backgroundColor = CPDFColorUtils.CAnnotationBarSelectBackgroundColor()
  231. default:
  232. break
  233. }
  234. }
  235. // MARK: - Action
  236. @objc func buttonItemClicked_FontStyle(sender: AnyObject) {
  237. fontStyleTableView = CPDFFontStyleTableView(frame: self.view.bounds, familyNames: baseName,styleName: baseStyleName, isFontStyle: false)
  238. fontStyleTableView?.delegate = self
  239. fontStyleTableView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  240. fontStyleTableView?.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  241. if(fontStyleTableView != nil) {
  242. self.view.addSubview(fontStyleTableView!)
  243. }
  244. }
  245. @objc func buttonItemClicked_StyleName(sender: AnyObject) {
  246. fontStyleTableView = CPDFFontStyleTableView(frame: self.view.bounds, familyNames: baseName,styleName: baseStyleName, isFontStyle: true)
  247. fontStyleTableView?.delegate = self
  248. fontStyleTableView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  249. fontStyleTableView?.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  250. if(fontStyleTableView != nil) {
  251. self.view.addSubview(fontStyleTableView!)
  252. }
  253. }
  254. @objc func buttonItemClicked_Left(sender: AnyObject) {
  255. leftBtn?.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  256. centerBtn?.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  257. rightBtn?.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  258. leftBtn?.backgroundColor = CPDFColorUtils.CAnnotationBarSelectBackgroundColor()
  259. annotStyle?.setAlignment(NSTextAlignment.left)
  260. sampleView?.textAlignment = NSTextAlignment.left
  261. if annotStyle != nil {
  262. delegate?.freeTextViewController?(self, annotStyle: annotStyle!)
  263. }
  264. sampleView?.setNeedsDisplay()
  265. }
  266. @objc func buttonItemClicked_Center(sender: AnyObject) {
  267. leftBtn?.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  268. centerBtn?.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  269. rightBtn?.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  270. centerBtn?.backgroundColor = CPDFColorUtils.CAnnotationBarSelectBackgroundColor()
  271. annotStyle?.setAlignment(NSTextAlignment.center)
  272. sampleView?.textAlignment = NSTextAlignment.center
  273. if annotStyle != nil {
  274. delegate?.freeTextViewController?(self, annotStyle: annotStyle!)
  275. }
  276. sampleView?.setNeedsDisplay()
  277. }
  278. @objc func buttonItemClicked_Right(sender: AnyObject) {
  279. leftBtn?.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  280. centerBtn?.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  281. rightBtn?.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  282. rightBtn?.backgroundColor = CPDFColorUtils.CAnnotationBarSelectBackgroundColor()
  283. annotStyle?.setAlignment(NSTextAlignment.right)
  284. sampleView?.textAlignment = NSTextAlignment.right
  285. if annotStyle != nil {
  286. delegate?.freeTextViewController?(self, annotStyle: annotStyle!)
  287. }
  288. sampleView?.setNeedsDisplay()
  289. }
  290. // MARK: - CPDFOpacitySliderViewDelegate
  291. override func opacitySliderView(_ opacitySliderView: CPDFOpacitySliderView, opacity: CGFloat) {
  292. sampleView?.opcity = opacity
  293. annotStyle?.setOpacity(opacity)
  294. annotStyle?.setInteriorOpacity(opacity)
  295. if annotStyle != nil {
  296. delegate?.freeTextViewController?(self, annotStyle: annotStyle!)
  297. }
  298. sampleView?.setNeedsDisplay()
  299. }
  300. // MARK: - CPDFThicknessSliderViewDelegate
  301. func thicknessSliderView(_ opacitySliderView: CPDFThicknessSliderView, thickness: CGFloat) {
  302. sampleView?.thickness = thickness
  303. annotStyle?.setFontSize(thickness)
  304. if annotStyle != nil {
  305. delegate?.freeTextViewController?(self, annotStyle: annotStyle!)
  306. }
  307. sampleView?.setNeedsDisplay()
  308. }
  309. // MARK: - CPDFFontStyleTableViewDelegate
  310. func fontStyleTableView(_ fontStyleTableView: CPDFFontStyleTableView, fontName: String, isFontStyle: Bool) {
  311. var styleName = baseStyleName
  312. var familyName = baseName
  313. if(isFontStyle) {
  314. styleName = fontName
  315. baseStyleName = fontName
  316. } else {
  317. baseName = fontName;
  318. familyName = fontName
  319. let datasArray:[String] = CPDFFont.fontNames(forFamilyName: familyName ?? "Helvetica")
  320. styleName = datasArray.first ?? ""
  321. }
  322. let pdfFont = CPDFFont.init(familyName: familyName ?? "Helvetica", fontStyle: styleName)
  323. self.sampleView?.familyName = pdfFont.familyName
  324. self.sampleView?.styleName = pdfFont.styleName ?? ""
  325. self.annotStyle?.setNewCFont(pdfFont)
  326. self.fontNameSelectLabel?.text = pdfFont.familyName
  327. self.fontStyleNameSelectLabel?.text = pdfFont.styleName
  328. self.sampleView?.setNeedsDisplay()
  329. if annotStyle != nil {
  330. delegate?.freeTextViewController?(self, annotStyle: annotStyle!)
  331. }
  332. }
  333. // MARK: - CPDFColorSelectViewDelegate
  334. override func selectColorView(_ select: CPDFColorSelectView) {
  335. if select == self.colorView {
  336. if #available(iOS 14.0, *) {
  337. let picker = UIColorPickerViewController()
  338. picker.delegate = self
  339. self.present(picker, animated: true, completion: nil)
  340. } else {
  341. let currentDevice = UIDevice.current
  342. self.colorPicker? = CPDFColorPickerView(frame: self.view.frame)
  343. self.colorPicker?.delegate = self
  344. self.colorPicker?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  345. self.colorPicker?.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  346. if colorPicker != nil {
  347. self.view.addSubview(self.colorPicker!)
  348. }
  349. self.updatePreferredContentSizeWithTraitCollection(traitCollection: self.traitCollection)
  350. }
  351. }
  352. }
  353. override func selectColorView(_ select: CPDFColorSelectView, color: UIColor) {
  354. sampleView?.color = color
  355. annotStyle?.setFontColor(color)
  356. sampleView?.setNeedsDisplay()
  357. if annotStyle != nil {
  358. delegate?.freeTextViewController?(self, annotStyle: self.annotStyle!)
  359. }
  360. }
  361. // MARK: - CPDFColorPickerViewDelegate
  362. override func pickerView(_ colorPickerView: CPDFColorPickerView, color: UIColor) {
  363. self.sampleView?.color = color
  364. annotStyle?.setFontColor(color)
  365. sampleView?.setNeedsDisplay()
  366. if annotStyle != nil {
  367. delegate?.freeTextViewController?(self, annotStyle: self.annotStyle!)
  368. }
  369. var red: CGFloat = 0
  370. var green: CGFloat = 0
  371. var blue: CGFloat = 0
  372. var alpha: CGFloat = 0
  373. color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  374. self.opacitySliderView?.opacitySlider?.value = Float(alpha)
  375. self.opacitySliderView?.startLabel?.text = "\(Int(((self.opacitySliderView?.opacitySlider?.value ?? 0)/1)*100))%"
  376. self.updatePreferredContentSizeWithTraitCollection(traitCollection: traitCollection)
  377. }
  378. // MARK: - UIColorPickerViewControllerDelegate
  379. @available(iOS 14.0, *)
  380. func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) {
  381. let color = viewController.selectedColor
  382. self.sampleView?.color = color;
  383. annotStyle?.setFontColor(color)
  384. sampleView?.setNeedsDisplay()
  385. if annotStyle != nil {
  386. delegate?.freeTextViewController?(self, annotStyle: self.annotStyle!)
  387. }
  388. var red: CGFloat = 0
  389. var green: CGFloat = 0
  390. var blue: CGFloat = 0
  391. var alpha: CGFloat = 0
  392. viewController.selectedColor.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  393. self.opacitySliderView?.opacitySlider?.value = Float(alpha)
  394. self.opacitySliderView?.startLabel?.text = "\(Int(((self.opacitySliderView?.opacitySlider?.value ?? 0) / 1) * 100))%"
  395. updatePreferredContentSizeWithTraitCollection(traitCollection: traitCollection)
  396. }
  397. }