CPDFSearchResultsViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //
  2. // CPDFSearchResultsViewController.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 "CPDFSearchResultsViewController.h"
  13. #import "CPDFSearchViewCell.h"
  14. #import <ComPDFKit/ComPDFKit.h>
  15. #import <CoreText/CoreText.h>
  16. #import "UIViewController+LeftItem.h"
  17. #import "CPDFColorUtils.h"
  18. #define kTextSearch_Content_Length_Max 100
  19. @interface CPDFSearchResultsViewController () <UITableViewDelegate, UITableViewDataSource>
  20. @property (nonatomic, strong) NSArray *resultArray;
  21. @property (nonatomic, strong) NSString *keyword;
  22. @property (nonatomic, strong) CPDFDocument *document;
  23. @property (nonatomic, strong) UITableView *tableView;
  24. @property (nonatomic, strong) UIView * searchResultView;
  25. @property (nonatomic, strong) UILabel * searchResultLabel;
  26. @property (nonatomic, strong) UILabel * pageLabel;
  27. @property (nonatomic, strong) UILabel *titleLabel;
  28. @property (nonatomic, strong) UIButton *backBtn;
  29. @end
  30. @implementation CPDFSearchResultsViewController
  31. #pragma mark - Initializers
  32. - (instancetype)initWithResultArray:(NSArray *)resultArray keyword:(NSString *) keyword document:(CPDFDocument *) document {
  33. if (self = [super init]) {
  34. _resultArray = resultArray;
  35. _keyword = keyword;
  36. _document = document;
  37. }
  38. return self;
  39. }
  40. #pragma mark - Accessors
  41. #pragma mark - UIViewController Methods
  42. - (void)viewDidLoad {
  43. [super viewDidLoad];
  44. // Do any additional setup after loading the view.
  45. // [self changeleftItem];
  46. // self.title = NSLocalizedString(@"Search Results", nil);
  47. self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
  48. self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  49. self.tableView.delegate = self;
  50. self.tableView.dataSource = self;
  51. self.view.backgroundColor = [UIColor whiteColor];
  52. self.tableView.rowHeight = UITableViewAutomaticDimension;
  53. self.tableView.estimatedRowHeight = 60;
  54. self.tableView.tableFooterView = [[UIView alloc] init];
  55. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  56. [self.tableView registerClass:[CPDFSearchViewCell class] forCellReuseIdentifier:@"cell"];
  57. [self.view addSubview:self.tableView];
  58. self.searchResultView = [[UIView alloc] init];
  59. self.searchResultLabel = [[UILabel alloc] init];
  60. self.pageLabel = [[UILabel alloc] init];
  61. self.searchResultView.backgroundColor = [UIColor colorWithRed:242./255 green:242/255. blue:242/255. alpha:1.0];
  62. self.pageLabel.font = [UIFont systemFontOfSize:14];
  63. self.pageLabel.text = NSLocalizedString(@"Page",nil);
  64. self.pageLabel.textAlignment = NSTextAlignmentRight;
  65. self.pageLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1.0];
  66. self.searchResultLabel.font = [UIFont systemFontOfSize:14];
  67. self.searchResultLabel.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1.0];
  68. [self.searchResultView addSubview:self.searchResultLabel];
  69. [self.searchResultView addSubview:self.pageLabel];
  70. [self.view addSubview:self.searchResultView];
  71. [self updatePreferredContentSizeWithTraitCollection:self.traitCollection];
  72. self.titleLabel = [[UILabel alloc] init];
  73. self.titleLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
  74. self.titleLabel.text = NSLocalizedString(@"Search Results", nil);
  75. self.titleLabel.font = [UIFont systemFontOfSize:20];
  76. self.titleLabel.adjustsFontSizeToFitWidth = YES;
  77. [self.view addSubview:self.titleLabel];
  78. self.backBtn = [[UIButton alloc] init];
  79. self.backBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  80. [self.backBtn setImage:[UIImage imageNamed:@"CPDFEditClose" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  81. [self.backBtn addTarget:self action:@selector(buttonItemClicked_back:) forControlEvents:UIControlEventTouchUpInside];
  82. [self.view addSubview:self.backBtn];
  83. self.view.backgroundColor = [CPDFColorUtils CPDFViewControllerBackgroundColor];
  84. }
  85. - (void)buttonItemClicked_back:(UIButton *)button {
  86. [self dismissViewControllerAnimated:YES completion:^{
  87. }];
  88. }
  89. - (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  90. [super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];
  91. [self updatePreferredContentSizeWithTraitCollection:newCollection];
  92. }
  93. - (void)updatePreferredContentSizeWithTraitCollection:(UITraitCollection *)traitCollection
  94. {
  95. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width,traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact ? (self.view.bounds.size.width*0.9) : 800);
  96. }
  97. - (void)viewWillLayoutSubviews {
  98. [super viewWillLayoutSubviews];
  99. if (@available(iOS 11.0, *)) {
  100. self.titleLabel.frame = CGRectMake((self.view.frame.size.width - 120)/2, 5, 120, 50);
  101. self.backBtn.frame = CGRectMake(self.view.frame.size.width - 60, 5, 50, 50);
  102. self.tableView.frame = CGRectMake(self.view.safeAreaInsets.left, self.view.safeAreaInsets.top +28 + 50, self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, self.view.frame.size.height - self.view.safeAreaInsets.bottom- self.view.safeAreaInsets.top - 50);
  103. self.searchResultView.frame = CGRectMake(self.view.safeAreaInsets.left, self.view.safeAreaInsets.top + 50, self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, 28);
  104. self.searchResultLabel.frame = CGRectMake(70, 4, 200, 20);
  105. self.searchResultLabel.text = [NSString stringWithFormat:@"%zd results found",self.resultArray.count];
  106. self.pageLabel.frame = CGRectMake(self.view.frame.size.width - 50, 4, 40, 20);
  107. } else {
  108. self.titleLabel.frame = CGRectMake((self.view.frame.size.width - 120)/2, 5, 120, 50);
  109. self.backBtn.frame = CGRectMake(self.view.frame.size.width - 60, 5, 50, 50);
  110. self.searchResultView.frame = CGRectMake(self.view.safeAreaInsets.left, self.view.safeAreaInsets.top, self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, 28);
  111. self.searchResultLabel.frame = CGRectMake(20, 4, 200, 20);
  112. self.searchResultLabel.text = [NSString stringWithFormat:@"%zd results found",self.resultArray.count];
  113. self.pageLabel.frame = CGRectMake(self.view.frame.size.width - 50, 4, 40, 20);
  114. // self.view.bounds
  115. self.tableView.frame = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y + 50 + 28, self.view.bounds.size.width, self.view.bounds.size.height - 50 - 28);
  116. }
  117. }
  118. #pragma mark - Button Actions
  119. - (void)buttonItemClicked_Back:(id)sender {
  120. if([self.delegate respondsToSelector:@selector(searchResultsViewControllerDismiss:)])
  121. [self.delegate searchResultsViewControllerDismiss:self];
  122. }
  123. #pragma mark - Private Methods
  124. - (NSMutableAttributedString *)getAttributedStringWithSelection:(CPDFSelection *)selection {
  125. CPDFPage * currentPage = selection.page;
  126. NSRange range = selection.range;
  127. NSUInteger startLocation = 0;
  128. NSUInteger maxLocation = 20;
  129. NSInteger keyLocation = 0;
  130. NSUInteger maxEndLocation = 80;
  131. if (range.location > maxLocation) {
  132. startLocation = range.location - maxLocation;
  133. keyLocation = maxLocation;
  134. } else {
  135. startLocation = 0;
  136. keyLocation = range.location;
  137. }
  138. NSUInteger endLocation = 0;
  139. if (range.location + maxEndLocation > currentPage.numberOfCharacters) {
  140. endLocation = currentPage.numberOfCharacters;
  141. } else {
  142. endLocation = range.location + maxEndLocation;
  143. }
  144. NSMutableAttributedString * attributed = nil;
  145. if (endLocation> startLocation) {
  146. NSString * currentString = [currentPage stringForRange:NSMakeRange(startLocation, endLocation - startLocation)];
  147. NSRange tRange = NSMakeRange(keyLocation, self.keyword.length);
  148. if (tRange.location != NSNotFound) {
  149. attributed = [[NSMutableAttributedString alloc] initWithString:currentString];
  150. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
  151. paragraphStyle.firstLineHeadIndent = 10.0;
  152. paragraphStyle.headIndent = 10.0;
  153. paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
  154. NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"HelveticaNeue-Medium" size:13.0],NSFontAttributeName,paragraphStyle,NSParagraphStyleAttributeName,nil];
  155. NSRange range = [[attributed string] rangeOfString:[attributed string]];
  156. [attributed setAttributes:dic range:range];
  157. dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:102./255. green:102/255. blue:102/255. alpha:1.],NSForegroundColorAttributeName,nil];
  158. [attributed addAttributes:dic range:NSMakeRange(0, currentString.length)];
  159. //hightlight string
  160. dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:1.0 green:220.0/255.0 blue:27.0/255.0 alpha:1.0],NSBackgroundColorAttributeName,nil];
  161. if (attributed.length > tRange.length + tRange.location) {
  162. [attributed addAttributes:dic range:tRange];
  163. }
  164. }
  165. }
  166. return attributed;
  167. }
  168. #pragma mark - UITableViewDatasource
  169. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  170. return [self.resultArray count];
  171. }
  172. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  173. NSArray *array = [self.resultArray objectAtIndex:section];
  174. return [array count];
  175. }
  176. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  177. CPDFSearchViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  178. if (!cell) {
  179. cell = [[CPDFSearchViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
  180. }
  181. CPDFSelection *selection = self.resultArray[indexPath.section][indexPath.row];
  182. cell.contentLabel.attributedText = [self getAttributedStringWithSelection:selection];
  183. return cell;
  184. }
  185. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  186. CPDFSelection *selection = self.resultArray[indexPath.section][indexPath.row];
  187. NSAttributedString *attributeText = [self getAttributedStringWithSelection:selection];
  188. CGFloat cellWidth = tableView.frame.size.width;
  189. UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);
  190. CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attributeText);
  191. CGSize suggestedSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, 0), NULL, CGSizeMake(cellWidth - padding.left - padding.right, CGFLOAT_MAX), NULL);
  192. CGFloat cellHeight = suggestedSize.height + padding.top + padding.bottom;
  193. CFRelease(framesetter);
  194. return cellHeight;
  195. }
  196. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  197. return 30;
  198. }
  199. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  200. NSArray *array = [self.resultArray objectAtIndex:section];
  201. CPDFSelection *selection = array.firstObject;
  202. NSInteger pageIndex = [self.document indexForPage:selection.page];
  203. NSString *pageStr = [NSString stringWithFormat:NSLocalizedString(@"Page %ld",nil), (long)(pageIndex+1)];
  204. NSString *countStr = [NSString stringWithFormat:@"%@", @(array.count)];
  205. UITableViewHeaderFooterView *view = [[UITableViewHeaderFooterView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
  206. view.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  207. view.backgroundColor = [UIColor colorWithRed:250./255. green:252/255. blue:255/255. alpha:1];
  208. UILabel *sublabel = [[UILabel alloc] init];
  209. sublabel.font = [UIFont systemFontOfSize:14];
  210. sublabel.text = countStr;
  211. sublabel.textColor = [UIColor colorWithRed:67./255. green:71.0/255.0 blue:77./255.0 alpha:1.0];
  212. [sublabel sizeToFit];
  213. sublabel.frame = CGRectMake(view.bounds.size.width-sublabel.bounds.size.width-10, 0,
  214. sublabel.bounds.size.width, view.bounds.size.height);
  215. sublabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  216. [view.contentView addSubview:sublabel];
  217. return view;
  218. }
  219. #pragma mark - UITableViewDelegate
  220. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  221. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  222. CPDFSelection *selection = self.resultArray[indexPath.section][indexPath.row];
  223. if([self.delegate respondsToSelector:@selector(searchResultsView:forSelection:indexPath:)]) {
  224. [self.delegate searchResultsView:self forSelection:selection indexPath:indexPath];
  225. }
  226. [self dismissViewControllerAnimated:YES completion:^{
  227. }];
  228. }
  229. @end