CPDFSearchResultsViewController.m 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #define kTextSearch_Content_Length_Max 100
  17. @interface CPDFSearchResultsViewController () <UITableViewDelegate, UITableViewDataSource>
  18. @property (nonatomic, strong) NSArray *resultArray;
  19. @property (nonatomic, strong) NSString *keyword;
  20. @property (nonatomic, strong) CPDFDocument *document;
  21. @property (nonatomic, strong) UITableView *tableView;
  22. @end
  23. @implementation CPDFSearchResultsViewController
  24. #pragma mark - Initializers
  25. - (instancetype)initWithResultArray:(NSArray *)resultArray keyword:(NSString *) keyword document:(CPDFDocument *) document {
  26. if (self = [super init]) {
  27. _resultArray = resultArray;
  28. _keyword = keyword;
  29. _document = document;
  30. }
  31. return self;
  32. }
  33. #pragma mark - Accessors
  34. #pragma mark - UIViewController Methods
  35. - (void)viewDidLoad {
  36. [super viewDidLoad];
  37. // Do any additional setup after loading the view.
  38. UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"CPDFSearchImagePrevious" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] style:UIBarButtonItemStylePlain target:self action:@selector(buttonItemClicked_Back:)];
  39. self.navigationItem.leftBarButtonItem = backItem;
  40. self.title = NSLocalizedString(@"Search Results", nil);
  41. self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
  42. self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  43. self.tableView.delegate = self;
  44. self.tableView.dataSource = self;
  45. self.tableView.rowHeight = UITableViewAutomaticDimension;
  46. self.tableView.estimatedRowHeight = 60;
  47. self.tableView.tableFooterView = [[UIView alloc] init];
  48. [self.tableView registerClass:[CPDFSearchViewCell class] forCellReuseIdentifier:@"cell"];
  49. [self.view addSubview:self.tableView];
  50. }
  51. #pragma mark - Button Actions
  52. - (void)buttonItemClicked_Back:(id)sender {
  53. if([self.delegate respondsToSelector:@selector(searchResultsViewControllerDismiss:)])
  54. [self.delegate searchResultsViewControllerDismiss:self];
  55. }
  56. #pragma mark - Private Methods
  57. - (NSMutableAttributedString *)getAttributedStringWithSelection:(CPDFSelection *)selection {
  58. CPDFPage * currentPage = selection.page;
  59. NSRange range = selection.range;
  60. NSUInteger startLocation = 0;
  61. NSUInteger maxLocation = 20;
  62. NSInteger keyLocation = 0;
  63. NSUInteger maxEndLocation = 80;
  64. if (range.location > maxLocation) {
  65. startLocation = range.location - maxLocation;
  66. keyLocation = maxLocation;
  67. } else {
  68. startLocation = 0;
  69. keyLocation = range.location;
  70. }
  71. NSUInteger endLocation = 0;
  72. if (range.location + maxEndLocation > currentPage.numberOfCharacters) {
  73. endLocation = currentPage.numberOfCharacters;
  74. } else {
  75. endLocation = range.location + maxEndLocation;
  76. }
  77. NSMutableAttributedString * attributed = nil;
  78. if (endLocation> startLocation) {
  79. NSString * currentString = [currentPage stringForRange:NSMakeRange(startLocation, endLocation - startLocation)];
  80. NSRange tRange = NSMakeRange(keyLocation, self.keyword.length);
  81. if (tRange.location != NSNotFound) {
  82. attributed = [[NSMutableAttributedString alloc] initWithString:currentString];
  83. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
  84. paragraphStyle.firstLineHeadIndent = 10.0;
  85. paragraphStyle.headIndent = 10.0;
  86. paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
  87. NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"HelveticaNeue-Medium" size:14.0],NSFontAttributeName,paragraphStyle,NSParagraphStyleAttributeName,nil];
  88. NSRange range = [[attributed string] rangeOfString:[attributed string]];
  89. [attributed setAttributes:dic range:range];
  90. //hightlight string
  91. dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:1.0 green:220.0/255.0 blue:27.0/255.0 alpha:1.0],NSBackgroundColorAttributeName,nil];
  92. if (attributed.length > tRange.length + tRange.location) {
  93. [attributed addAttributes:dic range:tRange];
  94. }
  95. }
  96. }
  97. return attributed;
  98. }
  99. #pragma mark - UITableViewDatasource
  100. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  101. return [self.resultArray count];
  102. }
  103. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  104. NSArray *array = [self.resultArray objectAtIndex:section];
  105. return [array count];
  106. }
  107. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  108. CPDFSearchViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  109. if (!cell) {
  110. cell = [[CPDFSearchViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
  111. }
  112. CPDFSelection *selection = self.resultArray[indexPath.section][indexPath.row];
  113. cell.contentLabel.attributedText = [self getAttributedStringWithSelection:selection];
  114. return cell;
  115. }
  116. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  117. CPDFSelection *selection = self.resultArray[indexPath.section][indexPath.row];
  118. NSAttributedString *attributeText = [self getAttributedStringWithSelection:selection];
  119. CGFloat cellWidth = tableView.frame.size.width;
  120. UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);
  121. CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attributeText);
  122. CGSize suggestedSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0, 0), NULL, CGSizeMake(cellWidth - padding.left - padding.right, CGFLOAT_MAX), NULL);
  123. CGFloat cellHeight = suggestedSize.height + padding.top + padding.bottom;
  124. CFRelease(framesetter);
  125. return cellHeight;
  126. }
  127. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  128. return 30;
  129. }
  130. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  131. NSArray *array = [self.resultArray objectAtIndex:section];
  132. CPDFSelection *selection = array.firstObject;
  133. NSInteger pageIndex = [self.document indexForPage:selection.page];
  134. NSString *pageStr = [NSString stringWithFormat:NSLocalizedString(@"Page %ld",nil), (long)(pageIndex+1)];
  135. NSString *countStr = [NSString stringWithFormat:@"%@", @(array.count)];
  136. UITableViewHeaderFooterView *view = [[UITableViewHeaderFooterView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
  137. view.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  138. view.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1.0];
  139. UILabel *label = [[UILabel alloc] init];
  140. label.font = [UIFont boldSystemFontOfSize:16];
  141. label.text = pageStr;
  142. [label sizeToFit];
  143. label.frame = CGRectMake(10, 0, label.bounds.size.width, view.bounds.size.height);
  144. [view.contentView addSubview:label];
  145. UILabel *sublabel = [[UILabel alloc] init];
  146. sublabel.font = [UIFont systemFontOfSize:14];
  147. sublabel.text = countStr;
  148. sublabel.textColor = [UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0];
  149. [sublabel sizeToFit];
  150. sublabel.frame = CGRectMake(view.bounds.size.width-sublabel.bounds.size.width-10, 0,
  151. sublabel.bounds.size.width, view.bounds.size.height);
  152. sublabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  153. [view.contentView addSubview:sublabel];
  154. return view;
  155. }
  156. #pragma mark - UITableViewDelegate
  157. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  158. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  159. CPDFSelection *selection = self.resultArray[indexPath.section][indexPath.row];
  160. if([self.delegate respondsToSelector:@selector(searchResultsView:forSelection:indexPath:)]) {
  161. [self.delegate searchResultsView:self forSelection:selection indexPath:indexPath];
  162. }
  163. }
  164. @end