KMTextBoxController.swift 28 KB

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