KMTextBoxController.swift 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. //
  2. // KMTextBoxController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/11/26.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMTextBoxController: NSViewController {
  10. @IBOutlet var fontBGView: NSView!
  11. @IBOutlet var fontLabel: NSTextField!
  12. @IBOutlet var fontNameSelect: ComponentSelect!
  13. @IBOutlet var fontStyleSelect: ComponentSelect!
  14. @IBOutlet var fontSizeSelect: ComponentSelect!
  15. @IBOutlet var fontAlignmentGroup: ComponentCSelectorGroup!
  16. //Color
  17. @IBOutlet var colorBGView: NSView!
  18. @IBOutlet var colorLabel: NSTextField!
  19. @IBOutlet var colorGroup: ComponentCColorGroup!
  20. @IBOutlet var borderColorGroup: ComponentCColorGroup!
  21. @IBOutlet var fillColorGroup: ComponentCColorGroup!
  22. @IBOutlet var opacitySlider: ComponentSlider!
  23. @IBOutlet var opacitySelect: ComponentSelect!
  24. //Line
  25. @IBOutlet var lineBGView: NSView!
  26. @IBOutlet var lineLabel: NSTextField!
  27. @IBOutlet var lineTypeSelector: ComponentCSelectorGroup!
  28. @IBOutlet var lineWidthSlider: ComponentSlider!
  29. @IBOutlet var lineWidthSelect: ComponentSelect!
  30. @IBOutlet var linedashInfoView: NSView!
  31. @IBOutlet var lineDashSlider: ComponentSlider!
  32. @IBOutlet var lineDashSelect: ComponentSelect!
  33. private var familyNames = CPDFFont.familyNames
  34. private let fontAlign_leftItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontAlign_left"), identifier: "fontAlign_left")
  35. private let fontAlign_centerItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontAlign_center"), identifier: "fontAlign_center")
  36. private let fontAlign_rightItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontAlign_right"), identifier: "fontAlign_right")
  37. private let fontAlign_justifyItem: ComponentCSelectorProperty = ComponentCSelectorProperty.init(size: .m, state: .normal, iconImage: NSImage(named: "fontAlign_justify"), identifier: "fontAlign_justify")
  38. private let solidProperty = ComponentCSelectorProperty.init(size: .s, state: .normal, text: "", iconImage: NSImage(named: "lineStyle_solid"))
  39. private let dashProperty = ComponentCSelectorProperty.init(size: .s, state: .normal, text: "", iconImage: NSImage(named: "lineStyle_dash"))
  40. private var annotations: [CPDFFreeTextAnnotation] = []
  41. var pdfView: CPDFListView?
  42. //MARK: - func
  43. override func viewDidAppear() {
  44. super.viewDidAppear()
  45. opacitySlider.reloadData()
  46. lineWidthSlider.reloadData()
  47. lineDashSlider.reloadData()
  48. }
  49. override func viewDidLoad() {
  50. super.viewDidLoad()
  51. // Do view setup here.
  52. setupProperty()
  53. }
  54. func setupProperty() {
  55. //Font
  56. fontNameSelect.properties = ComponentSelectProperties(size: .s,
  57. state: .normal,
  58. text: "")
  59. if true {
  60. var menuItemArr: [ComponentMenuitemProperty] = []
  61. for string in familyNames {
  62. let item = ComponentMenuitemProperty(type: .normal, text: string, identifier: string)
  63. menuItemArr.append(item)
  64. }
  65. fontNameSelect.updateMenuItemsArr(menuItemArr)
  66. }
  67. fontNameSelect.delegate = self
  68. fontStyleSelect.properties = ComponentSelectProperties(size: .s,
  69. state: .normal,
  70. text: "")
  71. fontStyleSelect.delegate = self
  72. fontSizeSelect.properties = ComponentSelectProperties(size: .s,
  73. state: .normal,
  74. creatable: true,
  75. text: "12 pt",
  76. textUnit: " pt",
  77. regexString: "0123456789")
  78. if true {
  79. var sizeItemArr: [ComponentMenuitemProperty] = []
  80. for string in KMHeaderFooterManager.getFontSize() {
  81. let item = ComponentMenuitemProperty(type: .normal, text: string + " pt", identifier: string)
  82. sizeItemArr.append(item)
  83. }
  84. fontSizeSelect.updateMenuItemsArr(sizeItemArr)
  85. }
  86. fontSizeSelect.delegate = self
  87. if true {
  88. let itemArr: [ComponentCSelectorProperty] = [fontAlign_leftItem, fontAlign_centerItem, fontAlign_rightItem, fontAlign_justifyItem]
  89. fontAlignmentGroup.updateItemProperty(itemArr)
  90. fontAlignmentGroup.delegate = self
  91. }
  92. //Color
  93. colorLabel.stringValue = KMLocalizedString("Color")
  94. colorLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  95. colorLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  96. if true {
  97. let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[0])
  98. let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[1])
  99. let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[2])
  100. let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[3])
  101. let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: KMPDFWatermarkData.watermarkDefaultColors()[4])
  102. colorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
  103. colorGroup.delegate = self
  104. }
  105. if true {
  106. let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[0])
  107. let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[1])
  108. let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[2])
  109. let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[3])
  110. let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: KMPDFWatermarkData.watermarkDefaultColors()[4])
  111. borderColorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
  112. borderColorGroup.delegate = self
  113. }
  114. if true {
  115. let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[0])
  116. let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[1])
  117. let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[2])
  118. let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: KMPDFWatermarkData.watermarkDefaultColors()[3])
  119. let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: KMPDFWatermarkData.watermarkDefaultColors()[4])
  120. fillColorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
  121. fillColorGroup.delegate = self
  122. }
  123. opacitySlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  124. opacitySlider.delegate = self
  125. opacitySelect.properties = ComponentSelectProperties(size: .s,
  126. state: .normal,
  127. creatable: true,
  128. text: "100%",
  129. textUnit: "%",
  130. regexString: "0123456789%")
  131. if true {
  132. var opacityItems: [ComponentMenuitemProperty] = []
  133. for string in ["25%", "50%", "75%", "100%"] {
  134. let item = ComponentMenuitemProperty(type: .normal, text: string)
  135. opacityItems.append(item)
  136. }
  137. opacitySelect.updateMenuItemsArr(opacityItems)
  138. }
  139. opacitySelect.delegate = self
  140. //Line
  141. lineLabel.stringValue = KMLocalizedString("Line")
  142. lineLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  143. lineLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  144. lineTypeSelector.updateItemProperty([solidProperty, dashProperty])
  145. lineTypeSelector.delegate = self
  146. lineWidthSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  147. lineWidthSlider.delegate = self
  148. lineWidthSelect.properties = ComponentSelectProperties(size: .s,
  149. state: .normal,
  150. creatable: true,
  151. text: "2",
  152. textUnit: " pt",
  153. regexString: "0123456789 pt")
  154. if true {
  155. var opacityItems: [ComponentMenuitemProperty] = []
  156. for string in ["1 pt", "3 pt", "6 pt", "9 pt", "12 pt", "15 pt", "18 pt"] {
  157. let item = ComponentMenuitemProperty(type: .normal, text: string)
  158. opacityItems.append(item)
  159. }
  160. lineWidthSelect.updateMenuItemsArr(opacityItems)
  161. }
  162. lineWidthSelect.delegate = self
  163. lineDashSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  164. lineDashSlider.delegate = self
  165. lineDashSelect.properties = ComponentSelectProperties(size: .s,
  166. state: .normal,
  167. creatable: true,
  168. text: "2",
  169. textUnit: " pt",
  170. regexString: "0123456789 pt")
  171. if true {
  172. var opacityItems: [ComponentMenuitemProperty] = []
  173. for string in ["1 pt", "3 pt", "6 pt", "9 pt", "12 pt", "15 pt", "18 pt"] {
  174. let item = ComponentMenuitemProperty(type: .normal, text: string)
  175. opacityItems.append(item)
  176. }
  177. lineDashSelect.updateMenuItemsArr(opacityItems)
  178. }
  179. lineDashSelect.delegate = self
  180. }
  181. func reloadData() {
  182. guard let pdfView = self.pdfView else {
  183. return
  184. }
  185. self.annotations.removeAll()
  186. let allAnnotations: [CPDFAnnotation] = pdfView.activeAnnotations as? [CPDFAnnotation] ?? []
  187. for annotation in allAnnotations {
  188. if annotation is CPDFFreeTextAnnotation {
  189. annotations.append((annotation as! CPDFFreeTextAnnotation))
  190. }
  191. }
  192. if annotations.count == 0 {
  193. return
  194. }
  195. guard let firstAnnotation = annotations.first else {
  196. return
  197. }
  198. if annotations.count == 1 {
  199. //Font
  200. if true {
  201. let styleNames = CPDFFont.fontNames(forFamilyName: firstAnnotation.cFont.familyName)
  202. var menuItemArr: [ComponentMenuitemProperty] = []
  203. for string in styleNames {
  204. let item = ComponentMenuitemProperty(type: .normal, text: string, identifier: string)
  205. menuItemArr.append(item)
  206. }
  207. fontStyleSelect.updateMenuItemsArr(menuItemArr)
  208. if firstAnnotation.cFont.styleName == nil {
  209. fontStyleSelect.selectItemAtIndex(0)
  210. } else {
  211. fontStyleSelect.properties.text = firstAnnotation.cFont.styleName
  212. fontStyleSelect.reloadData()
  213. }
  214. }
  215. fontSizeSelect.properties.text = String(format: "%.0f", firstAnnotation.fontSize)
  216. fontSizeSelect.reloadData()
  217. //Color
  218. colorGroup.currentColor = firstAnnotation.fontColor
  219. colorGroup.refreshUI()
  220. borderColorGroup.currentColor = firstAnnotation.borderColor
  221. borderColorGroup.refreshUI()
  222. fillColorGroup.currentColor = firstAnnotation.color
  223. fillColorGroup.refreshUI()
  224. fillColorGroup.refreshUI()
  225. let opacity = firstAnnotation.opacity
  226. opacitySlider.properties.percent = opacity
  227. opacitySlider.reloadData()
  228. opacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
  229. opacitySelect.reloadData()
  230. let border: CPDFBorder = firstAnnotation.border
  231. dashProperty.state = .normal
  232. solidProperty.state = .normal
  233. if border.style == .dashed {
  234. dashProperty.state = .pressed
  235. } else if border.style == .solid {
  236. solidProperty.state = .pressed
  237. }
  238. lineTypeSelector.reloadData()
  239. let percent = (border.lineWidth - 1)/17
  240. lineWidthSlider.properties.percent = percent
  241. lineWidthSlider.reloadData()
  242. lineWidthSelect.properties.text = String(format: "%.0f%@", border.lineWidth, " pt")
  243. lineWidthSelect.reloadData()
  244. linedashInfoView.isHidden = true
  245. if border.style == .dashed {
  246. linedashInfoView.isHidden = false
  247. var dash = 1.0
  248. for dashPattern in border.dashPattern {
  249. if let value = dashPattern as? CGFloat {
  250. dash = value
  251. break
  252. }
  253. }
  254. let percent: CGFloat = (CGFloat(dash) - 1)/17
  255. lineDashSlider.properties.percent = percent
  256. lineDashSlider.reloadData()
  257. lineDashSelect.properties.text = String(format: "%.0f%@", CGFloat(dash), " pt")
  258. lineDashSelect.reloadData()
  259. }
  260. } else {
  261. // if true {
  262. // var multiColor: Bool = false
  263. // for annotationA in annotations {
  264. // for annotationB in annotations {
  265. // if annotationA != annotationB {
  266. // if annotationA.color != annotationB.color {
  267. // multiColor = true
  268. // break
  269. // }
  270. // }
  271. // }
  272. // if multiColor == true {
  273. // break
  274. // }
  275. // }
  276. // if multiColor == true {
  277. // colorGroup.currentColor = NSColor.clear
  278. // } else {
  279. // colorGroup.currentColor = firstAnnotation.color
  280. // }
  281. // colorGroup.refreshUI()
  282. // }
  283. //
  284. // if true {
  285. // var multiOpacity: Bool = false
  286. // for annotationA in annotations {
  287. // for annotationB in annotations {
  288. // if annotationA != annotationB {
  289. // if annotationA.opacity != annotationB.opacity {
  290. // multiOpacity = true
  291. // break
  292. // }
  293. // }
  294. // }
  295. // if multiOpacity == true {
  296. // break
  297. // }
  298. // }
  299. //
  300. // if multiOpacity {
  301. // colorSlider.properties.percent = 0
  302. // colorSlider.reloadData()
  303. //
  304. // colorOpacitySelect.resetText("-")
  305. // } else {
  306. // let opacity = firstAnnotation.opacity
  307. //
  308. // colorSlider.properties.percent = opacity
  309. // colorSlider.reloadData()
  310. //
  311. // colorOpacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
  312. // colorOpacitySelect.reloadData()
  313. // }
  314. // }
  315. //
  316. // if true {
  317. // var multiStyle: Bool = false
  318. // for annotationA in annotations {
  319. // for annotationB in annotations {
  320. // if annotationA != annotationB {
  321. // if annotationA.borderStyle() != annotationB.borderStyle() {
  322. // multiStyle = true
  323. // break
  324. // }
  325. // }
  326. // }
  327. // if multiStyle == true {
  328. // break
  329. // }
  330. // }
  331. //
  332. // linedashInfoView.isHidden = true
  333. // if multiStyle {
  334. // dashProperty.state = .normal
  335. // solidProperty.state = .normal
  336. //
  337. // lineTypeSelector.reloadData()
  338. // } else {
  339. // let style = firstAnnotation.border.style
  340. // dashProperty.state = .normal
  341. // solidProperty.state = .normal
  342. // if style == .dashed {
  343. // dashProperty.state = .pressed
  344. // } else if style == .solid {
  345. // solidProperty.state = .pressed
  346. // }
  347. // lineTypeSelector.reloadData()
  348. //
  349. // if style == .dashed {
  350. // linedashInfoView.isHidden = false
  351. // }
  352. // }
  353. // }
  354. //
  355. // if true {
  356. // var multiLineWidth: Bool = false
  357. // for annotationA in annotations {
  358. // for annotationB in annotations {
  359. // if annotationA != annotationB {
  360. // if annotationA.borderStyle() != annotationB.borderStyle() {
  361. // multiLineWidth = true
  362. // break
  363. // }
  364. // }
  365. // }
  366. // if multiLineWidth == true {
  367. // break
  368. // }
  369. // }
  370. // if multiLineWidth {
  371. // lineWidthSlider.properties.percent = 0
  372. // lineWidthSlider.reloadData()
  373. //
  374. // lineWidthSelect.resetText("-")
  375. // } else {
  376. // let border: CPDFBorder = firstAnnotation.border
  377. //
  378. // let percent = (border.lineWidth - 1)/17
  379. // lineWidthSlider.properties.percent = percent
  380. // lineWidthSlider.reloadData()
  381. //
  382. // lineWidthSelect.properties.text = String(format: "%.0f%@", border.lineWidth, " pt")
  383. // lineWidthSelect.reloadData()
  384. // }
  385. // }
  386. //
  387. // if true {
  388. // var multiLineDash: Bool = false
  389. // for annotationA in annotations {
  390. // var dashA = 1.0
  391. // for dashPattern in annotationA.border.dashPattern {
  392. // if let value = dashPattern as? CGFloat {
  393. // dashA = value
  394. // break
  395. // }
  396. // }
  397. // for annotationB in annotations {
  398. // if annotationA != annotationB {
  399. // var dashB = 1.0
  400. // for dashPattern in annotationB.border.dashPattern {
  401. // if let value = dashPattern as? CGFloat {
  402. // dashB = value
  403. // break
  404. // }
  405. // }
  406. // if dashA != dashB {
  407. // multiLineDash = true
  408. // break
  409. // }
  410. // }
  411. // }
  412. // if multiLineDash == true {
  413. // break
  414. // }
  415. // }
  416. // if multiLineDash {
  417. // lineDashSlider.properties.percent = 0
  418. // lineDashSlider.reloadData()
  419. //
  420. // lineDashSelect.resetText("-")
  421. // } else {
  422. // var dashA = 1.0
  423. // for dashPattern in firstAnnotation.border.dashPattern {
  424. // if let value = dashPattern as? CGFloat {
  425. // dashA = value
  426. // break
  427. // }
  428. // }
  429. //
  430. // let percent = (dashA - 1)/17
  431. // lineDashSlider.properties.percent = percent
  432. // lineDashSlider.reloadData()
  433. //
  434. // lineDashSelect.properties.text = String(format: "%.0f%@", dashA, " pt")
  435. // lineDashSelect.reloadData()
  436. // }
  437. // }
  438. }
  439. }
  440. //MARK: - Mouse
  441. override func mouseDown(with event: NSEvent) {
  442. super.mouseDown(with: event)
  443. view.window?.makeFirstResponder(nil)
  444. }
  445. }
  446. //MARK: - ComponentCColorDelegate
  447. extension KMTextBoxController: ComponentCColorDelegate {
  448. func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) {
  449. if view == colorGroup {
  450. for annotation in self.annotations {
  451. annotation.updateFontColor(color, withPDFView: pdfView)
  452. }
  453. } else if view == borderColorGroup {
  454. for annotation in self.annotations {
  455. annotation.updateBorderColor(color, withPDFView: pdfView)
  456. }
  457. } else if view == fillColorGroup {
  458. for annotation in self.annotations {
  459. annotation.updateColor(color, withPDFView: pdfView)
  460. }
  461. }
  462. reloadData()
  463. }
  464. }
  465. //MARK: - ComponentSliderDelegate
  466. extension KMTextBoxController: ComponentSliderDelegate {
  467. func componentSliderDidUpdate(_ view: ComponentSlider) {
  468. if view == opacitySlider {
  469. let percent = view.properties.percent
  470. for annotation in self.annotations {
  471. annotation.updateOpacity(percent, withPDFView: pdfView)
  472. }
  473. } else if view == lineWidthSlider {
  474. let percent = view.properties.percent * 17 + 1
  475. for annotation in self.annotations {
  476. annotation.updateLineWidth(percent, withPDFView: pdfView)
  477. }
  478. } else if view == lineDashSlider {
  479. let percent = view.properties.percent * 17 + 1
  480. for annotation in self.annotations {
  481. annotation.updateDashPattern(percent, withPDFView: pdfView)
  482. }
  483. }
  484. reloadData()
  485. }
  486. }
  487. //MARK: - ComponentSelectDelegate
  488. extension KMTextBoxController: ComponentSelectDelegate {
  489. func componentSelectTextDidEndEditing(_ view: ComponentSelect, removeUnit text: String?) {
  490. if let result = text {
  491. if view == opacitySelect {
  492. let opacity = max(0, min(1, result.stringToCGFloat()/100))
  493. for annotation in self.annotations {
  494. annotation.updateOpacity(opacity, withPDFView: pdfView)
  495. }
  496. } else if view == lineWidthSelect {
  497. var value = result.stringToCGFloat()
  498. if value > 18 {
  499. value = 18
  500. } else if value < 1 {
  501. value = 1
  502. }
  503. for annotation in self.annotations {
  504. annotation.updateLineWidth(value, withPDFView: pdfView)
  505. }
  506. } else if view == lineDashSelect {
  507. var value = result.stringToCGFloat()
  508. if value > 18 {
  509. value = 18
  510. } else if value < 1 {
  511. value = 1
  512. }
  513. for annotation in self.annotations {
  514. annotation.updateDashPattern(value, withPDFView: pdfView)
  515. }
  516. }
  517. reloadData()
  518. }
  519. }
  520. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  521. if var result = menuItemProperty?.text {
  522. if let textUnit = view?.properties.textUnit {
  523. result = result.stringByDeleteCharString(textUnit)
  524. }
  525. if view == fontNameSelect {
  526. var styleName = "Regular"
  527. let styleNames = CPDFFont.fontNames(forFamilyName: result)
  528. if let first = styleNames.first {
  529. styleName = first
  530. }
  531. for annotation in self.annotations {
  532. annotation.updateFont(CPDFFont(familyName: result, fontStyle: styleName), withPDFView: pdfView)
  533. }
  534. } else if view == fontStyleSelect {
  535. let fontName = fontNameSelect.properties.text ?? "Helvetica"
  536. for annotation in self.annotations {
  537. annotation.updateFont(CPDFFont(familyName: fontName, fontStyle: result), withPDFView: pdfView)
  538. }
  539. } else if view == fontSizeSelect {
  540. let size = result.stringToCGFloat()
  541. for annotation in self.annotations {
  542. annotation.updateFontSize(size, withPDFView: pdfView)
  543. }
  544. } else if view == opacitySelect {
  545. let opacity = max(0, min(1, result.stringToCGFloat()/100))
  546. for annotation in self.annotations {
  547. annotation.updateOpacity(opacity, withPDFView: pdfView)
  548. }
  549. } else if view == lineWidthSelect {
  550. var value = result.stringToCGFloat()
  551. if value > 18 {
  552. value = 18
  553. } else if value < 1 {
  554. value = 1
  555. }
  556. for annotation in self.annotations {
  557. annotation.updateLineWidth(value, withPDFView: pdfView)
  558. }
  559. } else if view == lineDashSelect {
  560. var value = result.stringToCGFloat()
  561. if value > 18 {
  562. value = 18
  563. } else if value < 1 {
  564. value = 1
  565. }
  566. for annotation in self.annotations {
  567. annotation.updateDashPattern(value, withPDFView: pdfView)
  568. }
  569. }
  570. reloadData()
  571. }
  572. }
  573. }
  574. //MARK: - ComponentCSelectorGroupDelegate
  575. extension KMTextBoxController: ComponentCSelectorGroupDelegate {
  576. func componentCSelectorGroupDidChoose(_ view: ComponentCSelectorGroup, _ item: ComponentCSelectorItem) {
  577. if item.properties == solidProperty {
  578. for annotation in self.annotations {
  579. annotation.updateStyle(.solid, withPDFView: pdfView)
  580. }
  581. } else if item.properties == dashProperty {
  582. for annotation in self.annotations {
  583. annotation.updateStyle(.dashed, withPDFView: pdfView)
  584. }
  585. }
  586. reloadData()
  587. }
  588. }