CAnnotStyle.swift 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. //
  2. // CAnnotStyle.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 CAnnotStyle: NSObject {
  15. var isSelectAnnot:Bool = false
  16. var annotations:[CPDFAnnotation] = []
  17. var annotMode:CPDFViewAnnotationMode = .CPDFViewAnnotationModenone
  18. var headKeys:[String] = []
  19. var trialKeys:[String] = []
  20. init(annotionMode: CPDFViewAnnotationMode, annotations: [CPDFAnnotation]?) {
  21. super.init()
  22. if annotations?.count ?? 0 > 0 {
  23. isSelectAnnot = true
  24. self.annotations = annotations ?? []
  25. annotMode = convertAnnotationType()
  26. } else {
  27. isSelectAnnot = false
  28. annotMode = annotionMode
  29. }
  30. }
  31. var annotation: CPDFAnnotation? {
  32. return annotations.first
  33. }
  34. func convertAnnotationType() -> CPDFViewAnnotationMode {
  35. var annotationType: CPDFViewAnnotationMode = .CPDFViewAnnotationModenone
  36. if(annotation != nil) {
  37. if annotation is CPDFFreeTextAnnotation {
  38. annotationType = .freeText
  39. } else if annotation is CPDFTextAnnotation {
  40. annotationType = .note
  41. } else if annotation is CPDFCircleAnnotation {
  42. annotationType = .circle
  43. } else if annotation is CPDFSquareAnnotation {
  44. annotationType = .square
  45. } else if annotation is CPDFMarkupAnnotation {
  46. let markupAnnotation:CPDFMarkupAnnotation = annotation as? CPDFMarkupAnnotation ?? CPDFMarkupAnnotation()
  47. if markupAnnotation.markupType() == .highlight {
  48. annotationType = .highlight
  49. } else if markupAnnotation.markupType() == .strikeOut {
  50. annotationType = .strikeout
  51. } else if markupAnnotation.markupType() == .underline {
  52. annotationType = .underline
  53. } else if markupAnnotation.markupType() == .squiggly {
  54. annotationType = .squiggly
  55. }
  56. } else if annotation is CPDFLineAnnotation {
  57. let lineAnnotation:CPDFLineAnnotation = annotation as? CPDFLineAnnotation ?? CPDFLineAnnotation()
  58. if lineAnnotation.endLineStyle == .none && lineAnnotation.startLineStyle == .none {
  59. annotationType = .line
  60. } else {
  61. annotationType = .arrow
  62. }
  63. } else if annotation is CPDFInkAnnotation {
  64. annotationType = .ink
  65. } else if annotation is CPDFLinkAnnotation {
  66. annotationType = .link
  67. } else if annotation is CPDFSignatureAnnotation {
  68. annotationType = .signature
  69. } else if annotation is CPDFStampAnnotation {
  70. annotationType = .stamp
  71. } else if annotation is CPDFSoundAnnotation {
  72. annotationType = .sound
  73. } else if annotation is CPDFTextWidgetAnnotation {
  74. annotationType = .formModeText
  75. } else if annotation is CPDFButtonWidgetAnnotation {
  76. let buttonWidgetAnnotation:CPDFButtonWidgetAnnotation = annotation as? CPDFButtonWidgetAnnotation ?? CPDFButtonWidgetAnnotation()
  77. if buttonWidgetAnnotation.controlType() == .checkBoxControl {
  78. annotationType = .formModeCheckBox
  79. } else if buttonWidgetAnnotation.controlType() == .radioButtonControl {
  80. annotationType = .formModeRadioButton
  81. } else if buttonWidgetAnnotation.controlType() == .pushButtonControl {
  82. annotationType = .formModeButton
  83. }
  84. } else if annotation is CPDFChoiceWidgetAnnotation {
  85. let choiceWidgetAnnotation:CPDFChoiceWidgetAnnotation = annotation as? CPDFChoiceWidgetAnnotation ?? CPDFChoiceWidgetAnnotation()
  86. if choiceWidgetAnnotation.isListChoice {
  87. annotationType = .formModeList
  88. } else {
  89. annotationType = .formModeCombox
  90. }
  91. }
  92. }
  93. return annotationType
  94. }
  95. // MARK: - Common
  96. var color: UIColor? {
  97. if isSelectAnnot {
  98. return annotation?.color
  99. } else {
  100. let userDefaults = UserDefaults.standard
  101. switch annotMode {
  102. case .note:
  103. return userDefaults.PDFListViewColorForKey(CAnchoredNoteColorKey)
  104. case .circle:
  105. return userDefaults.PDFListViewColorForKey(CCircleNoteColorKey)
  106. case .square:
  107. return userDefaults.PDFListViewColorForKey(CSquareNoteColorKey)
  108. case .highlight:
  109. return userDefaults.PDFListViewColorForKey(CHighlightNoteColorKey)
  110. case .underline:
  111. return userDefaults.PDFListViewColorForKey(CUnderlineNoteColorKey)
  112. case .strikeout:
  113. return userDefaults.PDFListViewColorForKey(CStrikeOutNoteColorKey)
  114. case .squiggly:
  115. return userDefaults.PDFListViewColorForKey(CSquigglyNoteColorKey)
  116. case .line:
  117. return userDefaults.PDFListViewColorForKey(CLineNoteColorKey)
  118. case .arrow:
  119. return userDefaults.PDFListViewColorForKey(CArrowNoteColorKey)
  120. case .ink:
  121. return CPDFKitConfig.sharedInstance().freehandAnnotationColor()
  122. default:
  123. return nil
  124. }
  125. }
  126. }
  127. func setColor(_ color: UIColor?) {
  128. if isSelectAnnot {
  129. annotations.forEach { annotation in
  130. annotation.color = color
  131. }
  132. } else {
  133. let userDefaults = UserDefaults.standard
  134. switch annotMode {
  135. case .note:
  136. userDefaults.setPDFListViewColor(color, forKey: CAnchoredNoteColorKey)
  137. case .circle:
  138. userDefaults.setPDFListViewColor(color, forKey: CCircleNoteColorKey)
  139. case .square:
  140. userDefaults.setPDFListViewColor(color, forKey: CSquareNoteColorKey)
  141. case .highlight:
  142. userDefaults.setPDFListViewColor(color, forKey: CHighlightNoteColorKey)
  143. case .underline:
  144. userDefaults.setPDFListViewColor(color, forKey: CUnderlineNoteColorKey)
  145. case .strikeout:
  146. userDefaults.setPDFListViewColor(color, forKey: CStrikeOutNoteColorKey)
  147. case .squiggly:
  148. userDefaults.setPDFListViewColor(color, forKey: CSquigglyNoteColorKey)
  149. case .line:
  150. userDefaults.setPDFListViewColor(color, forKey: CLineNoteColorKey)
  151. case .arrow:
  152. userDefaults.setPDFListViewColor(color, forKey: CArrowNoteColorKey)
  153. case .ink:
  154. CPDFKitConfig.sharedInstance().setFreehandAnnotationColor(color)
  155. userDefaults.setPDFListViewColor(color, forKey: CInkNoteColorKey)
  156. default:
  157. break
  158. }
  159. userDefaults.synchronize()
  160. }
  161. }
  162. var opacity: CGFloat {
  163. var opacity: CGFloat = 0
  164. if isSelectAnnot {
  165. opacity = annotation?.opacity ?? 1.0
  166. } else {
  167. let userDefaults = UserDefaults.standard
  168. switch annotMode {
  169. case .freeText:
  170. opacity = CGFloat(userDefaults.float(forKey: CFreeTextNoteOpacityKey))
  171. case .note:
  172. opacity = CGFloat(userDefaults.float(forKey: CAnchoredNoteOpacityKey))
  173. case .circle:
  174. opacity = CGFloat(userDefaults.float(forKey: CCircleNoteOpacityKey))
  175. case .square:
  176. opacity = CGFloat(userDefaults.float(forKey: CSquareNoteOpacityKey))
  177. case .highlight:
  178. opacity = CGFloat(userDefaults.float(forKey: CHighlightNoteOpacityKey))
  179. case .underline:
  180. opacity = CGFloat(userDefaults.float(forKey: CUnderlineNoteOpacityKey))
  181. case .strikeout:
  182. opacity = CGFloat(userDefaults.float(forKey: CStrikeOutNoteOpacityKey))
  183. case .squiggly:
  184. opacity = CGFloat(userDefaults.float(forKey: CSquigglyNoteOpacityKey))
  185. case .line:
  186. opacity = CGFloat(userDefaults.float(forKey: CLineNoteOpacityKey))
  187. case .arrow:
  188. opacity = CGFloat(userDefaults.float(forKey: CArrowNoteOpacityKey))
  189. case .ink:
  190. opacity = CPDFKitConfig.sharedInstance().freehandAnnotationOpacity() / 100.0
  191. default:
  192. break
  193. }
  194. }
  195. return opacity
  196. }
  197. func setOpacity(_ opacity: CGFloat) {
  198. if isSelectAnnot {
  199. for annotation in annotations {
  200. annotation.opacity = opacity
  201. }
  202. } else {
  203. let userDefaults = UserDefaults.standard
  204. switch self.annotMode {
  205. case .freeText:
  206. userDefaults.set(opacity, forKey: CFreeTextNoteOpacityKey)
  207. case .note:
  208. userDefaults.set(opacity, forKey: CAnchoredNoteOpacityKey)
  209. case .circle:
  210. userDefaults.set(opacity, forKey: CCircleNoteOpacityKey)
  211. case .square:
  212. userDefaults.set(opacity, forKey: CSquareNoteOpacityKey)
  213. case .highlight:
  214. userDefaults.set(opacity, forKey: CHighlightNoteOpacityKey)
  215. case .underline:
  216. userDefaults.set(opacity, forKey: CUnderlineNoteOpacityKey)
  217. case .strikeout:
  218. userDefaults.set(opacity, forKey: CStrikeOutNoteOpacityKey)
  219. case .squiggly:
  220. userDefaults.set(opacity, forKey: CSquigglyNoteOpacityKey)
  221. case .line:
  222. userDefaults.set(opacity, forKey: CLineNoteOpacityKey)
  223. case .arrow:
  224. userDefaults.set(opacity, forKey: CArrowNoteOpacityKey)
  225. case .ink:
  226. CPDFKitConfig.sharedInstance().setFreehandAnnotationOpacity(opacity * 100)
  227. userDefaults.set(opacity, forKey: CInkNoteOpacityKey)
  228. default:
  229. break
  230. }
  231. userDefaults.synchronize()
  232. }
  233. }
  234. var style: CPDFBorderStyle {
  235. var style: CPDFBorderStyle = .solid
  236. if isSelectAnnot {
  237. style = annotation?.border.style ?? .solid
  238. } else {
  239. let userDefaults = UserDefaults.standard
  240. switch annotMode {
  241. case .freeText:
  242. style = CPDFBorderStyle(rawValue: userDefaults.integer(forKey: CFreeTextNoteLineStyleKey)) ?? .solid
  243. case .circle:
  244. style = CPDFBorderStyle(rawValue: userDefaults.integer(forKey: CCircleNoteLineStyleKey)) ?? .solid
  245. case .square:
  246. style = CPDFBorderStyle(rawValue: userDefaults.integer(forKey: CSquareNoteLineStyleKey)) ?? .solid
  247. case .line:
  248. style = CPDFBorderStyle(rawValue: userDefaults.integer(forKey: CLineNoteLineStyleKey)) ?? .solid
  249. case .arrow:
  250. style = CPDFBorderStyle(rawValue: userDefaults.integer(forKey: CArrowNoteLineStyleKey)) ?? .solid
  251. case .ink:
  252. style = CPDFBorderStyle(rawValue: userDefaults.integer(forKey: CInkNoteLineStyleyKey)) ?? .solid
  253. default:
  254. break
  255. }
  256. }
  257. return style
  258. }
  259. func setStyle(_ style: CPDFBorderStyle) {
  260. if isSelectAnnot {
  261. for annotation in annotations {
  262. let oldBorder = annotation.border
  263. var dashPattern: [CGFloat] = []
  264. if style == .dashed {
  265. dashPattern.append(5)
  266. }
  267. let border = CPDFBorder(style: style, lineWidth: oldBorder?.lineWidth ?? 0, dashPattern: dashPattern)
  268. annotation.border = border
  269. }
  270. } else {
  271. let userDefaults = UserDefaults.standard
  272. switch annotMode {
  273. case .freeText:
  274. userDefaults.set(style.rawValue, forKey: CFreeTextNoteLineStyleKey)
  275. case .circle:
  276. userDefaults.set(style.rawValue, forKey: CCircleNoteLineStyleKey)
  277. case .square:
  278. userDefaults.set(style.rawValue, forKey: CSquareNoteLineStyleKey)
  279. case .line:
  280. userDefaults.set(style.rawValue, forKey: CLineNoteLineStyleKey)
  281. case .arrow:
  282. userDefaults.set(style.rawValue, forKey: CArrowNoteLineStyleKey)
  283. case .ink:
  284. userDefaults.set(style.rawValue, forKey: CInkNoteLineStyleyKey)
  285. default:
  286. break
  287. }
  288. userDefaults.synchronize()
  289. }
  290. }
  291. var dashPattern: [NSNumber] {
  292. if isSelectAnnot {
  293. if annotation?.border.style == .dashed {
  294. return annotation?.border.dashPattern as? [NSNumber] ?? [0]
  295. } else {
  296. return [0]
  297. }
  298. } else {
  299. let userDefaults = UserDefaults.standard
  300. var dashPattern = 0
  301. switch annotMode {
  302. case .freeText:
  303. dashPattern = userDefaults.integer(forKey: CFreeTextNoteDashPatternKey)
  304. case .circle:
  305. dashPattern = UserDefaults.standard.integer(forKey: CCircleNoteDashPatternKey)
  306. case .square: dashPattern = UserDefaults.standard.integer(forKey: CSquareNoteDashPatternKey)
  307. case .line: dashPattern = UserDefaults.standard.integer(forKey: CLineNoteDashPatternKey)
  308. case .arrow: dashPattern = UserDefaults.standard.integer(forKey: CArrowNoteDashPatternKey)
  309. case .ink: dashPattern = UserDefaults.standard.integer(forKey: CInkNoteDashPatternKey)
  310. default: break
  311. }
  312. if self.style != .dashed {
  313. dashPattern = 0
  314. }
  315. return [dashPattern] as [NSNumber]
  316. }
  317. }
  318. func setDashPattern(_ dashPatterns: [NSNumber]) {
  319. if self.isSelectAnnot {
  320. for annotation in self.annotations {
  321. let oldBorder = annotation.border
  322. let border = CPDFBorder(style: oldBorder?.style ?? .solid, lineWidth: oldBorder?.lineWidth ?? 0, dashPattern: dashPatterns)
  323. annotation.border = border
  324. }
  325. } else {
  326. let dashPattern = dashPatterns.first?.intValue ?? 0
  327. let userDefaults = UserDefaults.standard
  328. switch self.annotMode {
  329. case .freeText:
  330. userDefaults.set(dashPattern, forKey: CFreeTextNoteDashPatternKey)
  331. case .circle:
  332. userDefaults.set(dashPattern, forKey: CCircleNoteDashPatternKey )
  333. case .square:
  334. userDefaults.set(dashPattern, forKey: CSquareNoteDashPatternKey )
  335. case .line:
  336. userDefaults.set(dashPattern, forKey: CLineNoteDashPatternKey )
  337. case .arrow:
  338. userDefaults.set(dashPattern, forKey: CArrowNoteDashPatternKey )
  339. case .ink:
  340. userDefaults.set(dashPattern, forKey: CInkNoteDashPatternKey )
  341. default:
  342. break
  343. }
  344. userDefaults.synchronize()
  345. }
  346. }
  347. var lineWidth: CGFloat {
  348. var zLineWidth: CGFloat = 0
  349. if self.isSelectAnnot {
  350. zLineWidth = self.annotation?.lineWidth ?? 0
  351. } else {
  352. let userDefaults = UserDefaults.standard
  353. switch self.annotMode {
  354. case .circle:
  355. zLineWidth = CGFloat(userDefaults.float(forKey: CCircleNoteLineWidthKey))
  356. case .square:
  357. zLineWidth = CGFloat(userDefaults.float(forKey: CSquareNoteLineWidthKey ))
  358. case .line:
  359. zLineWidth = CGFloat(userDefaults.float(forKey: CLineNoteLineWidthKey ))
  360. case .arrow:
  361. zLineWidth = CGFloat(userDefaults.float(forKey: CArrowNoteLineWidthKey ))
  362. case .ink:
  363. zLineWidth = CPDFKitConfig.sharedInstance().freehandAnnotationBorderWidth()
  364. default:
  365. break
  366. }
  367. }
  368. return zLineWidth
  369. }
  370. func setLineWidth(_ lineWidth: CGFloat) {
  371. if self.isSelectAnnot {
  372. for annotation in self.annotations {
  373. annotation.borderWidth = lineWidth
  374. }
  375. } else {
  376. let userDefaults = UserDefaults.standard
  377. switch self.annotMode {
  378. case .circle:
  379. userDefaults.set(Float(lineWidth), forKey: CCircleNoteLineWidthKey )
  380. case .square:
  381. userDefaults.set(Float(lineWidth), forKey: CSquareNoteLineWidthKey )
  382. case .line:
  383. userDefaults.set(Float(lineWidth), forKey: CLineNoteLineWidthKey )
  384. case .arrow:
  385. userDefaults.set(Float(lineWidth), forKey: CArrowNoteLineWidthKey )
  386. case .ink:
  387. CPDFKitConfig.sharedInstance().setFreehandAnnotationBorderWidth(lineWidth)
  388. userDefaults.set(Float(lineWidth), forKey: CInkNoteLineWidthKey )
  389. default:
  390. break
  391. }
  392. userDefaults.synchronize()
  393. }
  394. }
  395. var startLineStyle: CPDFLineStyle {
  396. var zstartLineStyle: CPDFLineStyle = .none
  397. if self.isSelectAnnot {
  398. if let lineAnnotation = self.annotation as? CPDFLineAnnotation {
  399. zstartLineStyle = lineAnnotation.startLineStyle
  400. }
  401. } else {
  402. let userDefaults = UserDefaults.standard
  403. switch self.annotMode {
  404. case .arrow:
  405. zstartLineStyle = CPDFLineStyle(rawValue: userDefaults.integer(forKey: CArrowNoteStartStyleKey)) ?? .none
  406. case .line:
  407. zstartLineStyle = CPDFLineStyle(rawValue: userDefaults.integer(forKey: CLineNoteStartStyleKey)) ?? .none
  408. default:
  409. break
  410. }
  411. }
  412. return zstartLineStyle
  413. }
  414. func setStartLineStyle(_ startLineStyle: CPDFLineStyle) {
  415. if self.isSelectAnnot {
  416. for annotation in self.annotations {
  417. if let lineAnnotation = annotation as? CPDFLineAnnotation {
  418. lineAnnotation.startLineStyle = startLineStyle
  419. }
  420. }
  421. } else {
  422. let userDefaults = UserDefaults.standard
  423. switch self.annotMode {
  424. case .arrow:
  425. userDefaults.set(startLineStyle.rawValue, forKey: CArrowNoteStartStyleKey)
  426. case .line:
  427. userDefaults.set(startLineStyle.rawValue, forKey: CLineNoteStartStyleKey)
  428. default:
  429. break
  430. }
  431. userDefaults.synchronize()
  432. }
  433. }
  434. var endLineStyle: CPDFLineStyle {
  435. var zendLineStyle: CPDFLineStyle = .none
  436. if self.isSelectAnnot {
  437. if let lineAnnotation = self.annotation as? CPDFLineAnnotation {
  438. zendLineStyle = lineAnnotation.endLineStyle
  439. }
  440. } else {
  441. let userDefaults = UserDefaults.standard
  442. switch self.annotMode {
  443. case .arrow:
  444. zendLineStyle = CPDFLineStyle(rawValue: userDefaults.integer(forKey: CArrowNoteEndStyleKey)) ?? .none
  445. case .line:
  446. zendLineStyle = CPDFLineStyle(rawValue: userDefaults.integer(forKey: CLineNoteEndStyleKey)) ?? .none
  447. default:
  448. break
  449. }
  450. }
  451. return zendLineStyle
  452. }
  453. func setEndLineStyle(_ endLineStyle: CPDFLineStyle) {
  454. if self.isSelectAnnot {
  455. for annotation in self.annotations {
  456. if let lineAnnotation = annotation as? CPDFLineAnnotation {
  457. lineAnnotation.endLineStyle = endLineStyle
  458. }
  459. }
  460. } else {
  461. let userDefaults = UserDefaults.standard
  462. switch self.annotMode {
  463. case .arrow:
  464. userDefaults.set(endLineStyle.rawValue, forKey: CArrowNoteEndStyleKey)
  465. case .line:
  466. userDefaults.set(endLineStyle.rawValue, forKey: CLineNoteEndStyleKey)
  467. default:
  468. break
  469. }
  470. userDefaults.synchronize()
  471. }
  472. }
  473. // MARK: - FreeText
  474. var fontColor: UIColor? {
  475. var zfontColor: UIColor? = nil
  476. if self.isSelectAnnot {
  477. if let freeTextAnnotation = self.annotation as? CPDFFreeTextAnnotation {
  478. zfontColor = freeTextAnnotation.fontColor
  479. }
  480. } else {
  481. let userDefaults = UserDefaults.standard
  482. switch self.annotMode {
  483. case .freeText:
  484. zfontColor = userDefaults.PDFListViewColorForKey(CFreeTextNoteFontColorKey)
  485. default:
  486. break
  487. }
  488. }
  489. return zfontColor
  490. }
  491. func setFontColor(_ fontColor: UIColor?) {
  492. if self.isSelectAnnot {
  493. for annotation in self.annotations {
  494. if let freeTextAnnotation = annotation as? CPDFFreeTextAnnotation {
  495. var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
  496. fontColor?.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  497. freeTextAnnotation.fontColor = UIColor(red: red, green: green, blue: blue, alpha: self.opacity)
  498. }
  499. }
  500. } else {
  501. let userDefaults = UserDefaults.standard
  502. switch self.annotMode {
  503. case .freeText:
  504. userDefaults.setPDFListViewColor(fontColor, forKey: CFreeTextNoteFontColorKey)
  505. default:
  506. break
  507. }
  508. userDefaults.synchronize()
  509. }
  510. }
  511. var fontSize: CGFloat {
  512. var zfontSize: CGFloat = 11
  513. if self.isSelectAnnot {
  514. if let freeTextAnnotation = self.annotation as? CPDFFreeTextAnnotation {
  515. zfontSize = freeTextAnnotation.fontSize
  516. }
  517. } else {
  518. let userDefaults = UserDefaults.standard
  519. switch self.annotMode {
  520. case .freeText:
  521. zfontSize = CGFloat(userDefaults.float(forKey: CFreeTextNoteFontSizeKey))
  522. default:
  523. break
  524. }
  525. }
  526. return zfontSize
  527. }
  528. func setFontSize(_ fontSize: CGFloat) {
  529. if self.isSelectAnnot {
  530. for annotation in self.annotations {
  531. if let freeTextAnnotation = annotation as? CPDFFreeTextAnnotation {
  532. let cFont = freeTextAnnotation.cFont
  533. freeTextAnnotation.fontSize = fontSize
  534. let appleFont = UIFont.init(name: CPDFFont.convertAppleFont(cFont ?? CPDFFont(familyName: "Helvetica", fontStyle: "")) ?? "Helvetica", size: fontSize)
  535. let attributes: [NSAttributedString.Key: Any] = [.font: appleFont ?? UIFont.systemFont(ofSize: 11.0)]
  536. let bounds = annotation.bounds
  537. let rect = freeTextAnnotation.contents.boundingRect(with: CGSize(width: bounds.size.width, height: CGFloat.greatestFiniteMagnitude), options: .usesLineFragmentOrigin, attributes: attributes, context: nil)
  538. var newBounds = bounds
  539. newBounds.origin.y = bounds.maxY - rect.size.height
  540. newBounds.size.height = rect.size.height + 6
  541. annotation.bounds = newBounds
  542. }
  543. }
  544. } else {
  545. let userDefaults = UserDefaults.standard
  546. switch self.annotMode {
  547. case .freeText:
  548. userDefaults.set(Float(fontSize), forKey: CFreeTextNoteFontSizeKey)
  549. default:
  550. break
  551. }
  552. userDefaults.synchronize()
  553. }
  554. }
  555. var newCFont: CPDFFont {
  556. var cfont: CPDFFont = CPDFFont.init(familyName: "Helvetica", fontStyle: "")
  557. if self.isSelectAnnot {
  558. if let freeTextAnnotation = self.annotation as? CPDFFreeTextAnnotation {
  559. cfont = freeTextAnnotation.cFont
  560. }
  561. } else {
  562. let userDefaults = UserDefaults.standard
  563. switch self.annotMode {
  564. case .freeText:
  565. let zfontName = userDefaults.string(forKey: CFreeTextNoteFontFamilyNameKey)
  566. let zfontStyle = userDefaults.string(forKey: CFreeTextNoteFontNewStyleKey)
  567. cfont = CPDFFont.init(familyName: zfontName ?? "Helvetica", fontStyle: zfontStyle ?? "")
  568. break
  569. default:
  570. break
  571. }
  572. }
  573. return cfont
  574. }
  575. func setNewCFont(_ font: CPDFFont) {
  576. if self.isSelectAnnot {
  577. for annotation in self.annotations {
  578. if let freeTextAnnotation = annotation as? CPDFFreeTextAnnotation {
  579. freeTextAnnotation.cFont = font
  580. }
  581. }
  582. } else {
  583. let userDefaults = UserDefaults.standard
  584. switch self.annotMode {
  585. case .freeText:
  586. let styleName = font.styleName
  587. let familyName = font.familyName
  588. if styleName?.count != 0 {
  589. userDefaults.set(styleName, forKey: CFreeTextNoteFontNewStyleKey)
  590. } else {
  591. userDefaults.set("", forKey: CFreeTextNoteFontNewStyleKey)
  592. }
  593. if familyName.count != 0 {
  594. userDefaults.set(familyName, forKey: CFreeTextNoteFontFamilyNameKey)
  595. } else {
  596. userDefaults.set("Helvetica", forKey: CFreeTextNoteFontNewStyleKey)
  597. }
  598. break
  599. default:
  600. break
  601. }
  602. userDefaults.synchronize()
  603. }
  604. }
  605. var alignment: NSTextAlignment {
  606. var zalignment: NSTextAlignment = .left
  607. if self.isSelectAnnot {
  608. if let freeTextAnnotation = self.annotation as? CPDFFreeTextAnnotation {
  609. zalignment = freeTextAnnotation.alignment
  610. }
  611. } else {
  612. let userDefaults = UserDefaults.standard
  613. switch self.annotMode {
  614. case .freeText:
  615. zalignment = NSTextAlignment(rawValue: userDefaults.integer(forKey: CFreeTextNoteAlignmentKey)) ?? .left
  616. default:
  617. break
  618. }
  619. }
  620. return zalignment
  621. }
  622. func setAlignment(_ alignment: NSTextAlignment) {
  623. if self.isSelectAnnot {
  624. for annotation in self.annotations {
  625. if let freeTextAnnotation = annotation as? CPDFFreeTextAnnotation {
  626. freeTextAnnotation.alignment = alignment
  627. }
  628. }
  629. } else {
  630. let userDefaults = UserDefaults.standard
  631. switch self.annotMode {
  632. case .freeText:
  633. userDefaults.set(alignment.rawValue, forKey: CFreeTextNoteAlignmentKey)
  634. default:
  635. break
  636. }
  637. userDefaults.synchronize()
  638. }
  639. }
  640. // MARK: - Circle&Square
  641. var interiorColor: UIColor? {
  642. var zinteriorColor: UIColor? = nil
  643. if self.isSelectAnnot {
  644. if(self.annotation is CPDFCircleAnnotation ) {
  645. guard let circleAnnotation = self.annotation as? CPDFCircleAnnotation else {
  646. return UIColor.clear
  647. }
  648. zinteriorColor = circleAnnotation.interiorColor
  649. } else if (self.annotation is CPDFSquareAnnotation) {
  650. let squareAnnotation:CPDFSquareAnnotation = self.annotation as! CPDFSquareAnnotation
  651. zinteriorColor = squareAnnotation.interiorColor
  652. } else if let freeTextAnnotation = self.annotation as? CPDFFreeTextAnnotation {
  653. zinteriorColor = freeTextAnnotation.color
  654. }
  655. } else {
  656. let userDefaults = UserDefaults.standard
  657. switch self.annotMode {
  658. case .circle:
  659. zinteriorColor = userDefaults.PDFListViewColorForKey(CCircleNoteInteriorColorKey)
  660. case .square:
  661. zinteriorColor = userDefaults.PDFListViewColorForKey( CSquareNoteInteriorColorKey)
  662. default:
  663. break
  664. }
  665. }
  666. return zinteriorColor
  667. }
  668. func setInteriorColor(_ interiorColor: UIColor?) {
  669. if self.isSelectAnnot {
  670. for annotation in self.annotations {
  671. if let circleAnnotation = annotation as? CPDFCircleAnnotation {
  672. circleAnnotation.interiorColor = interiorColor
  673. } else if let squareAnnotation = annotation as? CPDFSquareAnnotation {
  674. squareAnnotation.interiorColor = interiorColor
  675. } else if let freeTextAnnotation = annotation as? CPDFFreeTextAnnotation {
  676. freeTextAnnotation.color = interiorColor
  677. }
  678. }
  679. } else {
  680. let userDefaults = UserDefaults.standard
  681. switch self.annotMode {
  682. case .circle:
  683. userDefaults.setPDFListViewColor(interiorColor, forKey: CCircleNoteInteriorColorKey)
  684. case .square:
  685. userDefaults.setPDFListViewColor(interiorColor, forKey: CSquareNoteInteriorColorKey)
  686. default:
  687. break
  688. }
  689. }
  690. }
  691. var interiorOpacity: CGFloat {
  692. var zinteriorOpacity: CGFloat = 0
  693. if self.isSelectAnnot {
  694. if let circleAnnotation = annotation as? CPDFCircleAnnotation {
  695. zinteriorOpacity = circleAnnotation.interiorOpacity
  696. } else if let squareAnnotation = annotation as? CPDFSquareAnnotation {
  697. zinteriorOpacity = squareAnnotation.interiorOpacity
  698. } else {
  699. zinteriorOpacity = self.annotation?.opacity ?? 1.0
  700. }
  701. } else {
  702. let userDefaults = UserDefaults.standard
  703. switch self.annotMode {
  704. case .circle:
  705. zinteriorOpacity = CGFloat(userDefaults.float(forKey: CCircleNoteInteriorOpacityKey))
  706. case .square:
  707. zinteriorOpacity = CGFloat(userDefaults.float(forKey: CSquareNoteInteriorOpacityKey))
  708. default:
  709. break
  710. }
  711. }
  712. return zinteriorOpacity
  713. }
  714. func setInteriorOpacity(_ interiorOpacity: CGFloat) {
  715. if self.isSelectAnnot {
  716. for annotation in self.annotations {
  717. if let circleAnnotation = annotation as? CPDFCircleAnnotation {
  718. circleAnnotation.interiorOpacity = interiorOpacity
  719. } else if let squareAnnotation = annotation as? CPDFSquareAnnotation {
  720. squareAnnotation.interiorOpacity = interiorOpacity
  721. } else {
  722. annotation.opacity = interiorOpacity
  723. }
  724. }
  725. } else {
  726. let userDefaults = UserDefaults.standard
  727. switch self.annotMode {
  728. case .circle:
  729. userDefaults.set(Float(interiorOpacity), forKey: CCircleNoteInteriorOpacityKey)
  730. case .square:
  731. userDefaults.set(Float(interiorOpacity), forKey: CSquareNoteInteriorOpacityKey)
  732. default:
  733. break
  734. }
  735. userDefaults.synchronize()
  736. }
  737. }
  738. }