PDFKeyboardToolbar.m 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. //
  2. // PDFKeyboardToolbar.m
  3. // PDFReader
  4. //
  5. // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // The PDF Reader Sample applications are licensed with a modified BSD license.
  8. // Please see License for details. This notice may not be removed from this file.
  9. //
  10. #import "PDFKeyboardToolbar.h"
  11. #import "PDFKTColorPickView.h"
  12. #import "PDFKTSlideView.h"
  13. #import "PDFKTFontNamePopBgView.h"
  14. @interface PDFKeyboardToolbar () <UIPopoverPresentationControllerDelegate>
  15. @property (nonatomic,retain) UIView *toolbarView;
  16. @property (nonatomic,retain) CALayer *topBorder;
  17. @property (nonatomic,retain) NSArray *buttonsToAdd;
  18. @property (nonatomic,retain) PDFKTColorPickView *colorPickView;
  19. @property (nonatomic,retain) PDFKTFontNameView *fontNameView;
  20. @property (nonatomic,retain) PDFSettingFontView *setFontSizeView;
  21. @property (nonatomic,retain) PDFSettingFontView *opacticyView;
  22. @property (nonatomic,assign) CGFloat centerY;
  23. @property (nonatomic,retain) UIButton *fontButton;
  24. @property (nonatomic,retain) UIButton *sizeButton;
  25. @property (nonatomic,retain) UIButton *opacityButton;
  26. @property (nonatomic,retain) UIButton *boldButton;
  27. @property (nonatomic,retain) UIButton *italicButton;
  28. @property (nonatomic, retain) UILabel *fontLabel;
  29. @property (nonatomic, retain) UILabel *sizeLabel;
  30. @property (nonatomic, retain) UILabel *opacityLabel;
  31. @property (nonatomic, retain) UIImageView *colorPick;
  32. @property (nonatomic,assign) BOOL isBold;
  33. @property (nonatomic,assign) BOOL isItalic;
  34. @property (nonatomic, retain) UIScrollView *scrollView;
  35. @property (nonatomic, retain) UIButton *doneButton;
  36. @end
  37. @implementation PDFKeyboardToolbar
  38. @synthesize fontName = _fontName;
  39. @synthesize fontSize = _fontSize;
  40. @synthesize delegate = _delegate;
  41. @synthesize opacity = _opacity;
  42. - (id)initWithFontName:(NSString *)fontName fontSize:(CGFloat)fontSize{
  43. self = [super initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 40)];
  44. if (self) {
  45. _buttonsToAdd = nil;
  46. _fontName = [fontName copy];
  47. _fontSize = fontSize;
  48. self.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  49. self.backgroundColor = [UIColor colorWithRed:224.0/255.0 green:224.0/255.0 blue:224.0/255.0 alpha:1.0];
  50. _centerY = self.center.y;
  51. [self initButtonsToAdd];
  52. _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
  53. self.scrollView.clipsToBounds = NO;
  54. [self addSubview:self.scrollView];
  55. for (UIView *eachButton in _buttonsToAdd) {
  56. [self.scrollView addSubview:eachButton];
  57. }
  58. }
  59. return self;
  60. }
  61. - (id)initWithViews:(NSArray*)buttons {
  62. self = [super initWithFrame:CGRectMake(0, 0, self.window.rootViewController.view.bounds.size.width, 40)];
  63. if (self) {
  64. self.buttonsToAdd = buttons;
  65. self.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  66. [self addSubview:[self inputAccessoryView]];
  67. }
  68. return self;
  69. }
  70. - (void)dealloc{
  71. [_doneButton release];
  72. _delegate = nil;
  73. [_fontName release];
  74. [_buttonsToAdd release];
  75. [_toolbarView release];
  76. _scrollView.delegate = nil;
  77. [_scrollView release];
  78. [_fontNameView release];
  79. [_colorPickView release];
  80. [_colorPick release];
  81. [_sizeButton release];
  82. [_setFontSizeView release];
  83. [_opacityButton release];
  84. [_opacticyView release];
  85. [super dealloc];
  86. }
  87. - (void)bindToTextView:(UITextView *)textView
  88. {
  89. [textView setInputAccessoryView:self];
  90. }
  91. - (void)bindToTextField:(UITextField *)textField
  92. {
  93. [textField setInputAccessoryView:self];
  94. }
  95. - (void)refreshFontName:(NSString *)fontName fontSize:(CGFloat)fontSize opacity:(CGFloat)opacity
  96. {
  97. self.fontName = fontName;
  98. _fontSize = fontSize;
  99. _opacity = opacity * 100.0;
  100. [self analyzeFont:_fontName];
  101. if (_fontButton)
  102. [_fontButton setTitle:_fontName forState:UIControlStateNormal];
  103. if (_opacityButton)
  104. [_opacityButton setTitle:[NSString stringWithFormat:@"%.0f",_opacity] forState:UIControlStateNormal];
  105. if (_sizeButton)
  106. [_sizeButton setTitle:[NSString stringWithFormat:@"%.0f",_fontSize] forState:UIControlStateNormal];
  107. _boldButton.selected = _isBold;
  108. _italicButton.selected = _isItalic;
  109. }
  110. - (void)layoutSubviews {
  111. CGRect frame = _toolbarView.bounds;
  112. frame.size.height = 0.5f;
  113. _topBorder.frame = frame;
  114. [self.sizeLabel sizeToFit];
  115. [self.opacityLabel sizeToFit];
  116. CGFloat space = 5;
  117. if (self.bounds.size.width > 768) {
  118. space = 20;
  119. }
  120. CGFloat posX = space;
  121. _boldButton.center = CGPointMake(posX+_boldButton.frame.size.width/2.0, _centerY);
  122. posX += _boldButton.frame.size.width+space;
  123. _italicButton.center = CGPointMake(posX+_italicButton.frame.size.width/2.0, _centerY);
  124. posX += _italicButton.frame.size.width+space;
  125. _fontButton.center = CGPointMake(posX+_fontButton.frame.size.width/2.0, _centerY);
  126. posX += _fontButton.frame.size.width+space;
  127. self.sizeLabel.center = CGPointMake(posX+self.sizeLabel.frame.size.width/2.0, _centerY);
  128. posX += self.sizeLabel.frame.size.width+5;
  129. self.sizeButton.center = CGPointMake(posX+self.sizeButton.frame.size.width/2.0, _centerY);
  130. posX += self.sizeButton.frame.size.width+space;
  131. self.opacityLabel.center = CGPointMake(posX+self.opacityLabel.frame.size.width/2.0, _centerY);
  132. posX += self.opacityLabel.frame.size.width+5;
  133. self.opacityButton.center = CGPointMake(posX+self.opacityButton.frame.size.width/2.0, _centerY);
  134. posX += self.opacityButton.frame.size.width;
  135. CGFloat width = self.colorPick.frame.size.width+self.doneButton.frame.size.width+3*space+16;
  136. if (posX+width < self.bounds.size.width) {
  137. posX += (self.bounds.size.width-width-posX+space);
  138. } else {
  139. posX += space;
  140. }
  141. self.colorPick.center = CGPointMake(posX+self.colorPick.frame.size.width/2.0, _centerY);
  142. _colorPickView.frame = self.colorPick.frame;
  143. posX += self.colorPick.frame.size.width+space+8;
  144. self.doneButton.center = CGPointMake(posX+self.doneButton.frame.size.width/2.0, _centerY);
  145. posX += self.doneButton.frame.size.width+space;
  146. self.scrollView.frame = self.bounds;
  147. self.scrollView.contentSize = CGSizeMake(posX, self.frame.size.height);
  148. }
  149. - (void)initButtonsToAdd{
  150. CGFloat boundsWidth = self.bounds.size.width;
  151. _boldButton = [UIButton buttonWithType:UIButtonTypeCustom];
  152. UIImage *boldImage = [UIImage imageNamed:@"TImageNameBoldLetter"];
  153. [_boldButton setImage:boldImage forState:UIControlStateNormal];
  154. _boldButton.bounds = CGRectMake(0, 0, boldImage.size.width, boldImage.size.height);
  155. _boldButton.center = CGPointMake(boundsWidth * 0.03 *0.85, _centerY);
  156. // _boldButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
  157. [_boldButton addTarget:self action:@selector(boldSelected:) forControlEvents:UIControlEventTouchUpInside];
  158. [_boldButton setImage:[UIImage imageNamed:@"TImageNameSelectBoldLetter"] forState:UIControlStateSelected];
  159. _italicButton = [UIButton buttonWithType:UIButtonTypeCustom];
  160. UIImage *italicImage = [UIImage imageNamed:@"TImageNameLeanIcon"];
  161. [_italicButton setImage:italicImage forState:UIControlStateNormal];
  162. _italicButton.bounds = CGRectMake(0, 0, italicImage.size.width, italicImage.size.height);
  163. _italicButton.center = CGPointMake(boundsWidth * 0.1*0.85, _centerY);
  164. // _italicButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin;
  165. [_italicButton addTarget:self action:@selector(italicSelected:) forControlEvents:UIControlEventTouchUpInside];
  166. [_italicButton setImage:[UIImage imageNamed:@"TImageNameSelectLeanIcon"] forState:UIControlStateSelected];
  167. UILabel *fontLabel = [[UILabel alloc] initWithFrame:CGRectMake(110, 5, 50, 18)];
  168. self.fontLabel = fontLabel;
  169. fontLabel.text = NSLocalizedString(@"Fonts", nil);
  170. fontLabel.font = [UIFont systemFontOfSize:18];
  171. fontLabel.center = CGPointMake(boundsWidth * 0.18*0.85, _centerY);
  172. fontLabel.backgroundColor = [UIColor clearColor];
  173. // fontLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin;
  174. _fontButton = [UIButton buttonWithType:UIButtonTypeCustom];
  175. _fontButton.backgroundColor = [UIColor colorWithRed:142/255.0 green:142.0/255.0 blue:142/255.0 alpha:1.0];
  176. _fontButton.layer.cornerRadius = 5.0;
  177. [_fontButton setTitle:_fontName forState:UIControlStateNormal];
  178. [_fontButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  179. _fontButton.bounds = CGRectMake(0, 0, 129,34.0);
  180. _fontButton.center = CGPointMake(boundsWidth * 0.31*0.85, _centerY);
  181. // _fontButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
  182. [_fontButton addTarget:self action:@selector(fontBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
  183. UILabel *sizeLabel = [[UILabel alloc] initWithFrame:CGRectMake(300, 5, 50, 20)];
  184. self.sizeLabel = sizeLabel;
  185. sizeLabel.text = NSLocalizedString(@"Size", nil);
  186. sizeLabel.font = [UIFont systemFontOfSize:18];
  187. sizeLabel.backgroundColor = [UIColor clearColor];
  188. sizeLabel.center = CGPointMake(boundsWidth *0.45*0.85, _centerY);
  189. // sizeLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin ;
  190. self.sizeButton = [UIButton buttonWithType:UIButtonTypeCustom];
  191. _sizeButton.backgroundColor = [UIColor colorWithRed:142/255.0 green:142.0/255.0 blue:142/255.0 alpha:1.0];
  192. _sizeButton.layer.cornerRadius = 5.0;
  193. [_sizeButton setTitle:[NSString stringWithFormat:@"%.0f",_fontSize] forState:UIControlStateNormal];
  194. [_sizeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  195. _sizeButton.bounds = CGRectMake(0, 0, 51,34.0);
  196. _sizeButton.center = CGPointMake(boundsWidth * 0.52*0.85, _centerY);
  197. [_sizeButton addTarget:self action:@selector(fontSizeBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
  198. UILabel *opacityLabel = [[UILabel alloc] initWithFrame:CGRectMake(300, 5, 75, 40)];
  199. self.opacityLabel = opacityLabel;
  200. opacityLabel.text = NSLocalizedString(@"Opacity", nil);
  201. opacityLabel.font = [UIFont systemFontOfSize:18];
  202. opacityLabel.backgroundColor = [UIColor clearColor];
  203. // [opacityLabel sizeToFit];
  204. opacityLabel.center = CGPointMake(boundsWidth *0.62*0.85, _centerY);
  205. // opacityLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin ;
  206. self.opacityButton = [UIButton buttonWithType:UIButtonTypeCustom];
  207. _opacityButton.backgroundColor = [UIColor colorWithRed:142/255.0 green:142.0/255.0 blue:142/255.0 alpha:1.0];
  208. _opacityButton.layer.cornerRadius = 5.0;
  209. [_opacityButton setTitle:[NSString stringWithFormat:@"%.0f",_opacity] forState:UIControlStateNormal];
  210. [_opacityButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  211. _opacityButton.bounds = CGRectMake(0, 0, 51,34.0);
  212. _opacityButton.center = CGPointMake(boundsWidth * 0.725*0.85, _centerY);
  213. [_opacityButton addTarget:self action:@selector(opacityBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
  214. UIImage *colorPickImg = [UIImage imageNamed:@"text_color_bar.png"];
  215. UIImageView *colorPick = [[UIImageView alloc] initWithImage:colorPickImg];
  216. self.colorPick = colorPick;
  217. colorPick.frame = CGRectMake(0, 0, colorPickImg.size.width, colorPickImg.size.height);
  218. colorPick.center = CGPointMake(boundsWidth * 0.82, _centerY);
  219. // colorPick.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin;
  220. _colorPickView = [[PDFKTColorPickView alloc] initWithFrame:colorPick.frame];
  221. _colorPickView.colorPickDelegate = self;
  222. // _colorPickView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin;
  223. self.doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
  224. [self.doneButton setTitle:NSLocalizedString(@"Done", nil) forState:UIButtonTypeCustom];
  225. [self.doneButton setTitleColor:[UIColor colorWithRed:0 green:122.0/255.0 blue:1.0 alpha:1.0] forState:UIControlStateNormal];
  226. [self.doneButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
  227. [self.doneButton sizeToFit];
  228. [self.doneButton addTarget:self action:@selector(doneSelected:) forControlEvents:UIControlEventTouchUpInside];
  229. _buttonsToAdd = [[NSArray alloc]initWithObjects:_boldButton,_italicButton,_fontButton,sizeLabel,_sizeButton,opacityLabel,_opacityButton,colorPick,_colorPickView,self.doneButton, nil];
  230. [fontLabel release];
  231. [sizeLabel release];
  232. [opacityLabel release];
  233. [colorPick release];
  234. }
  235. - (UIView*)inputAccessoryView {
  236. _toolbarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 40)];
  237. _toolbarView.backgroundColor = [UIColor colorWithWhite:0.973 alpha:1.0];
  238. _toolbarView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  239. _topBorder = [CALayer layer];
  240. _topBorder.frame = CGRectMake(0.0f, 0.0f, self.bounds.size.width, 0.5f);
  241. _topBorder.backgroundColor = [UIColor colorWithWhite:0.678 alpha:1.0].CGColor;
  242. [_toolbarView.layer addSublayer:_topBorder];
  243. [_toolbarView addSubview:[self fakeToolbar]];
  244. return [_toolbarView autorelease];
  245. }
  246. - (UIScrollView*)fakeToolbar {
  247. _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 40)];
  248. _scrollView.backgroundColor = [UIColor clearColor];
  249. _scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  250. _scrollView.showsHorizontalScrollIndicator = NO;
  251. NSUInteger originX = 0;
  252. if (!_buttonsToAdd)
  253. [self initButtonsToAdd];//采用init方式初始化
  254. for (UIView *eachButton in _buttonsToAdd) {
  255. [_scrollView addSubview:eachButton];
  256. originX = originX + eachButton.bounds.size.width;
  257. }
  258. CGSize contentSize = _scrollView.contentSize;
  259. contentSize.width = originX;
  260. _scrollView.contentSize = contentSize;
  261. return [_scrollView autorelease];
  262. }
  263. - (void)fontBtnClicked:(id)sender
  264. {
  265. UIButton *btn = (UIButton *)sender;
  266. btn.selected = !btn.selected;
  267. if (!_fontNameView) {
  268. _fontNameView = [[PDFKTFontNameView alloc] initWithFrame:CGRectMake(0, 0, 160, 145)];
  269. _fontNameView.fontNamedelegate = self;
  270. _fontNameView.fontName = self.fontName;
  271. _fontNameView.center = CGPointMake(btn.center.x, _fontNameView.center.y);
  272. _fontNameView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
  273. }
  274. UIViewController *vc = [[UIViewController alloc] init];
  275. vc.view.backgroundColor = [UIColor clearColor];
  276. [vc.view addSubview:_fontNameView];
  277. vc.preferredContentSize = CGSizeMake(160, 145);
  278. vc.modalPresentationStyle = UIModalPresentationPopover;
  279. UIPopoverPresentationController *sortPopVC = vc.popoverPresentationController;
  280. sortPopVC.delegate = self;
  281. sortPopVC.sourceRect = btn.bounds;
  282. sortPopVC.sourceView = btn;
  283. sortPopVC.permittedArrowDirections = UIPopoverArrowDirectionDown;
  284. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:vc animated:YES completion:nil];
  285. }
  286. - (void)fontSizeBtnClicked:(id)sender
  287. {
  288. UIButton *btn = (UIButton *)sender;
  289. btn.selected = !btn.selected;
  290. if (!_setFontSizeView) {
  291. _setFontSizeView = [[PDFSettingFontView alloc] initWithFrame:CGRectMake(0, 0, 145, 300) minValue:2 maxValue:100];
  292. _setFontSizeView.fontNamedelegate = self;
  293. _setFontSizeView.curValue = self.fontSize;
  294. _setFontSizeView.center = CGPointMake(btn.center.x, _setFontSizeView.center.y);
  295. _setFontSizeView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
  296. }
  297. UIViewController *vc = [[UIViewController alloc] init];
  298. [vc.view addSubview:_setFontSizeView];
  299. vc.view.backgroundColor = [UIColor clearColor];
  300. vc.preferredContentSize = CGSizeMake(145, 300);
  301. vc.modalPresentationStyle = UIModalPresentationPopover;
  302. UIPopoverPresentationController *sortPopVC = vc.popoverPresentationController;
  303. sortPopVC.delegate = self;
  304. sortPopVC.sourceRect = btn.bounds;
  305. sortPopVC.sourceView = btn;
  306. sortPopVC.permittedArrowDirections = UIPopoverArrowDirectionDown;
  307. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:vc animated:YES completion:nil];
  308. }
  309. - (void)opacityBtnClicked:(id)sender
  310. {
  311. UIButton *btn = (UIButton *)sender;
  312. btn.selected = !btn.selected;
  313. if (!_opacticyView) {
  314. _opacticyView = [[PDFSettingFontView alloc] initWithFrame:CGRectMake(0, 0, 145, 300) minValue:25 maxValue:100];
  315. _opacticyView.fontNamedelegate = self;
  316. _opacticyView.center = CGPointMake(btn.center.x, _opacticyView.center.y);
  317. _opacticyView.curValue = self.opacity;
  318. _opacticyView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
  319. }
  320. UIViewController *vc = [[UIViewController alloc] init];
  321. [vc.view addSubview:_opacticyView];
  322. vc.preferredContentSize = CGSizeMake(145, 300);
  323. vc.modalPresentationStyle = UIModalPresentationPopover;
  324. UIPopoverPresentationController *sortPopVC = vc.popoverPresentationController;
  325. sortPopVC.delegate = self;
  326. sortPopVC.sourceRect = btn.bounds;
  327. vc.view.backgroundColor = [UIColor clearColor];
  328. sortPopVC.sourceView = btn;
  329. sortPopVC.permittedArrowDirections = UIPopoverArrowDirectionDown;
  330. [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:vc animated:YES completion:nil];
  331. }
  332. - (void)boldSelected:(id)sender
  333. {
  334. UIButton *btn = (UIButton *)sender;
  335. btn.selected = !btn.selected;
  336. _isBold = btn.selected;
  337. [self updateFontName:[self constructionFontname:_fontName isBold:_isBold isItalic:_isItalic]];
  338. }
  339. - (void)italicSelected:(id)sender
  340. {
  341. UIButton *btn = (UIButton *)sender;
  342. btn.selected = !btn.selected;
  343. _isItalic = btn.selected;
  344. [self updateFontName:[self constructionFontname:_fontName isBold:_isBold isItalic:_isItalic]];
  345. }
  346. - (void)doneSelected:(id)sender
  347. {
  348. if (_delegate && [_delegate respondsToSelector:@selector(keyboardShouldDissmiss:)]) {
  349. [_delegate keyboardShouldDissmiss:self];
  350. }
  351. }
  352. - (void)fontNamesView:(PDFSettingFontView *)fontNamesView didSelectedTextsize:(CGFloat)size
  353. {
  354. if ([fontNamesView isEqual:self.setFontSizeView]) {
  355. if (_delegate && [_delegate respondsToSelector:@selector(keyboard:updateFontSize:)]){
  356. [_delegate keyboard:self updateFontSize:size];
  357. [_sizeButton setTitle:[NSString stringWithFormat:@"%.0f",size] forState:UIControlStateNormal];
  358. }
  359. } else if ([fontNamesView isEqual:self.opacticyView]){
  360. if (_delegate && [_delegate respondsToSelector:@selector(keyboard:updateOpacity:)]) {
  361. [_delegate keyboard:self updateOpacity:size / 100.0];
  362. [_opacityButton setTitle:[NSString stringWithFormat:@"%.0f",size] forState:UIControlStateNormal];
  363. }
  364. }
  365. }
  366. #pragma mark - ColorPickViewDelegate
  367. - (void)colorPick:(PDFKTColorPickView *)view updateColor:(UIColor *)color
  368. {
  369. if (_delegate && [_delegate respondsToSelector:@selector(keyboard:updateTextColor:)])
  370. [_delegate keyboard:self updateTextColor:color];
  371. }
  372. - (void)colorPickDidBegan:(PDFKTColorPickView *)view{
  373. self.scrollView.scrollEnabled = NO;
  374. }
  375. - (void)colorPickDidEnd:(PDFKTColorPickView *)view{
  376. self.scrollView.scrollEnabled = YES;
  377. }
  378. #pragma mark - FontNameViewDelegate
  379. - (void)fontNamesView:(PDFKTFontNameView*)fontNamesView didSelectedTextFontName:(NSString *)fontName
  380. {
  381. [_fontButton setTitle:fontName forState:UIControlStateNormal];
  382. self.fontName = fontName;
  383. [self updateFontName:[self constructionFontname:_fontName isBold:_isBold isItalic:_isItalic]];
  384. }
  385. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  386. {
  387. UITouch *touch = [touches anyObject];
  388. CGPoint location = [touch locationInView:self];
  389. if (location.y < 800 - 44)
  390. {
  391. if ([_fontNameView superview])
  392. {
  393. [_fontNameView removeFromSuperview];
  394. _fontButton.selected = !_fontButton.selected;
  395. }
  396. if (_delegate && [_delegate respondsToSelector:@selector(keyboardShouldDissmiss:)])
  397. [_delegate keyboardShouldDissmiss:self];
  398. }
  399. }
  400. - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
  401. {
  402. UIView *hitView = [super hitTest:point withEvent:event];
  403. if (hitView == self)
  404. {
  405. return nil;
  406. }
  407. else
  408. {
  409. return hitView;
  410. }
  411. }
  412. - (void)analyzeFont:(NSString *)fontName
  413. {
  414. _isBold = NO;
  415. _isItalic = NO;
  416. if ([fontName rangeOfString:@"Bold"].location != NSNotFound)
  417. _isBold = YES;
  418. if ([fontName rangeOfString:@"Oblique"].location != NSNotFound)
  419. _isItalic = YES;
  420. if ([fontName rangeOfString:@"Italic"].location != NSNotFound)
  421. _isItalic = YES;
  422. if ([fontName rangeOfString:@"Helvetica"].location != NSNotFound)
  423. {
  424. _fontName = @"Helvetica";
  425. return ;
  426. }
  427. if ([fontName rangeOfString:@"Courier"].location != NSNotFound)
  428. {
  429. _fontName = @"Courier";
  430. return ;
  431. }
  432. if ([fontName rangeOfString:@"Times"].location != NSNotFound)
  433. {
  434. _fontName = @"Times-Roman";
  435. }
  436. }
  437. - (NSString *)constructionFontname:(NSString *)baseName isBold:(BOOL)isBold isItalic:(BOOL)isItalic
  438. {
  439. NSString *result;
  440. if ([baseName rangeOfString:@"Times"].location != NSNotFound)
  441. {
  442. if (isBold || isItalic)
  443. {
  444. if (isBold && isItalic) return @"Times-BoldItalic";
  445. if (isBold) return @"Times-Bold";
  446. if (isItalic) return @"Times-Italic";
  447. }
  448. else
  449. return @"Times-Roman";
  450. }
  451. if (isBold || isItalic)
  452. {
  453. result = [NSString stringWithFormat:@"%@-",baseName];
  454. if (isBold) result = [NSString stringWithFormat:@"%@Bold",result];
  455. if (isItalic) result = [NSString stringWithFormat:@"%@Oblique",result];
  456. }
  457. else return baseName;
  458. return result;
  459. }
  460. - (void)updateFontName:(NSString *)fontName
  461. {
  462. if (_delegate && [_delegate respondsToSelector:@selector(keyboard:updateFontName:)])
  463. [_delegate keyboard:self updateFontName:fontName];
  464. }
  465. #pragma mark - UIPopoverPresentationControllerDelegate
  466. - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection {
  467. return UIModalPresentationNone;
  468. }
  469. @end