// // PDFKeyboardToolbar.m // PDFReader // // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved. // // The PDF Reader Sample applications are licensed with a modified BSD license. // Please see License for details. This notice may not be removed from this file. // #import "PDFKeyboardToolbar.h" #import "PDFKTColorPickView.h" #import "PDFKTSlideView.h" #import "PDFKTFontNamePopBgView.h" @interface PDFKeyboardToolbar () @property (nonatomic,retain) UIView *toolbarView; @property (nonatomic,retain) CALayer *topBorder; @property (nonatomic,retain) NSArray *buttonsToAdd; @property (nonatomic,retain) PDFKTColorPickView *colorPickView; @property (nonatomic,retain) PDFKTFontNameView *fontNameView; @property (nonatomic,retain) PDFSettingFontView *setFontSizeView; @property (nonatomic,retain) PDFSettingFontView *opacticyView; @property (nonatomic,assign) CGFloat centerY; @property (nonatomic,retain) UIButton *fontButton; @property (nonatomic,retain) UIButton *sizeButton; @property (nonatomic,retain) UIButton *opacityButton; @property (nonatomic,retain) UIButton *boldButton; @property (nonatomic,retain) UIButton *italicButton; @property (nonatomic, retain) UILabel *fontLabel; @property (nonatomic, retain) UILabel *sizeLabel; @property (nonatomic, retain) UILabel *opacityLabel; @property (nonatomic, retain) UIImageView *colorPick; @property (nonatomic,assign) BOOL isBold; @property (nonatomic,assign) BOOL isItalic; @property (nonatomic, retain) UIScrollView *scrollView; @property (nonatomic, retain) UIButton *doneButton; @end @implementation PDFKeyboardToolbar @synthesize fontName = _fontName; @synthesize fontSize = _fontSize; @synthesize delegate = _delegate; @synthesize opacity = _opacity; - (id)initWithFontName:(NSString *)fontName fontSize:(CGFloat)fontSize{ self = [super initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 40)]; if (self) { _buttonsToAdd = nil; _fontName = [fontName copy]; _fontSize = fontSize; self.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; self.backgroundColor = [UIColor colorWithRed:224.0/255.0 green:224.0/255.0 blue:224.0/255.0 alpha:1.0]; _centerY = self.center.y; [self initButtonsToAdd]; _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds]; self.scrollView.clipsToBounds = NO; [self addSubview:self.scrollView]; for (UIView *eachButton in _buttonsToAdd) { [self.scrollView addSubview:eachButton]; } } return self; } - (id)initWithViews:(NSArray*)buttons { self = [super initWithFrame:CGRectMake(0, 0, self.window.rootViewController.view.bounds.size.width, 40)]; if (self) { self.buttonsToAdd = buttons; self.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; [self addSubview:[self inputAccessoryView]]; } return self; } - (void)dealloc{ [_doneButton release]; _delegate = nil; [_fontName release]; [_buttonsToAdd release]; [_toolbarView release]; _scrollView.delegate = nil; [_scrollView release]; [_fontNameView release]; [_colorPickView release]; [_colorPick release]; [_sizeButton release]; [_setFontSizeView release]; [_opacityButton release]; [_opacticyView release]; [super dealloc]; } - (void)bindToTextView:(UITextView *)textView { [textView setInputAccessoryView:self]; } - (void)bindToTextField:(UITextField *)textField { [textField setInputAccessoryView:self]; } - (void)refreshFontName:(NSString *)fontName fontSize:(CGFloat)fontSize opacity:(CGFloat)opacity { self.fontName = fontName; _fontSize = fontSize; _opacity = opacity * 100.0; [self analyzeFont:_fontName]; if (_fontButton) [_fontButton setTitle:_fontName forState:UIControlStateNormal]; if (_opacityButton) [_opacityButton setTitle:[NSString stringWithFormat:@"%.0f",_opacity] forState:UIControlStateNormal]; if (_sizeButton) [_sizeButton setTitle:[NSString stringWithFormat:@"%.0f",_fontSize] forState:UIControlStateNormal]; _boldButton.selected = _isBold; _italicButton.selected = _isItalic; } - (void)layoutSubviews { CGRect frame = _toolbarView.bounds; frame.size.height = 0.5f; _topBorder.frame = frame; [self.sizeLabel sizeToFit]; [self.opacityLabel sizeToFit]; CGFloat space = 5; if (self.bounds.size.width > 768) { space = 20; } CGFloat posX = space; _boldButton.center = CGPointMake(posX+_boldButton.frame.size.width/2.0, _centerY); posX += _boldButton.frame.size.width+space; _italicButton.center = CGPointMake(posX+_italicButton.frame.size.width/2.0, _centerY); posX += _italicButton.frame.size.width+space; _fontButton.center = CGPointMake(posX+_fontButton.frame.size.width/2.0, _centerY); posX += _fontButton.frame.size.width+space; self.sizeLabel.center = CGPointMake(posX+self.sizeLabel.frame.size.width/2.0, _centerY); posX += self.sizeLabel.frame.size.width+5; self.sizeButton.center = CGPointMake(posX+self.sizeButton.frame.size.width/2.0, _centerY); posX += self.sizeButton.frame.size.width+space; self.opacityLabel.center = CGPointMake(posX+self.opacityLabel.frame.size.width/2.0, _centerY); posX += self.opacityLabel.frame.size.width+5; self.opacityButton.center = CGPointMake(posX+self.opacityButton.frame.size.width/2.0, _centerY); posX += self.opacityButton.frame.size.width; CGFloat width = self.colorPick.frame.size.width+self.doneButton.frame.size.width+3*space+16; if (posX+width < self.bounds.size.width) { posX += (self.bounds.size.width-width-posX+space); } else { posX += space; } self.colorPick.center = CGPointMake(posX+self.colorPick.frame.size.width/2.0, _centerY); _colorPickView.frame = self.colorPick.frame; posX += self.colorPick.frame.size.width+space+8; self.doneButton.center = CGPointMake(posX+self.doneButton.frame.size.width/2.0, _centerY); posX += self.doneButton.frame.size.width+space; self.scrollView.frame = self.bounds; self.scrollView.contentSize = CGSizeMake(posX, self.frame.size.height); } - (void)initButtonsToAdd{ CGFloat boundsWidth = self.bounds.size.width; _boldButton = [UIButton buttonWithType:UIButtonTypeCustom]; UIImage *boldImage = [UIImage imageNamed:@"TImageNameBoldLetter"]; [_boldButton setImage:boldImage forState:UIControlStateNormal]; _boldButton.bounds = CGRectMake(0, 0, boldImage.size.width, boldImage.size.height); _boldButton.center = CGPointMake(boundsWidth * 0.03 *0.85, _centerY); // _boldButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; [_boldButton addTarget:self action:@selector(boldSelected:) forControlEvents:UIControlEventTouchUpInside]; [_boldButton setImage:[UIImage imageNamed:@"TImageNameSelectBoldLetter"] forState:UIControlStateSelected]; _italicButton = [UIButton buttonWithType:UIButtonTypeCustom]; UIImage *italicImage = [UIImage imageNamed:@"TImageNameLeanIcon"]; [_italicButton setImage:italicImage forState:UIControlStateNormal]; _italicButton.bounds = CGRectMake(0, 0, italicImage.size.width, italicImage.size.height); _italicButton.center = CGPointMake(boundsWidth * 0.1*0.85, _centerY); // _italicButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin; [_italicButton addTarget:self action:@selector(italicSelected:) forControlEvents:UIControlEventTouchUpInside]; [_italicButton setImage:[UIImage imageNamed:@"TImageNameSelectLeanIcon"] forState:UIControlStateSelected]; UILabel *fontLabel = [[UILabel alloc] initWithFrame:CGRectMake(110, 5, 50, 18)]; self.fontLabel = fontLabel; fontLabel.text = NSLocalizedString(@"Fonts", nil); fontLabel.font = [UIFont systemFontOfSize:18]; fontLabel.center = CGPointMake(boundsWidth * 0.18*0.85, _centerY); fontLabel.backgroundColor = [UIColor clearColor]; // fontLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin; _fontButton = [UIButton buttonWithType:UIButtonTypeCustom]; _fontButton.backgroundColor = [UIColor colorWithRed:142/255.0 green:142.0/255.0 blue:142/255.0 alpha:1.0]; _fontButton.layer.cornerRadius = 5.0; [_fontButton setTitle:_fontName forState:UIControlStateNormal]; [_fontButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; _fontButton.bounds = CGRectMake(0, 0, 129,34.0); _fontButton.center = CGPointMake(boundsWidth * 0.31*0.85, _centerY); // _fontButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth; [_fontButton addTarget:self action:@selector(fontBtnClicked:) forControlEvents:UIControlEventTouchUpInside]; UILabel *sizeLabel = [[UILabel alloc] initWithFrame:CGRectMake(300, 5, 50, 20)]; self.sizeLabel = sizeLabel; sizeLabel.text = NSLocalizedString(@"Size", nil); sizeLabel.font = [UIFont systemFontOfSize:18]; sizeLabel.backgroundColor = [UIColor clearColor]; sizeLabel.center = CGPointMake(boundsWidth *0.45*0.85, _centerY); // sizeLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin ; self.sizeButton = [UIButton buttonWithType:UIButtonTypeCustom]; _sizeButton.backgroundColor = [UIColor colorWithRed:142/255.0 green:142.0/255.0 blue:142/255.0 alpha:1.0]; _sizeButton.layer.cornerRadius = 5.0; [_sizeButton setTitle:[NSString stringWithFormat:@"%.0f",_fontSize] forState:UIControlStateNormal]; [_sizeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; _sizeButton.bounds = CGRectMake(0, 0, 51,34.0); _sizeButton.center = CGPointMake(boundsWidth * 0.52*0.85, _centerY); [_sizeButton addTarget:self action:@selector(fontSizeBtnClicked:) forControlEvents:UIControlEventTouchUpInside]; UILabel *opacityLabel = [[UILabel alloc] initWithFrame:CGRectMake(300, 5, 75, 40)]; self.opacityLabel = opacityLabel; opacityLabel.text = NSLocalizedString(@"Opacity", nil); opacityLabel.font = [UIFont systemFontOfSize:18]; opacityLabel.backgroundColor = [UIColor clearColor]; // [opacityLabel sizeToFit]; opacityLabel.center = CGPointMake(boundsWidth *0.62*0.85, _centerY); // opacityLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin ; self.opacityButton = [UIButton buttonWithType:UIButtonTypeCustom]; _opacityButton.backgroundColor = [UIColor colorWithRed:142/255.0 green:142.0/255.0 blue:142/255.0 alpha:1.0]; _opacityButton.layer.cornerRadius = 5.0; [_opacityButton setTitle:[NSString stringWithFormat:@"%.0f",_opacity] forState:UIControlStateNormal]; [_opacityButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; _opacityButton.bounds = CGRectMake(0, 0, 51,34.0); _opacityButton.center = CGPointMake(boundsWidth * 0.725*0.85, _centerY); [_opacityButton addTarget:self action:@selector(opacityBtnClicked:) forControlEvents:UIControlEventTouchUpInside]; UIImage *colorPickImg = [UIImage imageNamed:@"text_color_bar.png"]; UIImageView *colorPick = [[UIImageView alloc] initWithImage:colorPickImg]; self.colorPick = colorPick; colorPick.frame = CGRectMake(0, 0, colorPickImg.size.width, colorPickImg.size.height); colorPick.center = CGPointMake(boundsWidth * 0.82, _centerY); // colorPick.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin; _colorPickView = [[PDFKTColorPickView alloc] initWithFrame:colorPick.frame]; _colorPickView.colorPickDelegate = self; // _colorPickView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin; self.doneButton = [UIButton buttonWithType:UIButtonTypeCustom]; [self.doneButton setTitle:NSLocalizedString(@"Done", nil) forState:UIButtonTypeCustom]; [self.doneButton setTitleColor:[UIColor colorWithRed:0 green:122.0/255.0 blue:1.0 alpha:1.0] forState:UIControlStateNormal]; [self.doneButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted]; [self.doneButton sizeToFit]; [self.doneButton addTarget:self action:@selector(doneSelected:) forControlEvents:UIControlEventTouchUpInside]; _buttonsToAdd = [[NSArray alloc]initWithObjects:_boldButton,_italicButton,_fontButton,sizeLabel,_sizeButton,opacityLabel,_opacityButton,colorPick,_colorPickView,self.doneButton, nil]; [fontLabel release]; [sizeLabel release]; [opacityLabel release]; [colorPick release]; } - (UIView*)inputAccessoryView { _toolbarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 40)]; _toolbarView.backgroundColor = [UIColor colorWithWhite:0.973 alpha:1.0]; _toolbarView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; _topBorder = [CALayer layer]; _topBorder.frame = CGRectMake(0.0f, 0.0f, self.bounds.size.width, 0.5f); _topBorder.backgroundColor = [UIColor colorWithWhite:0.678 alpha:1.0].CGColor; [_toolbarView.layer addSublayer:_topBorder]; [_toolbarView addSubview:[self fakeToolbar]]; return [_toolbarView autorelease]; } - (UIScrollView*)fakeToolbar { _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 40)]; _scrollView.backgroundColor = [UIColor clearColor]; _scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; _scrollView.showsHorizontalScrollIndicator = NO; NSUInteger originX = 0; if (!_buttonsToAdd) [self initButtonsToAdd];//采用init方式初始化 for (UIView *eachButton in _buttonsToAdd) { [_scrollView addSubview:eachButton]; originX = originX + eachButton.bounds.size.width; } CGSize contentSize = _scrollView.contentSize; contentSize.width = originX; _scrollView.contentSize = contentSize; return [_scrollView autorelease]; } - (void)fontBtnClicked:(id)sender { UIButton *btn = (UIButton *)sender; btn.selected = !btn.selected; if (!_fontNameView) { _fontNameView = [[PDFKTFontNameView alloc] initWithFrame:CGRectMake(0, 0, 160, 145)]; _fontNameView.fontNamedelegate = self; _fontNameView.fontName = self.fontName; _fontNameView.center = CGPointMake(btn.center.x, _fontNameView.center.y); _fontNameView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin; } UIViewController *vc = [[UIViewController alloc] init]; vc.view.backgroundColor = [UIColor clearColor]; [vc.view addSubview:_fontNameView]; vc.preferredContentSize = CGSizeMake(160, 145); vc.modalPresentationStyle = UIModalPresentationPopover; UIPopoverPresentationController *sortPopVC = vc.popoverPresentationController; sortPopVC.delegate = self; sortPopVC.sourceRect = btn.bounds; sortPopVC.sourceView = btn; sortPopVC.permittedArrowDirections = UIPopoverArrowDirectionDown; [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:vc animated:YES completion:nil]; } - (void)fontSizeBtnClicked:(id)sender { UIButton *btn = (UIButton *)sender; btn.selected = !btn.selected; if (!_setFontSizeView) { _setFontSizeView = [[PDFSettingFontView alloc] initWithFrame:CGRectMake(0, 0, 145, 300) minValue:2 maxValue:100]; _setFontSizeView.fontNamedelegate = self; _setFontSizeView.curValue = self.fontSize; _setFontSizeView.center = CGPointMake(btn.center.x, _setFontSizeView.center.y); _setFontSizeView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin; } UIViewController *vc = [[UIViewController alloc] init]; [vc.view addSubview:_setFontSizeView]; vc.view.backgroundColor = [UIColor clearColor]; vc.preferredContentSize = CGSizeMake(145, 300); vc.modalPresentationStyle = UIModalPresentationPopover; UIPopoverPresentationController *sortPopVC = vc.popoverPresentationController; sortPopVC.delegate = self; sortPopVC.sourceRect = btn.bounds; sortPopVC.sourceView = btn; sortPopVC.permittedArrowDirections = UIPopoverArrowDirectionDown; [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:vc animated:YES completion:nil]; } - (void)opacityBtnClicked:(id)sender { UIButton *btn = (UIButton *)sender; btn.selected = !btn.selected; if (!_opacticyView) { _opacticyView = [[PDFSettingFontView alloc] initWithFrame:CGRectMake(0, 0, 145, 300) minValue:25 maxValue:100]; _opacticyView.fontNamedelegate = self; _opacticyView.center = CGPointMake(btn.center.x, _opacticyView.center.y); _opacticyView.curValue = self.opacity; _opacticyView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin; } UIViewController *vc = [[UIViewController alloc] init]; [vc.view addSubview:_opacticyView]; vc.preferredContentSize = CGSizeMake(145, 300); vc.modalPresentationStyle = UIModalPresentationPopover; UIPopoverPresentationController *sortPopVC = vc.popoverPresentationController; sortPopVC.delegate = self; sortPopVC.sourceRect = btn.bounds; vc.view.backgroundColor = [UIColor clearColor]; sortPopVC.sourceView = btn; sortPopVC.permittedArrowDirections = UIPopoverArrowDirectionDown; [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:vc animated:YES completion:nil]; } - (void)boldSelected:(id)sender { UIButton *btn = (UIButton *)sender; btn.selected = !btn.selected; _isBold = btn.selected; [self updateFontName:[self constructionFontname:_fontName isBold:_isBold isItalic:_isItalic]]; } - (void)italicSelected:(id)sender { UIButton *btn = (UIButton *)sender; btn.selected = !btn.selected; _isItalic = btn.selected; [self updateFontName:[self constructionFontname:_fontName isBold:_isBold isItalic:_isItalic]]; } - (void)doneSelected:(id)sender { if (_delegate && [_delegate respondsToSelector:@selector(keyboardShouldDissmiss:)]) { [_delegate keyboardShouldDissmiss:self]; } } - (void)fontNamesView:(PDFSettingFontView *)fontNamesView didSelectedTextsize:(CGFloat)size { if ([fontNamesView isEqual:self.setFontSizeView]) { if (_delegate && [_delegate respondsToSelector:@selector(keyboard:updateFontSize:)]){ [_delegate keyboard:self updateFontSize:size]; [_sizeButton setTitle:[NSString stringWithFormat:@"%.0f",size] forState:UIControlStateNormal]; } } else if ([fontNamesView isEqual:self.opacticyView]){ if (_delegate && [_delegate respondsToSelector:@selector(keyboard:updateOpacity:)]) { [_delegate keyboard:self updateOpacity:size / 100.0]; [_opacityButton setTitle:[NSString stringWithFormat:@"%.0f",size] forState:UIControlStateNormal]; } } } #pragma mark - ColorPickViewDelegate - (void)colorPick:(PDFKTColorPickView *)view updateColor:(UIColor *)color { if (_delegate && [_delegate respondsToSelector:@selector(keyboard:updateTextColor:)]) [_delegate keyboard:self updateTextColor:color]; } - (void)colorPickDidBegan:(PDFKTColorPickView *)view{ self.scrollView.scrollEnabled = NO; } - (void)colorPickDidEnd:(PDFKTColorPickView *)view{ self.scrollView.scrollEnabled = YES; } #pragma mark - FontNameViewDelegate - (void)fontNamesView:(PDFKTFontNameView*)fontNamesView didSelectedTextFontName:(NSString *)fontName { [_fontButton setTitle:fontName forState:UIControlStateNormal]; self.fontName = fontName; [self updateFontName:[self constructionFontname:_fontName isBold:_isBold isItalic:_isItalic]]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInView:self]; if (location.y < 800 - 44) { if ([_fontNameView superview]) { [_fontNameView removeFromSuperview]; _fontButton.selected = !_fontButton.selected; } if (_delegate && [_delegate respondsToSelector:@selector(keyboardShouldDissmiss:)]) [_delegate keyboardShouldDissmiss:self]; } } - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { UIView *hitView = [super hitTest:point withEvent:event]; if (hitView == self) { return nil; } else { return hitView; } } - (void)analyzeFont:(NSString *)fontName { _isBold = NO; _isItalic = NO; if ([fontName rangeOfString:@"Bold"].location != NSNotFound) _isBold = YES; if ([fontName rangeOfString:@"Oblique"].location != NSNotFound) _isItalic = YES; if ([fontName rangeOfString:@"Italic"].location != NSNotFound) _isItalic = YES; if ([fontName rangeOfString:@"Helvetica"].location != NSNotFound) { _fontName = @"Helvetica"; return ; } if ([fontName rangeOfString:@"Courier"].location != NSNotFound) { _fontName = @"Courier"; return ; } if ([fontName rangeOfString:@"Times"].location != NSNotFound) { _fontName = @"Times-Roman"; } } - (NSString *)constructionFontname:(NSString *)baseName isBold:(BOOL)isBold isItalic:(BOOL)isItalic { NSString *result; if ([baseName rangeOfString:@"Times"].location != NSNotFound) { if (isBold || isItalic) { if (isBold && isItalic) return @"Times-BoldItalic"; if (isBold) return @"Times-Bold"; if (isItalic) return @"Times-Italic"; } else return @"Times-Roman"; } if (isBold || isItalic) { result = [NSString stringWithFormat:@"%@-",baseName]; if (isBold) result = [NSString stringWithFormat:@"%@Bold",result]; if (isItalic) result = [NSString stringWithFormat:@"%@Oblique",result]; } else return baseName; return result; } - (void)updateFontName:(NSString *)fontName { if (_delegate && [_delegate respondsToSelector:@selector(keyboard:updateFontName:)]) [_delegate keyboard:self updateFontName:fontName]; } #pragma mark - UIPopoverPresentationControllerDelegate - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection { return UIModalPresentationNone; } @end