ThumbsMainToolbar.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // ThumbsMainToolbar.m
  3. // Reader v2.8.6
  4. //
  5. // Created by Julius Oklamcak on 2011-09-01.
  6. // Copyright © 2011-2015 Julius Oklamcak. All rights reserved.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights to
  11. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  12. // of the Software, and to permit persons to whom the Software is furnished to
  13. // do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in all
  16. // copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. //
  25. #import "ReaderConstants.h"
  26. #import "ThumbsMainToolbar.h"
  27. @implementation ThumbsMainToolbar
  28. #pragma mark - Constants
  29. #define BUTTON_X 8.0f
  30. #define BUTTON_Y 8.0f
  31. #define BUTTON_SPACE 8.0f
  32. #define BUTTON_HEIGHT 30.0f
  33. #define BUTTON_FONT_SIZE 15.0f
  34. #define TEXT_BUTTON_PADDING 24.0f
  35. #define SHOW_CONTROL_WIDTH 78.0f
  36. #define ICON_BUTTON_WIDTH 40.0f
  37. #define TITLE_FONT_SIZE 19.0f
  38. #define TITLE_HEIGHT 28.0f
  39. #define COLOR_BAR [UIColor colorWithRed:(248.0f/255.0f) green:(248.0f/255.0f) blue:(248.0f/255.0f) alpha:1.0]
  40. #define COLOR_ARANCIO [UIColor colorWithRed:241.0/255.0 green:90.0/255.0 blue:34.0/255.0 alpha:1.0]
  41. #pragma mark - Properties
  42. @synthesize delegate;
  43. #pragma mark - ThumbsMainToolbar instance methods
  44. - (instancetype)initWithFrame:(CGRect)frame
  45. {
  46. return [self initWithFrame:frame title:nil];
  47. }
  48. - (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title
  49. {
  50. if ((self = [super initWithFrame:frame]))
  51. {
  52. CGFloat viewWidth = self.bounds.size.width; // Toolbar view width
  53. #if (READER_FLAT_UI == TRUE) // Option
  54. UIImage *buttonH = nil; UIImage *buttonN = nil;
  55. #else
  56. UIImage *buttonH = [[UIImage imageNamed:@"Reader-Button-H"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];
  57. UIImage *buttonN = [[UIImage imageNamed:@"Reader-Button-N"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];
  58. #endif // end of READER_FLAT_UI Option
  59. BOOL largeDevice = ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad);
  60. const CGFloat buttonSpacing = BUTTON_SPACE; //const CGFloat iconButtonWidth = ICON_BUTTON_WIDTH;
  61. CGFloat titleX = BUTTON_X; CGFloat titleWidth = (viewWidth - (titleX + titleX));
  62. CGFloat leftButtonX = BUTTON_X; // Left-side button start X position
  63. UIFont *doneButtonFont = [UIFont systemFontOfSize:BUTTON_FONT_SIZE];
  64. NSString *doneButtonText = NSLocalizedString(@"Done", @"button");
  65. //CGSize doneButtonSize = [doneButtonText sizeWithFont:doneButtonFont];
  66. CGSize doneButtonSize = [doneButtonText sizeWithAttributes:@{NSFontAttributeName: doneButtonFont}];
  67. CGFloat doneButtonWidth = (doneButtonSize.width + TEXT_BUTTON_PADDING);
  68. UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
  69. doneButton.frame = CGRectMake(leftButtonX, BUTTON_Y, doneButtonWidth, BUTTON_HEIGHT);
  70. [doneButton setTitleColor:COLOR_ARANCIO forState:UIControlStateNormal];
  71. [doneButton setTitleColor:[UIColor colorWithWhite:1.0f alpha:1.0f] forState:UIControlStateHighlighted];
  72. [doneButton setTitle:doneButtonText forState:UIControlStateNormal]; doneButton.titleLabel.font = doneButtonFont;
  73. [doneButton addTarget:self action:@selector(doneButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
  74. [doneButton setBackgroundImage:buttonH forState:UIControlStateHighlighted];
  75. [doneButton setBackgroundImage:buttonN forState:UIControlStateNormal];
  76. doneButton.autoresizingMask = UIViewAutoresizingNone;
  77. //doneButton.backgroundColor = [UIColor grayColor];
  78. doneButton.exclusiveTouch = YES;
  79. [self addSubview:doneButton]; //leftButtonX += (doneButtonWidth + buttonSpacing);
  80. titleX += (doneButtonWidth + buttonSpacing); titleWidth -= (doneButtonWidth + buttonSpacing);
  81. #if (READER_BOOKMARKS == TRUE) // Option
  82. CGFloat showControlX = (viewWidth - (SHOW_CONTROL_WIDTH + buttonSpacing));
  83. UIImage *thumbsImage = [UIImage imageNamed:@"Reader-Thumbs"];
  84. UIImage *bookmarkImage = [UIImage imageNamed:@"Reader-Mark-Y"];
  85. NSArray *buttonItems = [NSArray arrayWithObjects:thumbsImage, bookmarkImage, nil];
  86. BOOL useTint = [self respondsToSelector:@selector(tintColor)]; // iOS 7 and up
  87. UISegmentedControl *showControl = [[UISegmentedControl alloc] initWithItems:buttonItems];
  88. showControl.frame = CGRectMake(showControlX, BUTTON_Y, SHOW_CONTROL_WIDTH, BUTTON_HEIGHT);
  89. showControl.tintColor = (useTint ? [UIColor blackColor] : [UIColor colorWithWhite:0.8f alpha:1.0f]);
  90. showControl.tintColor = COLOR_ARANCIO;
  91. showControl.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  92. //showControl.segmentedControlStyle = UISegmentedControlStyleBar;
  93. showControl.selectedSegmentIndex = 0; // Default segment index
  94. showControl.exclusiveTouch = YES;
  95. [showControl addTarget:self action:@selector(showControlTapped:) forControlEvents:UIControlEventValueChanged];
  96. [self addSubview:showControl];
  97. titleWidth -= (SHOW_CONTROL_WIDTH + buttonSpacing);
  98. #endif // end of READER_BOOKMARKS Option
  99. if (largeDevice == YES) // Show document filename in toolbar
  100. {
  101. CGRect titleRect = CGRectMake(titleX, BUTTON_Y, titleWidth, TITLE_HEIGHT);
  102. UILabel *titleLabel = [[UILabel alloc] initWithFrame:titleRect];
  103. titleLabel.textAlignment = NSTextAlignmentCenter;
  104. titleLabel.font = [UIFont systemFontOfSize:TITLE_FONT_SIZE];
  105. titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  106. titleLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
  107. titleLabel.textColor = [UIColor colorWithWhite:0.0f alpha:1.0f];
  108. titleLabel.backgroundColor = [UIColor clearColor];
  109. titleLabel.adjustsFontSizeToFitWidth = YES;
  110. titleLabel.minimumScaleFactor = 0.75f;
  111. titleLabel.text = title;
  112. #if (READER_FLAT_UI == FALSE) // Option
  113. titleLabel.shadowColor = [UIColor colorWithWhite:0.65f alpha:1.0f];
  114. titleLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
  115. #endif // end of READER_FLAT_UI Option
  116. [self addSubview:titleLabel];
  117. }
  118. }
  119. return self;
  120. }
  121. #pragma mark - UISegmentedControl action methods
  122. - (void)showControlTapped:(UISegmentedControl *)control
  123. {
  124. [delegate tappedInToolbar:self showControl:control];
  125. }
  126. #pragma mark - UIButton action methods
  127. - (void)doneButtonTapped:(UIButton *)button
  128. {
  129. [delegate tappedInToolbar:self doneButton:button];
  130. }
  131. @end