CPDFFormListViewController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. //
  2. // CPDFFormListViewController.m
  3. // compdfkit-tools
  4. //
  5. // Copyright © 2014-2023 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 "CPDFFormListViewController.h"
  13. #import "CPDFColorSelectView.h"
  14. #import "CPDFThicknessSliderView.h"
  15. #import "CPDFFormTextFieldView.h"
  16. #import "CPDFFormSwitchView.h"
  17. #import "CPDFFontSettingView.h"
  18. #import "CPDFFontAlignView.h"
  19. #import "CPDFFormInputTextView.h"
  20. #import <ComPDFKit/ComPDFKit.h>
  21. #import "CAnnotationManage.h"
  22. #import "CAnnotStyle.h"
  23. #import "CPDFColorPickerView.h"
  24. #import "CPDFColorUtils.h"
  25. #import "CPDFColorPickerView.h"
  26. #import "CPDFFontStyleTableView.h"
  27. @interface CPDFFormListViewController ()<CPDFFormTextFiledViewDelegate,CPDFColorSelectViewDelegate,CPDFThicknessSliderViewDelegate,CPDFFormSwitchViewDelegate,UIColorPickerViewControllerDelegate,CPDFColorPickerViewDelegate,CPDFFontSettingViewDelegate,CPDFFontStyleTableViewDelegate>
  28. @property (nonatomic, strong) CPDFColorSelectView * borderColorView;
  29. @property (nonatomic, strong) CPDFColorSelectView * backGroundColorView;
  30. @property (nonatomic, strong) CPDFColorSelectView * textColorView;
  31. @property (nonatomic, strong) CPDFThicknessSliderView * sizeThickNessView;
  32. @property (nonatomic, strong) CPDFFormTextFieldView * textFiledView;
  33. @property (nonatomic, strong) CPDFFormSwitchView * hideFormView;
  34. @property (nonatomic, strong) CPDFFontSettingView * fontSettingView;
  35. @property (nonatomic, strong) CAnnotationManage * annotManage;
  36. @property (nonatomic, strong) CPDFChoiceWidgetAnnotation * listChoiceWidget;
  37. @property (nonatomic, strong) CPDFColorPickerView * colorPicker;
  38. @property (nonatomic, strong) CPDFColorSelectView * currentSelectColorView;
  39. @property (nonatomic, strong) CPDFFontStyleTableView *fontStyleTableView;
  40. @property (nonatomic, copy) NSString * baseName;
  41. @property (nonatomic, assign) BOOL isBold;
  42. @property (nonatomic, assign) BOOL isItalic;
  43. @property (nonatomic, assign) CGFloat fontSize;
  44. @end
  45. @implementation CPDFFormListViewController
  46. - (instancetype)initWithManage:(CAnnotationManage *)annotManage {
  47. if (self = [super init]) {
  48. self.annotManage = annotManage;
  49. }
  50. return self;
  51. }
  52. - (void)viewDidLoad {
  53. [super viewDidLoad];
  54. // Do any additional setup after loading the view.
  55. self.borderColorView = [[CPDFColorSelectView alloc] init];
  56. self.borderColorView.delegate = self;
  57. self.borderColorView.colorLabel.text = NSLocalizedString(@"Border Color", nil);
  58. self.borderColorView.colorLabel.font = [UIFont boldSystemFontOfSize:13.0];
  59. self.borderColorView.colorLabel.textColor = [UIColor colorWithRed:153/255. green:153/255. blue:153/255. alpha:1];
  60. self.borderColorView.colorPickerView.showsHorizontalScrollIndicator = NO;
  61. self.backGroundColorView = [[CPDFColorSelectView alloc] init];
  62. self.backGroundColorView.colorLabel.text = NSLocalizedString(@"Background Color", nil);
  63. self.backGroundColorView.colorLabel.font = [UIFont boldSystemFontOfSize:13.0];
  64. self.backGroundColorView.colorLabel.textColor = [UIColor colorWithRed:153/255. green:153/255. blue:153/255. alpha:1];
  65. self.backGroundColorView.colorPickerView.showsHorizontalScrollIndicator = NO;
  66. self.backGroundColorView.delegate = self;
  67. self.textColorView = [[CPDFColorSelectView alloc] init];
  68. self.textColorView.colorLabel.text = NSLocalizedString(@"Text Color", nil);
  69. self.textColorView.colorLabel.font = [UIFont boldSystemFontOfSize:13.0];
  70. self.textColorView.colorLabel.textColor = [UIColor colorWithRed:153/255. green:153/255. blue:153/255. alpha:1];
  71. self.textColorView.colorPickerView.showsHorizontalScrollIndicator = NO;
  72. self.textColorView.delegate = self;
  73. [self.scrcollView addSubview:self.borderColorView];
  74. [self.scrcollView addSubview:self.backGroundColorView];
  75. [self.scrcollView addSubview:self.textColorView];
  76. self.sizeThickNessView = [[CPDFThicknessSliderView alloc] init];
  77. self.sizeThickNessView.titleLabel.text = NSLocalizedString(@"Size", nil);
  78. self.sizeThickNessView.titleLabel.font = [UIFont boldSystemFontOfSize:13.0];
  79. self.sizeThickNessView.titleLabel.textColor = [UIColor colorWithRed:153/255. green:153/255. blue:153/255. alpha:1];
  80. [self.scrcollView addSubview:self.sizeThickNessView];
  81. self.sizeThickNessView.delegate = self;
  82. self.sizeThickNessView.thick = 10.;
  83. self.textFiledView = [[CPDFFormTextFieldView alloc] init];
  84. self.textFiledView.delegate = self;
  85. [self.scrcollView addSubview:self.textFiledView];
  86. self.hideFormView = [[CPDFFormSwitchView alloc] init];
  87. self.hideFormView.delegate = self;
  88. self.hideFormView.titleLabel.font = [UIFont boldSystemFontOfSize:13.0];
  89. self.hideFormView.titleLabel.textColor = [UIColor colorWithRed:153/255. green:153/255. blue:153/255. alpha:1];
  90. self.hideFormView.titleLabel.text = NSLocalizedString(@"Hide Form", nil);
  91. [self.scrcollView addSubview:self.hideFormView];
  92. self.fontSettingView = [[CPDFFontSettingView alloc] init];
  93. self.fontSettingView.fontNameLabel.font = [UIFont boldSystemFontOfSize:13.0];
  94. self.fontSettingView.fontNameLabel.textColor = [UIColor colorWithRed:153/255. green:153/255. blue:153/255. alpha:1];
  95. self.fontSettingView.fontNameLabel.text = NSLocalizedString(@"Font", nil);
  96. self.fontSettingView.delegate = self;
  97. [self.scrcollView addSubview:self.fontSettingView];
  98. self.fontSize = 14;
  99. self.baseName = @"Helvetica";
  100. self.fontSettingView.fontNameSelectLabel.text = self.baseName;
  101. self.listChoiceWidget = (CPDFChoiceWidgetAnnotation*)self.annotManage.annotStyle.annotations.firstObject;
  102. }
  103. #pragma mark - Private
  104. - (void)analyzeFont:(NSString *)fontName {
  105. if(fontName == nil) {
  106. self.fontSettingView.isBold = NO;
  107. self.fontSettingView.isItalic = NO;
  108. return;
  109. }
  110. if ([fontName rangeOfString:@"Bold"].location != NSNotFound) {
  111. //isBold
  112. self.fontSettingView.isBold = YES;
  113. self.isBold = YES;
  114. } else {
  115. //is not bold
  116. self.fontSettingView.isBold = NO;
  117. self.isBold = NO;
  118. }
  119. if ([fontName rangeOfString:@"Italic"].location != NSNotFound || [fontName rangeOfString:@"Oblique"].location != NSNotFound) {
  120. //is Italic
  121. self.fontSettingView.isItalic = YES;
  122. self.isItalic = YES;
  123. } else {
  124. //is Not Italic
  125. self.fontSettingView.isItalic = NO;
  126. self.isItalic = NO;
  127. }
  128. if ([fontName rangeOfString:@"Helvetica"].location != NSNotFound)
  129. {
  130. self.baseName = @"Helvetica";
  131. self.fontSettingView.fontNameSelectLabel.text = self.baseName;
  132. return ;
  133. }
  134. if ([fontName rangeOfString:@"Courier"].location != NSNotFound)
  135. {
  136. self.baseName = @"Courier";
  137. self.fontSettingView.fontNameSelectLabel.text = self.baseName;
  138. return ;
  139. }
  140. if ([fontName rangeOfString:@"Times"].location != NSNotFound)
  141. {
  142. self.baseName = @"Times-Roman";
  143. self.fontSettingView.fontNameSelectLabel.text = self.baseName;
  144. }
  145. }
  146. - (NSString *)constructionFontname:(NSString *)baseName isBold:(BOOL)isBold isItalic:(BOOL)isItalic {
  147. NSString *result;
  148. if ([baseName rangeOfString:@"Times"].location != NSNotFound) {
  149. if (isBold || isItalic) {
  150. if (isBold && isItalic) return @"Times-BoldItalic";
  151. if (isBold) return @"Times-Bold";
  152. if (isItalic) return @"Times-Italic";
  153. }
  154. else
  155. return @"Times-Roman";
  156. }
  157. if (isBold || isItalic) {
  158. result = [NSString stringWithFormat:@"%@-",baseName];
  159. if (isBold) result = [NSString stringWithFormat:@"%@Bold",result];
  160. if (isItalic) result = [NSString stringWithFormat:@"%@Oblique",result];
  161. }
  162. else return baseName;
  163. return result;
  164. }
  165. #pragma mark - Setter
  166. - (void)setListChoiceWidget:(CPDFChoiceWidgetAnnotation *)listChoiceWidget {
  167. _listChoiceWidget = listChoiceWidget;
  168. //field Name
  169. self.textFiledView.contentField.text = self.listChoiceWidget.fieldName;
  170. //border color
  171. self.borderColorView.selectedColor = self.listChoiceWidget.borderColor;
  172. //back groundColor
  173. self.backGroundColorView.selectedColor = self.listChoiceWidget.backgroundColor;
  174. //Text color
  175. self.textColorView.selectedColor = self.listChoiceWidget.fontColor;
  176. //Text content
  177. [self analyzeFont:self.listChoiceWidget.font.fontName];
  178. if(!self.listChoiceWidget.font) {
  179. self.sizeThickNessView.defaultValue = 0.14;
  180. }else{
  181. if(self.listChoiceWidget.font.pointSize == 0){
  182. self.sizeThickNessView.defaultValue = 0.14;
  183. self.fontSize = 14;
  184. }else{
  185. self.sizeThickNessView.defaultValue = self.listChoiceWidget.font.pointSize/100. ;
  186. self.fontSize = self.listChoiceWidget.font.pointSize;
  187. }
  188. }
  189. //hide form
  190. self.hideFormView.switcher.on = self.listChoiceWidget.isHidden;
  191. }
  192. #pragma mark - Protect Mehtods
  193. - (void)buttonItemClicked_back:(id)sender {
  194. [self dismissViewControllerAnimated:YES completion:nil];
  195. }
  196. - (void)commomInitTitle {
  197. self.titleLabel.text = NSLocalizedString(@"List Box", nil);
  198. }
  199. - (void)updatePreferredContentSizeWithTraitCollection:(UITraitCollection *)traitCollection {
  200. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact ? 350 : 600);
  201. }
  202. - (void)viewWillLayoutSubviews {
  203. [super viewWillLayoutSubviews];
  204. self.scrcollView.frame = CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height-60);
  205. self.scrcollView.contentSize = CGSizeMake(self.view.frame.size.width, 550);
  206. self.scrcollView.showsVerticalScrollIndicator = NO;
  207. if (@available(iOS 11.0, *)) {
  208. self.textFiledView.frame = CGRectMake(self.view.safeAreaInsets.left, 8, self.view.frame.size.width - self.view.safeAreaInsets.left, 65);
  209. self.borderColorView.frame = CGRectMake(self.view.safeAreaInsets.left, CGRectGetMaxY(self.textFiledView.frame)+8,self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right,74);
  210. self.backGroundColorView.frame = CGRectMake(self.view.safeAreaInsets.left, CGRectGetMaxY(self.borderColorView.frame)+8,self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right,74);
  211. self.textColorView.frame = CGRectMake(self.view.safeAreaInsets.left, CGRectGetMaxY(self.backGroundColorView.frame)+8,self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, 74);
  212. self.fontSettingView.frame = CGRectMake(self.view.safeAreaInsets.left, CGRectGetMaxY(self.textColorView.frame)+8, self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, 30);
  213. self.sizeThickNessView.frame = CGRectMake(self.view.safeAreaInsets.left, CGRectGetMaxY(self.fontSettingView.frame),self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, 82);
  214. self.hideFormView.frame = CGRectMake(self.view.safeAreaInsets.left, CGRectGetMaxY(self.sizeThickNessView.frame) + 8,self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, 44);
  215. } else {
  216. self.textFiledView.frame = CGRectMake(0, 8, self.view.bounds.size.width, 60);
  217. self.borderColorView.frame = CGRectMake(0,CGRectGetMaxY(self.textFiledView.frame)+8, self.view.frame.size.width, 74);
  218. self.backGroundColorView.frame = CGRectMake(0,CGRectGetMaxY(self.borderColorView.frame)+8, self.view.frame.size.width, 74);
  219. self.textColorView.frame = CGRectMake(0, CGRectGetMaxY(self.backGroundColorView.frame)+8, self.view.frame.size.width, 74);
  220. self.fontSettingView.frame = CGRectMake(0, CGRectGetMaxY(self.textColorView.frame)+8, self.view.frame.size.width, 30);
  221. self.sizeThickNessView.frame = CGRectMake(0, CGRectGetMaxY(self.fontSettingView.frame), self.view.frame.size.width, 82);
  222. self.hideFormView.frame = CGRectMake(0, CGRectGetMaxY(self.sizeThickNessView.frame) + 8, self.view.frame.size.width, 44);
  223. }
  224. }
  225. #pragma mark - CPDFFormTextFiledViewDelegate
  226. - (void)SetCPDFFormTextFiledView:(CPDFFormTextFieldView *)view text:(NSString *)completedText {
  227. self.listChoiceWidget.fieldName = completedText;
  228. [self refreshUI];
  229. }
  230. #pragma mark - CPDFColorSelectViewDelegate
  231. - (void)selectColorView:(CPDFColorSelectView *)select color:(UIColor *)color {
  232. self.currentSelectColorView = select;
  233. if(select == self.borderColorView) {
  234. self.listChoiceWidget.borderColor = color;
  235. }else if(select == self.backGroundColorView) {
  236. self.listChoiceWidget.backgroundColor = color;
  237. }else if(select == self.textColorView) {
  238. self.listChoiceWidget.fontColor = color;
  239. }
  240. [self refreshUI];
  241. }
  242. - (void)selectColorView:(CPDFColorSelectView *)select {
  243. self.currentSelectColorView = select;
  244. if (@available(iOS 14.0, *)) {
  245. UIColorPickerViewController *picker = [[UIColorPickerViewController alloc] init];
  246. picker.delegate = self;
  247. [self presentViewController:picker animated:YES completion:nil];
  248. } else {
  249. self.colorPicker = [[CPDFColorPickerView alloc] initWithFrame:self.view.frame];
  250. self.colorPicker.delegate = self;
  251. self.colorPicker.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  252. self.colorPicker.backgroundColor = [CPDFColorUtils CAnnotationPropertyViewControllerBackgoundColor];
  253. [self.view addSubview:self.colorPicker];
  254. }
  255. [self refreshUI];
  256. }
  257. #pragma mark - CPDFColorPickerViewDelegate
  258. - (void)pickerView:(CPDFColorPickerView *)colorPickerView color:(UIColor *)color {
  259. if(self.currentSelectColorView == self.borderColorView) {
  260. self.listChoiceWidget.borderColor = color;
  261. }else if(self.currentSelectColorView == self.backGroundColorView) {
  262. self.listChoiceWidget.backgroundColor = color;
  263. }else if(self.currentSelectColorView == self.textColorView) {
  264. self.listChoiceWidget.fontColor = color;
  265. }
  266. [self refreshUI];
  267. }
  268. #pragma mark - UIColorPickerViewControllerDelegate
  269. - (void)colorPickerViewControllerDidFinish:(UIColorPickerViewController *)viewController API_AVAILABLE(ios(14.0)) {
  270. if(self.currentSelectColorView == self.borderColorView) {
  271. self.listChoiceWidget.borderColor = viewController.selectedColor;
  272. }else if(self.currentSelectColorView == self.backGroundColorView) {
  273. self.listChoiceWidget.backgroundColor = viewController.selectedColor;
  274. }else if(self.currentSelectColorView == self.textColorView) {
  275. self.listChoiceWidget.fontColor = viewController.selectedColor;
  276. }
  277. [self refreshUI];
  278. }
  279. #pragma mark - CPDFFormSwitchViewDelegate
  280. - (void)SwitchActionInView:(CPDFFormSwitchView *)view switcher:(UISwitch *)switcher {
  281. if(switcher.on) {
  282. if([self.annotManage.pdfListView.activeAnnotations containsObject:self.annotManage.annotStyle.annotations.firstObject]) {
  283. [self.annotManage.pdfListView updateActiveAnnotations:@[]];
  284. }
  285. }
  286. [self.listChoiceWidget setHidden:switcher.on];
  287. [self refreshUI];
  288. }
  289. #pragma mark - CPDFThicknessSliderViewDelegate
  290. - (void)thicknessSliderView:(CPDFThicknessSliderView *)thicknessSliderView thickness:(CGFloat)thickness {
  291. self.fontSize = thickness * 10;
  292. [self refreshUI];
  293. }
  294. #pragma mark - CPDFFontSettingViewDelegate
  295. - (void)CPDFFontSettingView:(CPDFFontSettingView *)view isBold:(BOOL)isBold {
  296. self.isBold = isBold;
  297. [self refreshUI];
  298. }
  299. - (void)CPDFFontSettingView:(CPDFFontSettingView *)view isItalic:(BOOL)isItalic {
  300. self.isItalic = isItalic;
  301. [self refreshUI];
  302. }
  303. - (void)CPDFFontSettingView:(CPDFFontSettingView *)view text:(NSString *)text {
  304. }
  305. #pragma mark - CPDFFontSettingViewDelegate
  306. - (void)CPDFFontSettingViewFontSelect:(CPDFFontSettingView *)view {
  307. self.fontStyleTableView = [[CPDFFontStyleTableView alloc] initWithFrame:self.view.bounds];
  308. self.fontStyleTableView.delegate = self;
  309. self.fontStyleTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  310. self.fontStyleTableView.backgroundColor = [CPDFColorUtils CAnnotationPropertyViewControllerBackgoundColor];
  311. [self.view addSubview:self.fontStyleTableView];
  312. [self refreshUI];
  313. }
  314. #pragma mark - CPDFFontStyleTableViewDelegate
  315. - (void)fontStyleTableView:(CPDFFontStyleTableView *)fontStyleTableView fontName:(NSString *)fontName {
  316. self.baseName = fontName;
  317. self.fontSettingView.fontNameSelectLabel.text = fontName;
  318. [self refreshUI];
  319. }
  320. - (void)refreshUI {
  321. NSString * fontName = [self constructionFontname:self.baseName isBold:self.isBold isItalic:self.isItalic];
  322. self.listChoiceWidget.font = [UIFont fontWithName:fontName size:self.fontSize];
  323. [self.listChoiceWidget updateAppearanceStream];
  324. CPDFPage *page = self.listChoiceWidget.page;
  325. [self.annotManage.pdfListView setNeedsDisplayForPage:page];
  326. }
  327. @end