CAnnotStyle.m 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. //
  2. // CAnnotStyle.m
  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 "CAnnotStyle.h"
  13. #import "CPDFListView.h"
  14. #import "CStringConstants.h"
  15. #import "CPDFAnnotation+PDFListView.h"
  16. #import "NSUserDefaults+Utils.h"
  17. #pragma mark - CAnnotStyle
  18. @interface CAnnotStyle ()
  19. @property (nonatomic, strong) NSArray *headKeys;
  20. @property (nonatomic, strong) NSArray *trialKeys;
  21. @property (nonatomic, strong) NSArray *annotations;
  22. @property (nonatomic, assign) CPDFViewAnnotationMode annotMode;
  23. @property (nonatomic, assign) BOOL isSelectAnnot;
  24. @end
  25. @implementation CAnnotStyle
  26. - (instancetype)initWithAnnotionMode:(CPDFViewAnnotationMode)annotionMode annotations:(NSArray *)annotations {
  27. if (self = [super init]) {
  28. if(annotations.count > 0) {
  29. self.isSelectAnnot = YES;
  30. self.annotations = annotations;
  31. self.annotMode = [self convertAnnotationType];
  32. } else {
  33. self.isSelectAnnot = NO;
  34. self.annotMode = annotionMode;
  35. }
  36. }
  37. return self;
  38. }
  39. - (CPDFAnnotation *)annotation {
  40. return self.annotations.firstObject;
  41. }
  42. - (CPDFViewAnnotationMode)convertAnnotationType {
  43. CPDFViewAnnotationMode annotationType = CPDFViewAnnotationModeNone;
  44. if ([self.annotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
  45. annotationType = CPDFViewAnnotationModeFreeText;
  46. } else if ([self.annotation isKindOfClass:[CPDFTextAnnotation class]]) {
  47. annotationType = CPDFViewAnnotationModeNote;
  48. } else if ([self.annotation isKindOfClass:[CPDFCircleAnnotation class]]) {
  49. annotationType = CPDFViewAnnotationModeCircle;
  50. } else if ([self.annotation isKindOfClass:[CPDFSquareAnnotation class]]) {
  51. annotationType = CPDFViewAnnotationModeSquare;
  52. } else if ([self.annotation isKindOfClass:[CPDFMarkupAnnotation class]]) {
  53. CPDFMarkupAnnotation *markup = (CPDFMarkupAnnotation *)self.annotation;
  54. if (CPDFMarkupTypeHighlight == markup.markupType) {
  55. annotationType = CPDFViewAnnotationModeHighlight;
  56. } else if (CPDFMarkupTypeStrikeOut == markup.markupType) {
  57. annotationType = CPDFViewAnnotationModeStrikeout;
  58. } else if (CPDFMarkupTypeUnderline == markup.markupType) {
  59. annotationType = CPDFViewAnnotationModeUnderline;
  60. } else if (CPDFMarkupTypeSquiggly == markup.markupType) {
  61. annotationType = CPDFViewAnnotationModeSquiggly;
  62. }
  63. } else if ([self.annotation isKindOfClass:[CPDFLineAnnotation class]]) {
  64. CPDFLineAnnotation *line = (CPDFLineAnnotation *)self.annotation;
  65. if (CPDFLineStyleNone == line.endLineStyle && CPDFLineStyleNone == line.startLineStyle) {
  66. annotationType = CPDFViewAnnotationModeLine;
  67. } else {
  68. annotationType = CPDFViewAnnotationModeArrow;
  69. }
  70. } else if ([self.annotation isKindOfClass:[CPDFInkAnnotation class]]) {
  71. annotationType = CPDFViewAnnotationModeInk;
  72. } else if ([self.annotation isKindOfClass:[CPDFLinkAnnotation class]]) {
  73. annotationType = CPDFViewAnnotationModeLink;
  74. } else if ([self.annotation isKindOfClass:[CPDFSignatureAnnotation class]]) {
  75. annotationType = CPDFViewAnnotationModeSignature;
  76. } else if ([self.annotation isKindOfClass:[CPDFStampAnnotation class]]) {
  77. annotationType = CPDFViewAnnotationModeStamp;
  78. } else if ([self.annotation isKindOfClass:[CPDFSoundAnnotation class]]) {
  79. annotationType = CPDFViewAnnotationModeSound;
  80. }else if([self.annotation isKindOfClass:[CPDFTextWidgetAnnotation class]]){
  81. annotationType = CPDFViewFormModeText;
  82. }else if([self.annotation isKindOfClass:[CPDFButtonWidgetAnnotation class]]) {
  83. CPDFButtonWidgetAnnotation * annotation = (CPDFButtonWidgetAnnotation*)self.annotation;
  84. if(annotation.controlType == CPDFWidgetCheckBoxControl) {
  85. annotationType = CPDFViewFormModeCheckBox;
  86. }else if(annotation.controlType == CPDFWidgetRadioButtonControl) {
  87. annotationType = CPDFViewFormModeRadioButton;
  88. }else if(annotation.controlType == CPDFWidgetPushButtonControl) {
  89. annotationType = CPDFViewFormModeButton;
  90. }
  91. }else if([self.annotation isKindOfClass:[CPDFChoiceWidgetAnnotation class]]){
  92. CPDFChoiceWidgetAnnotation * annotation = (CPDFChoiceWidgetAnnotation*)self.annotation;
  93. if(annotation.isListChoice){
  94. annotationType = CPDFViewFormModeList;
  95. }else {
  96. annotationType = CPDFViewFormModeCombox;
  97. }
  98. }
  99. return annotationType;
  100. }
  101. #pragma mark - Common
  102. - (UIColor *)color {
  103. UIColor *color = nil;
  104. if (self.isSelectAnnot) {
  105. color = [self annotation].color;
  106. } else {
  107. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  108. switch (_annotMode) {
  109. case CPDFViewAnnotationModeNote:
  110. color = [userDefaults PDFListViewColorForKey:CAnchoredNoteColorKey];
  111. break;
  112. case CPDFViewAnnotationModeCircle:
  113. color = [userDefaults PDFListViewColorForKey:CCircleNoteColorKey];
  114. break;
  115. case CPDFViewAnnotationModeSquare:
  116. color = [userDefaults PDFListViewColorForKey:CSquareNoteColorKey];
  117. break;
  118. case CPDFViewAnnotationModeHighlight:
  119. color = [userDefaults PDFListViewColorForKey:CHighlightNoteColorKey];
  120. break;
  121. case CPDFViewAnnotationModeUnderline:
  122. color = [userDefaults PDFListViewColorForKey:CUnderlineNoteColorKey];
  123. break;
  124. case CPDFViewAnnotationModeStrikeout:
  125. color = [userDefaults PDFListViewColorForKey:CStrikeOutNoteColorKey];
  126. break;
  127. case CPDFViewAnnotationModeSquiggly:
  128. color = [userDefaults PDFListViewColorForKey:CSquigglyNoteColorKey];
  129. break;
  130. case CPDFViewAnnotationModeLine:
  131. color = [userDefaults PDFListViewColorForKey:CLineNoteColorKey];
  132. break;
  133. case CPDFViewAnnotationModeArrow:
  134. color = [userDefaults PDFListViewColorForKey:CArrowNoteColorKey];
  135. break;
  136. case CPDFViewAnnotationModeInk:
  137. color = [CPDFKitConfig sharedInstance].freehandAnnotationColor;
  138. break;
  139. default:
  140. break;
  141. }
  142. }
  143. return color;
  144. }
  145. - (void)setColor:(UIColor *)color {
  146. if (self.isSelectAnnot) {
  147. for (CPDFAnnotation *annotation in self.annotations) {
  148. annotation.color = color;
  149. }
  150. } else {
  151. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  152. switch (self.annotMode) {
  153. case CPDFViewAnnotationModeNote:
  154. [userDefaults setPDFListViewColor:color forKey:CAnchoredNoteColorKey];
  155. break;
  156. case CPDFViewAnnotationModeCircle:
  157. [userDefaults setPDFListViewColor:color forKey:CCircleNoteColorKey];
  158. break;
  159. case CPDFViewAnnotationModeSquare:
  160. [userDefaults setPDFListViewColor:color forKey:CSquareNoteColorKey];
  161. break;
  162. case CPDFViewAnnotationModeHighlight:
  163. [userDefaults setPDFListViewColor:color forKey:CHighlightNoteColorKey];
  164. break;
  165. case CPDFViewAnnotationModeUnderline:
  166. [userDefaults setPDFListViewColor:color forKey:CUnderlineNoteColorKey];
  167. break;
  168. case CPDFViewAnnotationModeStrikeout:
  169. [userDefaults setPDFListViewColor:color forKey:CStrikeOutNoteColorKey];
  170. break;
  171. case CPDFViewAnnotationModeSquiggly:
  172. [userDefaults setPDFListViewColor:color forKey:CSquigglyNoteColorKey];
  173. break;
  174. case CPDFViewAnnotationModeLine:
  175. [userDefaults setPDFListViewColor:color forKey:CLineNoteColorKey];
  176. break;
  177. case CPDFViewAnnotationModeArrow:
  178. [userDefaults setPDFListViewColor:color forKey:CArrowNoteColorKey];
  179. break;
  180. case CPDFViewAnnotationModeInk:
  181. [[CPDFKitConfig sharedInstance] setFreehandAnnotationColor:color];
  182. [userDefaults setPDFListViewColor:color forKey:CInkNoteColorKey];
  183. break;
  184. default:
  185. break;
  186. }
  187. [userDefaults synchronize];
  188. }
  189. }
  190. - (CGFloat)opacity {
  191. CGFloat opacity = 0;
  192. if (self.isSelectAnnot) {
  193. opacity = self.annotation.opacity;
  194. } else {
  195. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  196. switch (self.annotMode) {
  197. case CPDFViewAnnotationModeFreeText:
  198. opacity = [userDefaults floatForKey:CFreeTextNoteOpacityKey];
  199. break;
  200. case CPDFViewAnnotationModeNote:
  201. opacity = [userDefaults floatForKey:CAnchoredNoteOpacityKey];
  202. break;
  203. case CPDFViewAnnotationModeCircle:
  204. opacity = [userDefaults floatForKey:CCircleNoteOpacityKey];
  205. break;
  206. case CPDFViewAnnotationModeSquare:
  207. opacity = [userDefaults floatForKey:CSquareNoteOpacityKey];
  208. break;
  209. case CPDFViewAnnotationModeHighlight:
  210. opacity = [userDefaults floatForKey:CHighlightNoteOpacityKey];
  211. break;
  212. case CPDFViewAnnotationModeUnderline:
  213. opacity = [userDefaults floatForKey:CUnderlineNoteOpacityKey];
  214. break;
  215. case CPDFViewAnnotationModeStrikeout:
  216. opacity = [userDefaults floatForKey:CStrikeOutNoteOpacityKey];
  217. break;
  218. case CPDFViewAnnotationModeSquiggly:
  219. opacity = [userDefaults floatForKey:CSquigglyNoteOpacityKey];
  220. break;
  221. case CPDFViewAnnotationModeLine:
  222. opacity = [userDefaults floatForKey:CLineNoteOpacityKey];
  223. break;
  224. case CPDFViewAnnotationModeArrow:
  225. opacity = [userDefaults floatForKey:CArrowNoteOpacityKey];
  226. break;
  227. case CPDFViewAnnotationModeInk:
  228. opacity = [CPDFKitConfig sharedInstance].freehandAnnotationOpacity;
  229. opacity/=100;
  230. break;
  231. default:
  232. break;
  233. }
  234. }
  235. return opacity;
  236. }
  237. - (void)setOpacity:(CGFloat)opacity {
  238. if (self.isSelectAnnot) {
  239. for (CPDFAnnotation *annotation in self.annotations) {
  240. [annotation setOpacity:opacity];
  241. }
  242. } else {
  243. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  244. switch (self.annotMode) {
  245. case CPDFViewAnnotationModeFreeText:
  246. [userDefaults setFloat:opacity forKey:CFreeTextNoteOpacityKey];
  247. break;
  248. case CPDFViewAnnotationModeNote:
  249. [userDefaults setFloat:opacity forKey:CAnchoredNoteOpacityKey];
  250. break;
  251. case CPDFViewAnnotationModeCircle:
  252. [userDefaults setFloat:opacity forKey:CCircleNoteOpacityKey];
  253. break;
  254. case CPDFViewAnnotationModeSquare:
  255. [userDefaults setFloat:opacity forKey:CSquareNoteOpacityKey];
  256. break;
  257. case CPDFViewAnnotationModeHighlight:
  258. [userDefaults setFloat:opacity forKey:CHighlightNoteOpacityKey];
  259. break;
  260. case CPDFViewAnnotationModeUnderline:
  261. [userDefaults setFloat:opacity forKey:CUnderlineNoteOpacityKey];
  262. break;
  263. case CPDFViewAnnotationModeStrikeout:
  264. [userDefaults setFloat:opacity forKey:CStrikeOutNoteOpacityKey];
  265. break;
  266. case CPDFViewAnnotationModeSquiggly:
  267. [userDefaults setFloat:opacity forKey:CSquigglyNoteOpacityKey];
  268. break;
  269. case CPDFViewAnnotationModeLine:
  270. [userDefaults setFloat:opacity forKey:CLineNoteOpacityKey];
  271. break;
  272. case CPDFViewAnnotationModeArrow:
  273. [userDefaults setFloat:opacity forKey:CArrowNoteOpacityKey];
  274. break;
  275. case CPDFViewAnnotationModeInk:
  276. [[CPDFKitConfig sharedInstance] setFreehandAnnotationOpacity:opacity * 100];
  277. [userDefaults setFloat:opacity forKey:CInkNoteOpacityKey];
  278. break;
  279. default:
  280. break;
  281. }
  282. [userDefaults synchronize];
  283. }
  284. }
  285. - (CPDFBorderStyle)style {
  286. CPDFBorderStyle style = CPDFBorderStyleSolid;
  287. if (self.isSelectAnnot) {
  288. style = self.annotation.borderStyle;
  289. } else {
  290. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  291. switch (self.annotMode) {
  292. case CPDFViewAnnotationModeFreeText:
  293. style = [userDefaults integerForKey:CFreeTextNoteLineStyleKey];
  294. break;
  295. case CPDFViewAnnotationModeCircle:
  296. style = [userDefaults integerForKey:CCircleNoteLineStyleKey];
  297. break;
  298. case CPDFViewAnnotationModeSquare:
  299. style = [userDefaults integerForKey:CSquareNoteLineStyleKey];
  300. break;
  301. case CPDFViewAnnotationModeLine:
  302. style = [userDefaults integerForKey:CLineNoteLineStyleKey];
  303. break;
  304. case CPDFViewAnnotationModeArrow:
  305. style = [userDefaults integerForKey:CArrowNoteLineStyleKey];
  306. break;
  307. case CPDFViewAnnotationModeInk:
  308. style = [userDefaults integerForKey:CInkNoteLineStyleyKey];
  309. break;
  310. default:
  311. break;
  312. }
  313. }
  314. return style;
  315. }
  316. - (void)setStyle:(CPDFBorderStyle)style {
  317. if (self.isSelectAnnot) {
  318. for (CPDFAnnotation *annotation in self.annotations) {
  319. [annotation setBorderStyle:style];
  320. }
  321. } else {
  322. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  323. switch (self.annotMode) {
  324. case CPDFViewAnnotationModeFreeText:
  325. [userDefaults setInteger:style forKey:CFreeTextNoteLineStyleKey];
  326. break;
  327. case CPDFViewAnnotationModeCircle:
  328. [userDefaults setInteger:style forKey:CCircleNoteLineStyleKey];
  329. break;
  330. case CPDFViewAnnotationModeSquare:
  331. [userDefaults setInteger:style forKey:CSquareNoteLineStyleKey];
  332. break;
  333. case CPDFViewAnnotationModeLine:
  334. [userDefaults setInteger:style forKey:CLineNoteLineStyleKey];
  335. break;
  336. case CPDFViewAnnotationModeArrow:
  337. [userDefaults setInteger:style forKey:CArrowNoteLineStyleKey];
  338. break;
  339. case CPDFViewAnnotationModeInk:
  340. [userDefaults setInteger:style forKey:CInkNoteLineStyleyKey];
  341. break;
  342. default:
  343. break;
  344. }
  345. [userDefaults synchronize];
  346. }
  347. }
  348. - (NSArray *)dashPattern {
  349. if (self.isSelectAnnot) {
  350. if(CPDFBorderStyleDashed == self.annotation.border.style) {
  351. return self.annotation.dashPattern;
  352. } else {
  353. return 0;
  354. }
  355. } else {
  356. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  357. NSInteger dashPattern = 0;
  358. switch (self.annotMode) {
  359. case CPDFViewAnnotationModeFreeText:
  360. dashPattern = [userDefaults integerForKey:CFreeTextNoteDashPatternKey];
  361. break;
  362. case CPDFViewAnnotationModeCircle:
  363. dashPattern = [userDefaults integerForKey:CCircleNoteDashPatternKey];
  364. break;
  365. case CPDFViewAnnotationModeSquare:
  366. dashPattern = [userDefaults integerForKey:CSquareNoteDashPatternKey];
  367. break;
  368. case CPDFViewAnnotationModeLine:
  369. dashPattern = [userDefaults integerForKey:CLineNoteDashPatternKey];
  370. break;
  371. case CPDFViewAnnotationModeArrow:
  372. dashPattern = [userDefaults integerForKey:CArrowNoteDashPatternKey];
  373. break;
  374. case CPDFViewAnnotationModeInk:
  375. dashPattern = [userDefaults integerForKey:CInkNoteDashPatternKey];
  376. break;
  377. default:
  378. break;
  379. }
  380. if(CPDFBorderStyleDashed != self.style) {
  381. dashPattern= 0;
  382. }
  383. return @[@(dashPattern)];
  384. }
  385. }
  386. - (void)setDashPattern:(NSArray *)dashPatterns {
  387. if (self.isSelectAnnot) {
  388. for (CPDFAnnotation *annotation in self.annotations) {
  389. [annotation setDashPattern:dashPatterns];
  390. }
  391. } else {
  392. NSInteger dashPattern = [dashPatterns.firstObject integerValue];
  393. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  394. switch (self.annotMode) {
  395. case CPDFViewAnnotationModeFreeText:
  396. [userDefaults setInteger:dashPattern forKey:CFreeTextNoteDashPatternKey];
  397. break;
  398. case CPDFViewAnnotationModeCircle:
  399. [userDefaults setInteger:dashPattern forKey:CCircleNoteDashPatternKey];
  400. break;
  401. case CPDFViewAnnotationModeSquare:
  402. [userDefaults setInteger:dashPattern forKey:CSquareNoteDashPatternKey];
  403. break;
  404. case CPDFViewAnnotationModeLine:
  405. [userDefaults setInteger:dashPattern forKey:CLineNoteDashPatternKey];
  406. break;
  407. case CPDFViewAnnotationModeArrow:
  408. [userDefaults setInteger:dashPattern forKey:CArrowNoteDashPatternKey];
  409. break;
  410. case CPDFViewAnnotationModeInk:
  411. [userDefaults setInteger:dashPattern forKey:CInkNoteDashPatternKey];
  412. break;
  413. default:
  414. break;
  415. }
  416. [userDefaults synchronize];
  417. }
  418. }
  419. - (CGFloat)lineWidth {
  420. CGFloat lineWidth = 0;
  421. if (self.isSelectAnnot) {
  422. lineWidth = self.annotation.lineWidth;
  423. } else {
  424. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  425. switch (self.annotMode) {
  426. case CPDFViewAnnotationModeCircle:
  427. lineWidth = [userDefaults floatForKey:CCircleNoteLineWidthKey];
  428. break;
  429. case CPDFViewAnnotationModeSquare:
  430. lineWidth = [userDefaults floatForKey:CSquareNoteLineWidthKey];
  431. break;
  432. case CPDFViewAnnotationModeLine:
  433. lineWidth = [userDefaults floatForKey:CLineNoteLineWidthKey];
  434. break;
  435. case CPDFViewAnnotationModeArrow:
  436. lineWidth = [userDefaults floatForKey:CArrowNoteLineWidthKey];
  437. break;
  438. case CPDFViewAnnotationModeInk:
  439. lineWidth = [CPDFKitConfig sharedInstance].freehandAnnotationBorderWidth;
  440. break;
  441. default:
  442. break;
  443. }
  444. }
  445. return lineWidth;
  446. }
  447. - (void)setLineWidth:(CGFloat)lineWidth {
  448. if (self.isSelectAnnot) {
  449. for (CPDFAnnotation *annotation in self.annotations) {
  450. [annotation setBorderWidth:lineWidth];
  451. }
  452. } else {
  453. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  454. switch (self.annotMode) {
  455. case CPDFViewAnnotationModeCircle:
  456. [userDefaults setFloat:lineWidth forKey:CCircleNoteLineWidthKey];
  457. break;
  458. case CPDFViewAnnotationModeSquare:
  459. [userDefaults setFloat:lineWidth forKey:CSquareNoteLineWidthKey];
  460. break;
  461. case CPDFViewAnnotationModeLine:
  462. [userDefaults setFloat:lineWidth forKey:CLineNoteLineWidthKey];
  463. break;
  464. case CPDFViewAnnotationModeArrow:
  465. [userDefaults setFloat:lineWidth forKey:CArrowNoteLineWidthKey];
  466. break;
  467. case CPDFViewAnnotationModeInk:
  468. [[CPDFKitConfig sharedInstance] setFreehandAnnotationBorderWidth:lineWidth];
  469. [userDefaults setFloat:lineWidth forKey:CInkNoteLineWidthKey];
  470. break;
  471. default:
  472. break;
  473. }
  474. [userDefaults synchronize];
  475. }
  476. }
  477. - (CPDFLineStyle)startLineStyle {
  478. CPDFLineStyle startLineStyle = CPDFLineStyleNone;
  479. if (self.isSelectAnnot) {
  480. if ([self.annotation isKindOfClass:[CPDFLineAnnotation class]]) {
  481. startLineStyle = [(CPDFLineAnnotation *)self.annotation startLineStyle];
  482. }
  483. } else {
  484. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  485. switch (self.annotMode) {
  486. case CPDFViewAnnotationModeArrow:
  487. startLineStyle = [userDefaults integerForKey:CArrowNoteStartStyleKey];
  488. break;
  489. case CPDFViewAnnotationModeLine:
  490. startLineStyle = [userDefaults integerForKey:CLineNoteStartStyleKey];
  491. break;
  492. default:
  493. break;
  494. }
  495. }
  496. return startLineStyle;
  497. }
  498. - (void)setStartLineStyle:(CPDFLineStyle)startLineStyle {
  499. if(self.isSelectAnnot) {
  500. for (CPDFAnnotation *annotation in self.annotations) {
  501. if([annotation isKindOfClass:[CPDFLineAnnotation class]]){
  502. [(CPDFLineAnnotation*)annotation setStartLineStyle:startLineStyle];
  503. }
  504. }
  505. } else {
  506. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  507. switch (self.annotMode) {
  508. case CPDFViewAnnotationModeArrow:
  509. [userDefaults setInteger:startLineStyle forKey:CArrowNoteStartStyleKey];
  510. break;
  511. case CPDFViewAnnotationModeLine:
  512. [userDefaults setInteger:startLineStyle forKey:CLineNoteStartStyleKey];
  513. break;
  514. default:
  515. break;
  516. }
  517. [userDefaults synchronize];
  518. }
  519. }
  520. - (CPDFLineStyle)endLineStyle {
  521. CPDFLineStyle endLineStyle = CPDFLineStyleNone;
  522. if(self.isSelectAnnot) {
  523. if ([self.annotation isKindOfClass:[CPDFLineAnnotation class]]) {
  524. endLineStyle = [(CPDFLineAnnotation *)self.annotation endLineStyle];
  525. }
  526. } else {
  527. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  528. switch (self.annotMode) {
  529. case CPDFViewAnnotationModeArrow:
  530. endLineStyle = [userDefaults integerForKey:CArrowNoteEndStyleKey];
  531. break;
  532. case CPDFViewAnnotationModeLine:
  533. endLineStyle = [userDefaults integerForKey:CLineNoteEndStyleKey];
  534. break;
  535. default:
  536. break;
  537. }
  538. }
  539. return endLineStyle;
  540. }
  541. -(void)setEndLineStyle:(CPDFLineStyle)endLineStyle {
  542. if(self.isSelectAnnot) {
  543. for (CPDFAnnotation *annotation in self.annotations) {
  544. if([annotation isKindOfClass:[CPDFLineAnnotation class]]){
  545. [(CPDFLineAnnotation*)annotation setEndLineStyle:endLineStyle];
  546. }
  547. }
  548. } else {
  549. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  550. switch (self.annotMode) {
  551. case CPDFViewAnnotationModeArrow:
  552. [userDefaults setInteger:endLineStyle forKey:CArrowNoteEndStyleKey];
  553. break;
  554. case CPDFViewAnnotationModeLine:
  555. [userDefaults setInteger:endLineStyle forKey:CLineNoteEndStyleKey];
  556. break;
  557. default:
  558. break;
  559. }
  560. [userDefaults synchronize];
  561. }
  562. }
  563. #pragma mark - FreeText
  564. - (UIColor *)fontColor {
  565. UIColor *color = nil;
  566. if (self.isSelectAnnot) {
  567. if([self.annotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
  568. color = [(CPDFFreeTextAnnotation *)self.annotation fontColor];
  569. }
  570. } else {
  571. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  572. switch (self.annotMode) {
  573. case CPDFViewAnnotationModeFreeText:
  574. color = [userDefaults PDFListViewColorForKey:CFreeTextNoteFontColorKey];
  575. break;
  576. default:
  577. break;
  578. }
  579. }
  580. return color;
  581. }
  582. - (void)setFontColor:(UIColor *)fontColor {
  583. if (self.isSelectAnnot) {
  584. for (CPDFAnnotation *annotation in self.annotations) {
  585. if ([annotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
  586. CGFloat red,green,blue,alpha;
  587. [fontColor getRed:&red green:&green blue:&blue alpha:&alpha];
  588. [(CPDFFreeTextAnnotation *)annotation setFontColor:[UIColor colorWithRed:red green:green blue:blue alpha:self.opacity]];
  589. }
  590. }
  591. } else {
  592. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  593. switch (self.annotMode) {
  594. case CPDFViewAnnotationModeFreeText:
  595. [userDefaults setPDFListViewColor:fontColor forKey:CFreeTextNoteFontColorKey];
  596. break;
  597. default:
  598. break;
  599. }
  600. [userDefaults synchronize];
  601. }
  602. }
  603. - (CGFloat)fontSize {
  604. CGFloat fontSize = 11;
  605. if (self.isSelectAnnot) {
  606. if ([self.annotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
  607. fontSize = [(CPDFFreeTextAnnotation *)self.annotation font].pointSize;
  608. }
  609. } else {
  610. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  611. switch (self.annotMode) {
  612. case CPDFViewAnnotationModeFreeText:
  613. fontSize = [userDefaults floatForKey:CFreeTextNoteFontSizeKey];
  614. break;
  615. default:
  616. break;
  617. }
  618. }
  619. return fontSize;
  620. }
  621. - (void)setFontSize:(CGFloat)fontSize {
  622. if (self.isSelectAnnot) {
  623. for (CPDFAnnotation *annotation in self.annotations) {
  624. if ([annotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
  625. CPDFFreeTextAnnotation *freeText = (CPDFFreeTextAnnotation *)annotation;
  626. UIFont *font = [(CPDFFreeTextAnnotation *)annotation font];
  627. [(CPDFFreeTextAnnotation *)annotation setFont:[UIFont fontWithName:font.fontName size:fontSize]];
  628. NSDictionary *attributes = @{NSFontAttributeName : freeText.font};
  629. CGRect bounds = annotation.bounds;
  630. CGRect rect = [freeText.contents boundingRectWithSize:CGSizeMake(bounds.size.width, CGFLOAT_MAX)
  631. options:NSStringDrawingUsesLineFragmentOrigin
  632. attributes:attributes
  633. context:nil];
  634. rect.size.height += 6;
  635. bounds.origin.y = CGRectGetMaxY(bounds) - rect.size.height;
  636. bounds.size.height = rect.size.height;
  637. //
  638. annotation.bounds = bounds;
  639. }
  640. }
  641. } else {
  642. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  643. switch (self.annotMode) {
  644. case CPDFViewAnnotationModeFreeText:
  645. [userDefaults setFloat:fontSize forKey:CFreeTextNoteFontSizeKey];
  646. break;
  647. default:
  648. break;
  649. }
  650. [userDefaults synchronize];
  651. }
  652. }
  653. - (NSString *)fontName {
  654. NSString *fontName = nil;
  655. if (self.isSelectAnnot) {
  656. if ([self.annotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
  657. fontName = [(CPDFFreeTextAnnotation *)self.annotation font].fontName;
  658. }
  659. } else {
  660. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  661. switch (self.annotMode) {
  662. case CPDFViewAnnotationModeFreeText:
  663. fontName = [userDefaults objectForKey:CFreeTextNoteFontNameKey];
  664. break;
  665. default:
  666. break;
  667. }
  668. }
  669. return fontName;
  670. }
  671. - (void)setFontName:(NSString *)fontName {
  672. if (self.isSelectAnnot) {
  673. for (CPDFAnnotation *annotation in self.annotations) {
  674. if ([annotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
  675. UIFont *font = [(CPDFFreeTextAnnotation *)annotation font];
  676. [(CPDFFreeTextAnnotation *)annotation setFont:[UIFont fontWithName:fontName size:font.pointSize]];
  677. }
  678. }
  679. } else {
  680. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  681. switch (self.annotMode) {
  682. case CPDFViewAnnotationModeFreeText:
  683. [userDefaults setObject:fontName forKey:CFreeTextNoteFontNameKey];
  684. break;
  685. default:
  686. break;
  687. }
  688. [userDefaults synchronize];
  689. }
  690. }
  691. - (NSTextAlignment)alignment {
  692. NSTextAlignment alignment = NSTextAlignmentLeft;
  693. if (self.isSelectAnnot) {
  694. if ([self.annotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
  695. alignment = [(CPDFFreeTextAnnotation *)self.annotation alignment];
  696. }
  697. } else {
  698. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  699. switch (self.annotMode) {
  700. case CPDFViewAnnotationModeFreeText:
  701. alignment = [userDefaults integerForKey:CFreeTextNoteAlignmentKey];
  702. break;
  703. default:
  704. break;
  705. }
  706. }
  707. return alignment;
  708. }
  709. - (void)setAlignment:(NSTextAlignment)alignment {
  710. if (self.isSelectAnnot){
  711. for (CPDFAnnotation *annotation in self.annotations) {
  712. if ([annotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
  713. [(CPDFFreeTextAnnotation *)annotation setAlignment:alignment];
  714. }
  715. }
  716. } else {
  717. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  718. switch (self.annotMode) {
  719. case CPDFViewAnnotationModeFreeText:
  720. [userDefaults setInteger:alignment forKey:CFreeTextNoteAlignmentKey];
  721. break;
  722. default:
  723. break;
  724. }
  725. [userDefaults synchronize];
  726. }
  727. }
  728. #pragma mark - Circle&Square
  729. - (UIColor *)interiorColor {
  730. UIColor *interiorColor = nil;
  731. if (self.isSelectAnnot) {
  732. if([self.annotation isKindOfClass:[CPDFCircleAnnotation class]] ||
  733. [self.annotation isKindOfClass:[CPDFSquareAnnotation class]]) {
  734. interiorColor = [(CPDFCircleAnnotation *)self.annotation interiorColor];
  735. } else if ([self.annotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
  736. interiorColor = [(CPDFFreeTextAnnotation *)self.annotation color];
  737. }
  738. } else {
  739. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  740. switch (self.annotMode) {
  741. case CPDFViewAnnotationModeCircle:
  742. interiorColor = [userDefaults PDFListViewColorForKey:CCircleNoteInteriorColorKey];
  743. break;
  744. case CPDFViewAnnotationModeSquare:
  745. interiorColor = [userDefaults PDFListViewColorForKey:CSquareNoteInteriorColorKey];
  746. break;
  747. default:
  748. break;
  749. }
  750. }
  751. return interiorColor;
  752. }
  753. - (void)setInteriorColor:(UIColor *)interiorColor {
  754. if (self.isSelectAnnot){
  755. for (CPDFAnnotation *annotation in self.annotations) {
  756. if ([annotation isKindOfClass:[CPDFCircleAnnotation class]] ||
  757. [annotation isKindOfClass:[CPDFSquareAnnotation class]]) {
  758. [(CPDFCircleAnnotation *)annotation setInteriorColor:interiorColor];
  759. } else if ([annotation isKindOfClass:[CPDFFreeTextAnnotation class]]) {
  760. [(CPDFFreeTextAnnotation *)annotation setColor:interiorColor];
  761. }
  762. }
  763. } else {
  764. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  765. switch (self.annotMode) {
  766. case CPDFViewAnnotationModeCircle:
  767. [userDefaults setPDFListViewColor:interiorColor forKey:CCircleNoteInteriorColorKey];
  768. break;
  769. case CPDFViewAnnotationModeSquare:
  770. [userDefaults setPDFListViewColor:interiorColor forKey:CSquareNoteInteriorColorKey];
  771. break;
  772. default:
  773. break;
  774. }
  775. }
  776. }
  777. - (CGFloat)interiorOpacity {
  778. CGFloat opacity = 0;
  779. if (self.isSelectAnnot) {
  780. if([self.annotation isKindOfClass:[CPDFCircleAnnotation class]] ||
  781. [self.annotation isKindOfClass:[CPDFSquareAnnotation class]]) {
  782. opacity = [(CPDFCircleAnnotation *)self.annotation interiorOpacity];
  783. } else {
  784. opacity = [self.annotation opacity];
  785. }
  786. } else {
  787. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  788. switch (self.annotMode) {
  789. case CPDFViewAnnotationModeCircle:
  790. opacity = [userDefaults floatForKey:CCircleNoteInteriorOpacityKey];
  791. break;
  792. case CPDFViewAnnotationModeSquare:
  793. opacity = [userDefaults floatForKey:CSquareNoteInteriorOpacityKey];
  794. break;
  795. default:
  796. break;
  797. }
  798. }
  799. return opacity;
  800. }
  801. - (void)setInteriorOpacity:(CGFloat)interiorOpacity {
  802. if (self.isSelectAnnot) {
  803. for (CPDFAnnotation *annotation in self.annotations) {
  804. if ([annotation isKindOfClass:[CPDFCircleAnnotation class]] ||
  805. [annotation isKindOfClass:[CPDFSquareAnnotation class]]) {
  806. [(CPDFCircleAnnotation *)annotation setInteriorOpacity:interiorOpacity];
  807. } else {
  808. [(CPDFCircleAnnotation *)annotation setOpacity:interiorOpacity];
  809. }
  810. }
  811. } else {
  812. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  813. switch (self.annotMode) {
  814. case CPDFViewAnnotationModeCircle:
  815. [userDefaults setFloat:interiorOpacity forKey:CCircleNoteInteriorOpacityKey];
  816. break;
  817. case CPDFViewAnnotationModeSquare:
  818. [userDefaults setFloat:interiorOpacity forKey:CSquareNoteInteriorOpacityKey];
  819. break;
  820. default:
  821. break;
  822. }
  823. [userDefaults synchronize];
  824. }
  825. }
  826. @end