KMTextBoxController.swift 30 KB

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