CPDFEditViewController.m 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. //
  2. // CPDFEditViewController.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. //
  11. #import "CPDFEditViewController.h"
  12. #import "CPDFImagePropertyCell.h"
  13. #import "CPDFTextPropertyCell.h"
  14. #import "CPDFColorPickerView.h"
  15. #import "CPDFEditFontNameSelectView.h"
  16. #import <ComPDFKit/ComPDFKit.h>
  17. #import "CPDFColorUtils.h"
  18. #import "CPDFEditTextSampleView.h"
  19. #import "CPDFEditImageSampleView.h"
  20. #import "CPDFTextProperty.h"
  21. @interface CPDFEditViewController ()<UITableViewDelegate,UITableViewDataSource,CPDFColorPickerViewDelegate,CPDFEditFontNameSelectViewDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate,UIColorPickerViewControllerDelegate>
  22. @property (nonatomic, strong) UIView * splitView;
  23. @property (nonatomic, strong) UITableView * tableView;
  24. @property (nonatomic, strong) CPDFColorPickerView * colorPickerView;
  25. @property (nonatomic, strong) CPDFEditFontNameSelectView * fontSelectView;
  26. @property (nonatomic, strong) UIButton *backBtn;
  27. @property (nonatomic, strong) CPDFEditTextSampleView * textSampleView;
  28. @property (nonatomic, strong) CPDFEditImageSampleView * imageSampleView;
  29. @end
  30. @implementation CPDFEditViewController
  31. #pragma mark - Initializers
  32. - (instancetype)initWithPDFView:(CPDFView *)pdfView {
  33. if (self = [super init]) {
  34. _pdfView = pdfView;
  35. }
  36. return self;
  37. }
  38. #pragma mark - ViewController Methods
  39. - (void)viewDidLoad {
  40. [super viewDidLoad];
  41. CGFloat topPadding = 0;
  42. CGFloat bottomPadding = 0;
  43. CGFloat leftPadding = 0;
  44. CGFloat rightPadding = 0;
  45. if (@available(iOS 11.0, *)) {
  46. UIWindow *window = UIApplication.sharedApplication.windows.firstObject;
  47. topPadding = window.safeAreaInsets.top;
  48. bottomPadding = window.safeAreaInsets.bottom;
  49. leftPadding = window.safeAreaInsets.left;
  50. rightPadding = window.safeAreaInsets.right;
  51. }
  52. self.view.frame = CGRectMake(leftPadding, [UIScreen mainScreen].bounds.size.height - bottomPadding , [UIScreen mainScreen].bounds.size.width - leftPadding - rightPadding,self.view.frame.size.height);
  53. // Do any additional setup after loading the view.
  54. self.titleLabel = [[UILabel alloc] init];
  55. self.titleLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
  56. if(self.editMode == CPDFEditModeText){
  57. self.titleLabel.text = NSLocalizedString(@"Text Properties", nil);
  58. }else{
  59. self.titleLabel.text = NSLocalizedString(@"Image Properties", nil);
  60. }
  61. self.titleLabel.font = [UIFont systemFontOfSize:20];
  62. self.titleLabel.adjustsFontSizeToFitWidth = YES;
  63. [self.view addSubview:self.titleLabel];
  64. self.backBtn = [[UIButton alloc] init];
  65. self.backBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  66. [self.backBtn setImage:[UIImage imageNamed:@"CPDFEditClose" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  67. [self.backBtn addTarget:self action:@selector(buttonItemClicked_back:) forControlEvents:UIControlEventTouchUpInside];
  68. [self.view addSubview:self.backBtn];
  69. self.splitView = [[UIView alloc] init];
  70. self.splitView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1];
  71. [self.view addSubview:self.splitView];
  72. self.view.backgroundColor = [CPDFColorUtils CAnnotationPropertyViewControllerBackgoundColor];
  73. [self updatePreferredContentSizeWithTraitCollection:self.traitCollection];
  74. self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  75. self.tableView.delegate = self;
  76. self.tableView.dataSource = self;
  77. if (@available(iOS 15.0, *)) {
  78. self.tableView.sectionHeaderTopPadding = 0;
  79. } else {
  80. // Fallback on earlier versions
  81. }
  82. [self.tableView reloadData];
  83. [self.view addSubview:self.tableView];
  84. self.tableView.backgroundColor = [CPDFColorUtils CAnnotationSampleBackgoundColor];
  85. }
  86. - (void)viewWillLayoutSubviews {
  87. if (@available(iOS 11.0, *)) {
  88. self.titleLabel.frame = CGRectMake((self.view.frame.size.width - 120)/2, 5, 120, 50);
  89. self.splitView.frame = CGRectMake(self.view.safeAreaInsets.left, 51, self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, 1);
  90. self.tableView.frame = CGRectMake(self.view.safeAreaInsets.left, 52, self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, self.view.frame.size.height - 52);
  91. self.backBtn.frame = CGRectMake(self.view.frame.size.width - 60, 5, 50, 50);
  92. } else {
  93. self.titleLabel.frame = CGRectMake((self.view.frame.size.width - 120)/2, 5, 120, 50);
  94. self.splitView.frame = CGRectMake(0, 51, self.view.frame.size.width, 1);
  95. self.tableView.frame = CGRectMake(0, 52, self.view.frame.size.width, self.view.frame.size.height - 52);
  96. self.backBtn.frame = CGRectMake(self.view.frame.size.width - 60, 5, 50, 50);
  97. }
  98. }
  99. - (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  100. [super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];
  101. [self updatePreferredContentSizeWithTraitCollection:newCollection];
  102. }
  103. - (void)updatePreferredContentSizeWithTraitCollection:(UITraitCollection *)traitCollection
  104. {
  105. if(self.editMode == CPDFEditModeText){
  106. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact ? 300 : 600);
  107. }else{
  108. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact ? 300 : 600);
  109. }
  110. }
  111. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  112. if(self.editMode == CPDFEditModeText){
  113. UIView * view = [[UIView alloc] initWithFrame:CGRectMake(20, 0, self.view.bounds.size.width-40, 120)];
  114. view.backgroundColor = [UIColor whiteColor];
  115. view.layer.borderWidth = 1.0;
  116. view.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1].CGColor;
  117. view.backgroundColor = [CPDFColorUtils CAnnotationSampleBackgoundColor];
  118. self.textSampleView = [[CPDFEditTextSampleView alloc] init];
  119. self.textSampleView.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1].CGColor;
  120. self.textSampleView.layer.borderWidth = 1.0;
  121. self.textSampleView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
  122. self.textSampleView.frame = CGRectMake((self.view.frame.size.width - 300)/2, 15, 300, view.bounds.size.height - 30);
  123. CPDFEditArea *editArea = self.pdfView.editingArea;
  124. if(editArea) {
  125. if ([editArea IsTextArea]) {
  126. CPDFEditTextArea *editTextArea = (CPDFEditTextArea *)editArea;
  127. self.textSampleView.textAlignmnet = [self.pdfView editingSelectionAlignmentWithTextArea:editTextArea];
  128. self.textSampleView.textColor = [self.pdfView editingSelectionFontColorWithTextArea:editTextArea];
  129. self.textSampleView.textOpacity = [self.pdfView getCurrentOpacity];
  130. self.textSampleView.fontName = [self.pdfView editingSelectionFontNameWithTextArea:editTextArea];
  131. self.textSampleView.isBold = [self.pdfView isBoldCurrentSelectionWithTextArea:editTextArea];;
  132. self.textSampleView.isItalic = [self.pdfView isItalicCurrentSelectionWithTextArea:editTextArea];
  133. self.textSampleView.fontSize = [self.pdfView editingSelectionFontSizesWithTextArea:editTextArea];
  134. }
  135. } else {
  136. self.textSampleView.textAlignmnet = [CPDFTextProperty sharedManager].textAlignment;
  137. self.textSampleView.textColor = [CPDFTextProperty sharedManager].fontColor;
  138. self.textSampleView.textOpacity = [CPDFTextProperty sharedManager].textOpacity;
  139. self.textSampleView.fontName = [CPDFTextProperty sharedManager].fontName;
  140. self.textSampleView.isBold = [CPDFTextProperty sharedManager].isBold;
  141. self.textSampleView.isItalic = [CPDFTextProperty sharedManager].isItalic;
  142. self.textSampleView.fontSize = [CPDFTextProperty sharedManager].fontSize;
  143. }
  144. [view addSubview:self.textSampleView];
  145. return view;
  146. } else {
  147. UIView * view = [[UIView alloc] initWithFrame:CGRectMake(20, 0, self.view.bounds.size.width-40, 120)];
  148. view.backgroundColor = [UIColor whiteColor];
  149. view.layer.borderWidth = 1.0;
  150. view.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1].CGColor;
  151. view.backgroundColor = [CPDFColorUtils CAnnotationSampleBackgoundColor];
  152. self.imageSampleView = [[CPDFEditImageSampleView alloc] init];
  153. self.imageSampleView.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1].CGColor;
  154. self.imageSampleView.layer.borderWidth = 1.0;
  155. self.imageSampleView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
  156. self.imageSampleView.frame = CGRectMake((self.view.frame.size.width - 300)/2, 15, 300, view.bounds.size.height - 30);
  157. UIImage *image = nil;
  158. if(self.pdfView.editingArea.IsImageArea) {
  159. CPDFEditImageArea *editImageArea = (CPDFEditImageArea *)self.pdfView.editingArea;
  160. image = [editImageArea thumbnailImageWithSize:editImageArea.bounds.size];
  161. }
  162. if(image) {
  163. self.imageSampleView.imageView.image = image;
  164. } else {
  165. if ([self.pdfView getRotationEditArea:(CPDFEditImageArea *)self.pdfView.editingArea] > 0) {
  166. if ([self.pdfView getRotationEditArea:(CPDFEditImageArea *)self.pdfView.editingArea] > 90) {
  167. self.imageSampleView.imageView.transform = CGAffineTransformRotate(self.imageSampleView.imageView.transform, M_PI);
  168. } else {
  169. self.imageSampleView.imageView.transform = CGAffineTransformRotate(self.imageSampleView.imageView.transform, M_PI/2);
  170. }
  171. } else if (([self.pdfView getRotationEditArea:(CPDFEditImageArea *)self.pdfView.editingArea] < 0)) {
  172. self.imageSampleView.imageView.transform = CGAffineTransformRotate(self.imageSampleView.imageView.transform, -M_PI/2);
  173. }
  174. // self.imageSampleView.transFormType = 0;
  175. self.imageSampleView.imageView.alpha = [self.pdfView getCurrentOpacity];
  176. }
  177. [view addSubview:self.imageSampleView];
  178. return view;
  179. }
  180. }
  181. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  182. if(self.editMode == CPDFEditModeText){
  183. return 120;
  184. }else{
  185. return 120;
  186. }
  187. }
  188. #pragma mark - UITableViewDelegate
  189. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  190. return 1;
  191. }
  192. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  193. if(self.editMode == CPDFEditModeText){
  194. CPDFTextPropertyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Textcell"];
  195. if (!cell) {
  196. cell = [[CPDFTextPropertyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Textcell"];
  197. }
  198. cell.backgroundColor = [UIColor colorWithRed:250./255 green:252/255. blue:255/255. alpha:1.];
  199. if(self.fontSelectView.fontName.length > 0){
  200. cell.currentSelectFontName = self.fontSelectView.fontName;
  201. }else{
  202. if(self.pdfView.editingArea) {
  203. cell.currentSelectFontName = [self.pdfView editingSelectionFontNameWithTextArea:self.pdfView.editingArea];
  204. } else {
  205. cell.currentSelectFontName = [CPDFTextProperty sharedManager].fontName;
  206. }
  207. }
  208. cell.pdfView = self.pdfView;
  209. __block __typeof(self) blockSelf = self;
  210. cell.actionBlock = ^(CPDFTextActionType actionType) {
  211. if(actionType == CPDFTextActionColorSelect){
  212. //Add colorSelectView
  213. if (@available(iOS 14.0, *)) {
  214. UIColorPickerViewController *picker = [[UIColorPickerViewController alloc] init];
  215. picker.delegate = blockSelf;
  216. [blockSelf presentViewController:picker animated:YES completion:nil];
  217. } else {
  218. blockSelf.colorPickerView = [[CPDFColorPickerView alloc] initWithFrame:self.view.frame];
  219. blockSelf.colorPickerView.delegate = blockSelf;
  220. blockSelf.colorPickerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  221. blockSelf.colorPickerView.backgroundColor = [CPDFColorUtils CPDFViewControllerBackgroundColor];
  222. [blockSelf.view addSubview:blockSelf.colorPickerView];
  223. }
  224. }else if(actionType == CPDFTextActionFontNameSelect) {
  225. //Add actionFontNameSelect
  226. blockSelf.fontSelectView = [[CPDFEditFontNameSelectView alloc] initWithFrame:blockSelf.view.bounds];
  227. blockSelf.fontSelectView.fontNameArr = [NSMutableArray arrayWithArray:[blockSelf.pdfView getFontList]];
  228. blockSelf.fontSelectView.fontName = blockSelf.textSampleView.fontName;
  229. blockSelf.fontSelectView.delegate = blockSelf;
  230. blockSelf.fontSelectView.backgroundColor = [CPDFColorUtils CPDFViewControllerBackgroundColor];
  231. [blockSelf.view addSubview:blockSelf.fontSelectView];
  232. }
  233. };
  234. cell.colorBlock = ^(UIColor * _Nonnull selectColor) {
  235. blockSelf.textSampleView.textColor = selectColor;
  236. if (blockSelf.pdfView.editingArea) {
  237. [blockSelf.pdfView setEditingSelectionFontColor:selectColor withTextArea:blockSelf.pdfView.editingArea];
  238. } else {
  239. [CPDFTextProperty sharedManager].fontColor = selectColor;
  240. }
  241. };
  242. __block __typeof(CPDFTextPropertyCell *) blockCell = cell;
  243. cell.boldBlock = ^(BOOL isBold) {
  244. blockSelf.textSampleView.isBold = isBold;
  245. CPDFEditArea *editingArea = blockSelf.pdfView.editingArea;
  246. if (editingArea.IsTextArea) {
  247. BOOL result = [blockSelf.pdfView setCurrentSelectionIsBold:isBold withTextArea:(CPDFEditTextArea *)editingArea];
  248. blockCell.boldBtn.selected = [blockSelf.pdfView isBoldCurrentSelectionWithTextArea:(CPDFEditTextArea *)editingArea];
  249. if(!result) {
  250. UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"ComPDFKit cannot change this font style because not all styles are available for this font.",nil) message:nil preferredStyle:UIAlertControllerStyleAlert];
  251. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  252. }];
  253. [alert addAction:okAction];
  254. [blockSelf presentViewController:alert animated:YES completion:nil];
  255. }
  256. } else {
  257. [CPDFTextProperty sharedManager].isBold = isBold;
  258. }
  259. };
  260. cell.italicBlock = ^(BOOL isItalic) {
  261. blockSelf.textSampleView.isItalic = isItalic;
  262. CPDFEditArea *editingArea = blockSelf.pdfView.editingArea;
  263. if (editingArea.IsTextArea) {
  264. BOOL result = [blockSelf.pdfView setCurrentSelectionIsItalic:isItalic withTextArea:(CPDFEditTextArea *)editingArea];
  265. blockCell.italicBtn.selected = [blockSelf.pdfView isItalicCurrentSelectionWithTextArea:(CPDFEditTextArea *)editingArea];
  266. if(!result) {
  267. UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"ComPDFKit cannot change this font style because not all styles are available for this font.",nil) message:nil preferredStyle:UIAlertControllerStyleAlert];
  268. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  269. }];
  270. [alert addAction:okAction];
  271. [blockSelf presentViewController:alert animated:YES completion:nil];
  272. }
  273. } else {
  274. [CPDFTextProperty sharedManager].isItalic = isItalic;
  275. }
  276. };
  277. cell.alignmentBlock = ^(CPDFTextAlignment alignment) {
  278. blockSelf.textSampleView.textAlignmnet = alignment;
  279. if (blockSelf.pdfView.editingArea) {
  280. [blockSelf.pdfView setCurrentSelectionAlignment:(NSTextAlignment)alignment withTextArea:blockSelf.pdfView.editingArea];
  281. } else {
  282. [CPDFTextProperty sharedManager].textAlignment = (NSTextAlignment)alignment;
  283. }
  284. };
  285. cell.fontSizeBlock = ^(CGFloat fontSize) {
  286. blockSelf.textSampleView.fontSize = fontSize * 10;
  287. if (blockSelf.pdfView.editingArea) {
  288. [blockSelf.pdfView setEditingSelectionFontSize:fontSize * 10 withTextArea:blockSelf.pdfView.editingArea isAutoSize:YES];
  289. } else {
  290. [CPDFTextProperty sharedManager].fontSize = fontSize * 10;
  291. }
  292. };
  293. cell.opacityBlock = ^(CGFloat opacity) {
  294. blockSelf.textSampleView.textOpacity = opacity;
  295. if (blockSelf.pdfView.editingArea) {
  296. [blockSelf.pdfView setCharsFontTransparency:opacity withTextArea:blockSelf.pdfView.editingArea];
  297. } else {
  298. [CPDFTextProperty sharedManager].textOpacity = opacity;
  299. }
  300. };
  301. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  302. tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  303. return cell;
  304. }else{
  305. CPDFImagePropertyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ImageCell"];
  306. if (!cell) {
  307. cell = [[CPDFImagePropertyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CPDFImagePropertyCell"];
  308. }
  309. cell.backgroundColor = [UIColor colorWithRed:250./255 green:252/255. blue:255/255. alpha:1.];
  310. cell.pdfView = self.pdfView;
  311. __block typeof(self) blockSelf = self;
  312. cell.rotateBlock = ^(CPDFImageRotateType rotateType, BOOL isRotated) {
  313. if(rotateType == CPDFImageRotateTypeLeft){
  314. [blockSelf.pdfView rotateEditArea:(CPDFEditImageArea*)blockSelf.pdfView.editingArea rotateAngle:-90];
  315. blockSelf.imageSampleView.imageView.transform = CGAffineTransformRotate(blockSelf.imageSampleView.imageView.transform, -M_PI/2);
  316. [blockSelf.imageSampleView setNeedsLayout];
  317. }else if(rotateType == CPDFImageRotateTypeRight){
  318. [blockSelf.pdfView rotateEditArea:(CPDFEditImageArea*)blockSelf.pdfView.editingArea rotateAngle:90];
  319. blockSelf.imageSampleView.imageView.transform = CGAffineTransformRotate(blockSelf.imageSampleView.imageView.transform, M_PI/2);
  320. [blockSelf.imageSampleView setNeedsLayout];
  321. }
  322. };
  323. cell.transFormBlock = ^(CPDFImageTransFormType transformType, BOOL isTransformed) {
  324. if(transformType == CPDFImageTransFormTypeVertical){
  325. [blockSelf.pdfView verticalMirrorEditArea:(CPDFEditImageArea*)blockSelf.pdfView.editingArea];
  326. blockSelf.imageSampleView.imageView.transform = CGAffineTransformScale(blockSelf.imageSampleView.imageView.transform, 1.0, -1.0);
  327. [blockSelf.imageSampleView setNeedsLayout];
  328. }else if(transformType == CPDFImageTransFormTypeHorizontal){
  329. [blockSelf.pdfView horizontalMirrorEditArea:(CPDFEditImageArea*)blockSelf.pdfView.editingArea];
  330. blockSelf.imageSampleView.imageView.transform = CGAffineTransformScale(blockSelf.imageSampleView.imageView.transform, -1.0, 1.0);
  331. [blockSelf.imageSampleView setNeedsLayout];
  332. }
  333. };
  334. cell.transparencyBlock = ^(CGFloat transparency) {
  335. [blockSelf.pdfView setImageTransparencyEditArea:(CPDFEditImageArea*)blockSelf.pdfView.editingArea transparency:transparency];
  336. blockSelf.imageSampleView.imageView.alpha = transparency;
  337. [blockSelf.imageSampleView setNeedsLayout];
  338. };
  339. cell.replaceImageBlock = ^{
  340. UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
  341. imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  342. imagePicker.delegate = blockSelf;
  343. [self presentViewController:imagePicker animated:YES completion:nil];
  344. };
  345. cell.cropImageBlock = ^{
  346. [self.pdfView beginCropEditArea:(CPDFEditImageArea*)self.pdfView.editingArea];
  347. [self controllerDismiss];
  348. };
  349. cell.exportImageBlock = ^{
  350. BOOL saved = [blockSelf.pdfView extractImageWithEditImageArea:blockSelf.pdfView.editingArea];
  351. if(saved){
  352. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:NSLocalizedString(@"Export Successfully!", nil) preferredStyle:UIAlertControllerStyleAlert];
  353. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK!", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  354. [blockSelf controllerDismiss];
  355. }]];
  356. [blockSelf presentViewController:alertController animated:YES completion:nil];
  357. }else{
  358. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:NSLocalizedString(@"Export Failed!", nil) preferredStyle:UIAlertControllerStyleAlert];
  359. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK!", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  360. [blockSelf controllerDismiss];
  361. }]];
  362. [blockSelf presentViewController:alertController animated:YES completion:nil];
  363. }
  364. };
  365. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  366. tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  367. return cell;
  368. }
  369. }
  370. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  371. if(self.editMode == CPDFEditModeText){
  372. return 400;
  373. }else{
  374. return 380;
  375. }
  376. }
  377. #pragma mark - ColorPickerDelegate
  378. - (void)pickerView:(CPDFColorPickerView *)colorPickerView color:(UIColor *)color {
  379. self.textSampleView.textColor = color;
  380. CGFloat red, green, blue, alpha;
  381. [color getRed:&red green:&green blue:&blue alpha:&alpha];
  382. if (self.pdfView.editingArea) {
  383. [self.pdfView setEditingSelectionFontColor:color withTextArea:self.pdfView.editingArea];
  384. } else {
  385. [CPDFTextProperty sharedManager].fontColor = color;
  386. }
  387. }
  388. #pragma mark - CPDFEditFontNameSelectViewDelegate
  389. - (void)pickerView:(CPDFEditFontNameSelectView *)colorPickerView fontName:(NSString *)fontName{
  390. self.textSampleView.fontName = fontName;
  391. if (self.pdfView.editingArea) {
  392. [self.pdfView setEditingSelectionFontName:fontName withTextArea:self.pdfView.editingArea];
  393. } else {
  394. [CPDFTextProperty sharedManager].fontName = fontName;
  395. }
  396. [self.tableView reloadData];
  397. }
  398. #pragma mark - UIColorPickerViewControllerDelegate
  399. - (void)colorPickerViewControllerDidFinish:(UIColorPickerViewController *)viewController API_AVAILABLE(ios(14.0)) {
  400. self.textSampleView.textColor = viewController.selectedColor;
  401. CGFloat red, green, blue, alpha;
  402. [viewController.selectedColor getRed:&red green:&green blue:&blue alpha:&alpha];
  403. if (self.pdfView.editingArea) {
  404. [self.pdfView setEditingSelectionFontColor:viewController.selectedColor withTextArea:self.pdfView.editingArea];
  405. [self.pdfView setCharsFontTransparency:alpha withTextArea:self.pdfView.editingArea];
  406. } else {
  407. [CPDFTextProperty sharedManager].fontColor = viewController.selectedColor;
  408. [CPDFTextProperty sharedManager].textOpacity = alpha;
  409. }
  410. self.textSampleView.textOpacity = alpha;
  411. [self.tableView reloadData];
  412. }
  413. #pragma mark - setMode
  414. - (void)setEditMode:(CPDFEditMode)editMode{
  415. _editMode = editMode;
  416. [self updatePreferredContentSizeWithTraitCollection:self.traitCollection];
  417. }
  418. #pragma mark - Action
  419. - (void)buttonItemClicked_back:(UIButton *)button {
  420. [self controllerDismiss];
  421. }
  422. #pragma mark - Accessors
  423. #pragma mark - UIImagePickerControllerDelegate
  424. -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
  425. {
  426. if (@available(iOS 11.0, *)) {
  427. NSURL * url = info[UIImagePickerControllerImageURL];
  428. [self.pdfView replaceEditImageArea:(CPDFEditImageArea*)self.pdfView.editingArea imagePath:url.path];
  429. } else {
  430. NSURL * url = info[UIImagePickerControllerMediaURL];
  431. [self.pdfView replaceEditImageArea:(CPDFEditImageArea*)self.pdfView.editingArea imagePath:url.path];
  432. }
  433. [picker dismissViewControllerAnimated:YES completion:nil];
  434. [self controllerDismiss];
  435. }
  436. - (void)controllerDismiss {
  437. [self dismissViewControllerAnimated:YES completion:^{
  438. }];
  439. }
  440. @end