ThumbsMainToolbar.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // ThumbsMainToolbar.m
  3. // Reader v2.9.0
  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. #pragma mark - Properties
  40. @synthesize delegate;
  41. #pragma mark - ThumbsMainToolbar instance methods
  42. - (instancetype)initWithFrame:(CGRect)frame
  43. {
  44. return [self initWithFrame:frame title:nil];
  45. }
  46. - (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title
  47. {
  48. if ((self = [super initWithFrame:frame]))
  49. {
  50. CGFloat viewWidth = self.bounds.size.width; // Toolbar view width
  51. #if (READER_FLAT_UI == TRUE) // Option
  52. UIImage *buttonH = nil; UIImage *buttonN = nil;
  53. #else
  54. UIImage *buttonH = [[UIImage imageNamed:@"Reader-Button-H"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];
  55. UIImage *buttonN = [[UIImage imageNamed:@"Reader-Button-N"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];
  56. #endif // end of READER_FLAT_UI Option
  57. BOOL largeDevice = ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad);
  58. const CGFloat buttonSpacing = BUTTON_SPACE; //const CGFloat iconButtonWidth = ICON_BUTTON_WIDTH;
  59. CGFloat titleX = BUTTON_X; CGFloat titleWidth = (viewWidth - (titleX + titleX));
  60. CGFloat leftButtonX = BUTTON_X; // Left-side button start X position
  61. UIFont *doneButtonFont = [UIFont systemFontOfSize:BUTTON_FONT_SIZE];
  62. NSString *doneButtonText = NSLocalizedString(@"Done", @"button text");
  63. CGSize doneButtonSize = [doneButtonText sizeWithAttributes:@{NSFontAttributeName : doneButtonFont}];
  64. CGFloat doneButtonWidth = (ceil(doneButtonSize.width) + TEXT_BUTTON_PADDING);
  65. UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
  66. doneButton.frame = CGRectMake(leftButtonX, BUTTON_Y, doneButtonWidth, BUTTON_HEIGHT);
  67. [doneButton setTitleColor:[UIColor colorWithWhite:0.0f alpha:1.0f] forState:UIControlStateNormal];
  68. [doneButton setTitleColor:[UIColor colorWithWhite:1.0f alpha:1.0f] forState:UIControlStateHighlighted];
  69. [doneButton setTitle:doneButtonText forState:UIControlStateNormal]; doneButton.titleLabel.font = doneButtonFont;
  70. [doneButton addTarget:self action:@selector(doneButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
  71. [doneButton setBackgroundImage:buttonH forState:UIControlStateHighlighted];
  72. [doneButton setBackgroundImage:buttonN forState:UIControlStateNormal];
  73. doneButton.autoresizingMask = UIViewAutoresizingNone;
  74. //doneButton.backgroundColor = [UIColor grayColor];
  75. doneButton.exclusiveTouch = YES;
  76. [self addSubview:doneButton]; //leftButtonX += (doneButtonWidth + buttonSpacing);
  77. titleX += (doneButtonWidth + buttonSpacing); titleWidth -= (doneButtonWidth + buttonSpacing);
  78. #if (READER_BOOKMARKS == TRUE) // Option
  79. CGFloat showControlX = (viewWidth - (SHOW_CONTROL_WIDTH + buttonSpacing));
  80. UIImage *thumbsImage = [UIImage imageNamed:@"Reader-Thumbs"];
  81. UIImage *bookmarkImage = [UIImage imageNamed:@"Reader-Mark-Y"];
  82. NSArray *buttonItems = [NSArray arrayWithObjects:thumbsImage, bookmarkImage, nil];
  83. BOOL useTint = [self respondsToSelector:@selector(tintColor)]; // iOS 7 and up
  84. UISegmentedControl *showControl = [[UISegmentedControl alloc] initWithItems:buttonItems];
  85. showControl.frame = CGRectMake(showControlX, BUTTON_Y, SHOW_CONTROL_WIDTH, BUTTON_HEIGHT);
  86. showControl.tintColor = (useTint ? [UIColor blackColor] : [UIColor colorWithWhite:0.8f alpha:1.0f]);
  87. showControl.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  88. //showControl.segmentedControlStyle = UISegmentedControlStyleBar;
  89. showControl.selectedSegmentIndex = 0; // Default segment index
  90. //showControl.backgroundColor = [UIColor grayColor];
  91. showControl.exclusiveTouch = YES;
  92. [showControl addTarget:self action:@selector(showControlTapped:) forControlEvents:UIControlEventValueChanged];
  93. [self addSubview:showControl];
  94. titleWidth -= (SHOW_CONTROL_WIDTH + buttonSpacing);
  95. #endif // end of READER_BOOKMARKS Option
  96. if (largeDevice == YES) // Show document filename in toolbar
  97. {
  98. CGRect titleRect = CGRectMake(titleX, BUTTON_Y, titleWidth, TITLE_HEIGHT);
  99. UILabel *titleLabel = [[UILabel alloc] initWithFrame:titleRect];
  100. titleLabel.textAlignment = NSTextAlignmentCenter;
  101. titleLabel.font = [UIFont systemFontOfSize:TITLE_FONT_SIZE];
  102. titleLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  103. titleLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
  104. titleLabel.textColor = [UIColor colorWithWhite:0.0f alpha:1.0f];
  105. titleLabel.backgroundColor = [UIColor clearColor];
  106. titleLabel.adjustsFontSizeToFitWidth = YES;
  107. titleLabel.minimumScaleFactor = 0.75f;
  108. titleLabel.text = title;
  109. #if (READER_FLAT_UI == FALSE) // Option
  110. titleLabel.shadowColor = [UIColor colorWithWhite:0.65f alpha:1.0f];
  111. titleLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
  112. #endif // end of READER_FLAT_UI Option
  113. [self addSubview:titleLabel];
  114. }
  115. }
  116. return self;
  117. }
  118. #pragma mark - UISegmentedControl action methods
  119. - (void)showControlTapped:(UISegmentedControl *)control
  120. {
  121. [delegate tappedInToolbar:self showControl:control];
  122. }
  123. #pragma mark - UIButton action methods
  124. - (void)doneButtonTapped:(UIButton *)button
  125. {
  126. [delegate tappedInToolbar:self doneButton:button];
  127. }
  128. @end