REMenuItemView.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //
  2. // REMenuItemView.m
  3. // REMenu
  4. //
  5. // Copyright (c) 2013 Roman Efimov (https://github.com/romaonthego)
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. //
  25. #import "REMenuItemView.h"
  26. #import "REMenuItem.h"
  27. @interface REMenuItemView ()
  28. @property (strong, readwrite, nonatomic) UIView *backgroundView;
  29. @end
  30. @implementation REMenuItemView
  31. - (id)initWithFrame:(CGRect)frame menu:(REMenu *)menu item:(REMenuItem*) item hasSubtitle:(BOOL)hasSubtitle
  32. {
  33. self = [super initWithFrame:frame];
  34. if (self) {
  35. self.menu = menu;
  36. self.item = item;
  37. self.isAccessibilityElement = YES;
  38. self.accessibilityTraits = UIAccessibilityTraitButton;
  39. self.accessibilityHint = NSLocalizedString(@"Double tap to choose", @"Double tap to choose");
  40. _backgroundView = ({
  41. UIView *view = [[UIView alloc] initWithFrame:self.bounds];
  42. view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  43. if (menu.liveBlur && REUIKitIsFlatMode())
  44. view.alpha = 0.5f;
  45. view;
  46. });
  47. [self addSubview:_backgroundView];
  48. CGRect titleFrame;
  49. if (hasSubtitle) {
  50. // Dividing lines at 1/1.725 (vs 1/2.000) results in labels about 28-top 20-bottom or 60/40 title/subtitle (for a 48 frame height)
  51. //
  52. titleFrame = CGRectMake(self.item.textOffset.width == 0.0 && self.item.textOffset.height == 0.0 ? self.menu.textOffset.width : self.item.textOffset.width, self.item.textOffset.width == 0.0 && self.item.textOffset.height == 0.0 ? self.menu.textOffset.height : self.item.textOffset.height, 0, floorf(frame.size.height / 1.725));
  53. CGRect subtitleFrame = CGRectMake(self.item.subtitleTextOffset.width == 0.0 && self.item.subtitleTextOffset.height == 0.0 ? self.menu.subtitleTextOffset.width : self.item.subtitleTextOffset.width, (self.item.subtitleTextOffset.width == 0.0 && self.item.subtitleTextOffset.height == 0.0 ? self.menu.subtitleTextOffset.height : self.item.subtitleTextOffset.height) + titleFrame.size.height, 0, floorf(frame.size.height * (1.0 - 1.0 / 1.725)));
  54. self.subtitleLabel = ({
  55. UILabel *label =[[UILabel alloc] initWithFrame:subtitleFrame];
  56. label.contentMode = UIViewContentModeCenter;
  57. label.textAlignment = (NSInteger)self.item.subtitleTextAlignment == -1 ? self.menu.subtitleTextAlignment : self.item.subtitleTextAlignment;
  58. label.backgroundColor = [UIColor clearColor];
  59. label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  60. label.isAccessibilityElement = NO;
  61. label;
  62. });
  63. [self addSubview:_subtitleLabel];
  64. } else {
  65. titleFrame = CGRectMake(self.item.textOffset.width == 0.0 && self.item.textOffset.height == 0.0 ? self.menu.textOffset.width : self.item.textOffset.width, self.item.textOffset.width == 0.0 && self.item.textOffset.height == 0.0 ? self.menu.textOffset.height : self.item.textOffset.height, 0, frame.size.height);
  66. }
  67. _titleLabel = ({
  68. UILabel *label = [[UILabel alloc] initWithFrame:titleFrame];
  69. label.isAccessibilityElement = NO;
  70. label.contentMode = UIViewContentModeCenter;
  71. label.textAlignment = (NSInteger)self.item.textAlignment == -1 ? self.menu.textAlignment : self.item.subtitleTextAlignment;
  72. label.backgroundColor = [UIColor clearColor];
  73. label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  74. label;
  75. });
  76. _imageView = [[UIImageView alloc] initWithFrame:CGRectNull];
  77. _badgeLabel = ({
  78. UILabel *label = [[UILabel alloc] init];
  79. label.backgroundColor = [UIColor colorWithWhite:0.559 alpha:1.000];
  80. label.font = [UIFont systemFontOfSize:11];
  81. label.textAlignment = NSTextAlignmentCenter;
  82. label.textColor = [UIColor whiteColor];
  83. label.hidden = YES;
  84. label.layer.cornerRadius = 4.0;
  85. label.layer.borderColor = [UIColor colorWithWhite:0.630 alpha:1.000].CGColor;
  86. label.layer.borderWidth = 1.0;
  87. label.layer.masksToBounds = YES;
  88. label;
  89. });
  90. [self addSubview:_titleLabel];
  91. [self addSubview:_imageView];
  92. [self addSubview:_badgeLabel];
  93. }
  94. return self;
  95. }
  96. - (void)layoutSubviews
  97. {
  98. [super layoutSubviews];
  99. self.imageView.image = self.item.image;
  100. // Adjust frames
  101. //
  102. CGFloat verticalOffset = floor((self.frame.size.height - self.item.image.size.height) / 2.0);
  103. CGFloat horizontalOffset = floor((self.menu.itemHeight - self.item.image.size.height) / 2.0);
  104. CGFloat x = (self.menu.imageAlignment == REMenuImageAlignmentLeft) ? horizontalOffset + self.menu.imageOffset.width :
  105. self.titleLabel.frame.size.width - (horizontalOffset + self.menu.imageOffset.width + self.item.image.size.width);
  106. self.imageView.frame = CGRectMake(x, verticalOffset + self.menu.imageOffset.height, self.item.image.size.width, self.item.image.size.height);
  107. if ([self.imageView respondsToSelector:@selector(setTintColor:)]) {
  108. self.imageView.tintColor = self.menu.imageTintColor;
  109. }
  110. // Set up badge
  111. //
  112. self.badgeLabel.hidden = !self.item.badge;
  113. if (self.item.badge) {
  114. self.badgeLabel.text = self.item.badge;
  115. NSAttributedString *badgeAttributedString = [[NSAttributedString alloc] initWithString:self.item.badge
  116. attributes:@{NSFontAttributeName:self.badgeLabel.font}];
  117. CGRect rect = [badgeAttributedString boundingRectWithSize:CGSizeMake(CGRectGetMaxX(self.frame), CGRectGetMaxY(self.frame))
  118. options:NSStringDrawingUsesLineFragmentOrigin
  119. context:nil];
  120. CGFloat x = self.menu.imageAlignment == REMenuImageAlignmentLeft ? CGRectGetMaxX(self.imageView.frame) - 2.0 :
  121. CGRectGetMinX(self.imageView.frame) - rect.size.height - 4.0;
  122. self.badgeLabel.frame = CGRectMake(x, self.imageView.frame.origin.y - 2.0, rect.size.width + 6.0, rect.size.height + 2.0);
  123. if (self.menu.badgeLabelConfigurationBlock)
  124. self.menu.badgeLabelConfigurationBlock(self.badgeLabel, self.item);
  125. }
  126. // Accessibility
  127. //
  128. self.accessibilityLabel = self.item.title;
  129. if (self.subtitleLabel.text)
  130. self.accessibilityLabel = [NSString stringWithFormat:@"%@, %@", self.item.title, self.item.subtitle];
  131. // Adjust styles
  132. //
  133. self.backgroundView.backgroundColor = self.item.backgroundColor == nil ? [UIColor clearColor] : self.item.backgroundColor;
  134. self.titleLabel.font = self.item.font == nil ? self.menu.font : self.item.font;
  135. self.titleLabel.text = self.item.title;
  136. self.titleLabel.textColor = self.item.textColor == nil ? self.menu.textColor : self.item.textColor;
  137. self.titleLabel.shadowColor = self.item.textShadowColor ? self.menu.textShadowColor : self.item.textShadowColor;
  138. self.titleLabel.shadowOffset = self.item.textShadowOffset.width == 0 && self.item.textShadowOffset.height == 0 ? self.menu.textShadowOffset : self.item.textShadowOffset;
  139. self.titleLabel.textAlignment = (NSInteger)self.item.textAlignment == -1 ? self.menu.textAlignment : self.item.textAlignment;
  140. self.subtitleLabel.font = self.item.subtitleFont == nil ? self.menu.subtitleFont : self.item.subtitleFont;
  141. self.subtitleLabel.text = self.item.subtitle;
  142. self.subtitleLabel.textColor = self.item.subtitleTextColor == nil ? self.menu.subtitleTextColor : self.item.subtitleTextColor;
  143. self.subtitleLabel.shadowColor = self.item.subtitleTextShadowColor == nil ? self.menu.subtitleTextShadowColor : self.item.subtitleTextShadowColor;
  144. self.subtitleLabel.shadowOffset = self.item.subtitleTextShadowOffset.width == 0 && self.item.subtitleTextShadowOffset.height == 0 ? self.menu.subtitleTextShadowOffset : self.item.subtitleTextShadowOffset;
  145. self.subtitleLabel.textAlignment = (NSInteger)self.item.subtitleTextAlignment == -1 ? self.menu.subtitleTextAlignment : self.item.subtitleTextAlignment;
  146. self.item.customView.frame = CGRectMake(0, 0, self.titleLabel.frame.size.width, self.frame.size.height);
  147. }
  148. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  149. {
  150. self.backgroundView.backgroundColor = self.item.highlightedBackgroundColor == nil ? self.menu.highlightedBackgroundColor : self.item.highlightedBackgroundColor;
  151. self.separatorView.backgroundColor = self.item.highlightedSeparatorColor == nil ? self.menu.highlightedSeparatorColor : self.item.highlightedSeparatorColor;
  152. self.imageView.image = self.item.highlightedImage ? self.item.highlightedImage : self.item.image;
  153. if ([self.imageView respondsToSelector:@selector(setTintColor:)]) {
  154. self.imageView.tintColor = self.menu.highlightedImageTintColor;
  155. }
  156. self.titleLabel.textColor = self.item.highlightedTextColor == nil ? self.menu.highlightedTextColor : self.item.highlightedTextColor;
  157. self.titleLabel.shadowColor = self.item.highlightedTextShadowColor == nil ? self.menu.highlightedTextShadowColor : self.item.highlightedTextShadowColor;
  158. self.titleLabel.shadowOffset = self.item.highlightedTextShadowOffset.width == 0 && self.item.highlightedTextShadowOffset.height == 0 ? self.menu.highlightedTextShadowOffset : self.item.highlightedTextShadowOffset;
  159. self.subtitleLabel.textColor = self.item.subtitleHighlightedTextColor == nil ? self.menu.subtitleHighlightedTextColor : self.item.subtitleHighlightedTextColor;
  160. self.subtitleLabel.shadowColor = self.item.subtitleHighlightedTextShadowColor == nil ? self.menu.subtitleHighlightedTextShadowColor : self.item.subtitleHighlightedTextShadowColor;
  161. self.subtitleLabel.shadowOffset = self.item.subtitleHighlightedTextShadowOffset.width == 0 && self.item.subtitleHighlightedTextShadowOffset.height == 0 ? self.menu.subtitleHighlightedTextShadowOffset : self.item.subtitleHighlightedTextShadowOffset;
  162. }
  163. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
  164. {
  165. self.backgroundView.backgroundColor = self.item.backgroundColor == nil ? [UIColor clearColor] : self.item.backgroundColor;
  166. self.separatorView.backgroundColor = self.menu.separatorColor;
  167. self.imageView.image = self.item.image;
  168. if ([self.imageView respondsToSelector:@selector(setTintColor:)]) {
  169. self.imageView.tintColor = self.menu.imageTintColor;
  170. }
  171. self.titleLabel.textColor = self.item.textColor == nil ? self.menu.textColor : self.item.textColor;
  172. self.titleLabel.shadowColor = self.item.textShadowColor == nil ?self.menu.textShadowColor : self.item.textShadowColor;
  173. self.titleLabel.shadowOffset = self.item.textShadowOffset.width == 0 && self.item.textShadowOffset.height == 0 ? self.menu.textShadowOffset : self.item.textShadowOffset;
  174. self.subtitleLabel.textColor = self.item.subtitleTextColor == nil ? self.menu.subtitleTextColor : self.item.subtitleTextColor;
  175. self.subtitleLabel.shadowColor = self.item.subtitleTextShadowColor == nil ? self.menu.subtitleTextShadowColor : self.item.subtitleTextShadowColor;
  176. self.subtitleLabel.shadowOffset = self.item.subtitleTextShadowOffset.width == 0 && self.item.subtitleTextShadowOffset.height == 0 ? self.menu.subtitleTextShadowOffset : self.item.subtitleTextShadowOffset;
  177. }
  178. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  179. {
  180. self.backgroundView.backgroundColor = self.item.backgroundColor == nil ? [UIColor clearColor] : self.item.backgroundColor;
  181. self.separatorView.backgroundColor = self.item.separatorColor == nil ? self.menu.separatorColor : self.item.separatorColor;
  182. self.imageView.image = self.item.image;
  183. if ([self.imageView respondsToSelector:@selector(setTintColor:)]) {
  184. self.imageView.tintColor = self.menu.imageTintColor;
  185. }
  186. self.titleLabel.textColor = self.item.textColor == nil ? self.menu.textColor : self.item.textColor;
  187. self.titleLabel.shadowColor = self.item.textShadowColor == nil ? self.menu.textShadowColor : self.item.textShadowColor;
  188. self.titleLabel.shadowOffset = self.item.textShadowOffset.width == 0 && self.item.textShadowOffset.height ? self.menu.textShadowOffset : self.item.textShadowOffset;
  189. self.subtitleLabel.textColor = self.item.subtitleTextColor == nil ? self.menu.subtitleTextColor : self.item.subtitleTextColor;
  190. self.subtitleLabel.shadowColor = self.menu.subtitleTextShadowColor == nil ? self.menu.subtitleTextShadowColor : self.item.subtitleTextShadowColor;
  191. self.subtitleLabel.shadowOffset = self.item.subtitleTextShadowOffset.width == 0 && self.item.subtitleTextShadowOffset.height == 0 ? self.menu.subtitleTextShadowOffset : self.item.subtitleTextShadowOffset;
  192. CGPoint endedPoint = [touches.anyObject locationInView:self];
  193. if (endedPoint.y < 0 || endedPoint.y > CGRectGetHeight(self.bounds))
  194. return;
  195. if (!self.menu.closeOnSelection) {
  196. if (self.item.action)
  197. self.item.action(self.item);
  198. } else {
  199. if (self.item.action) {
  200. if (self.menu.waitUntilAnimationIsComplete) {
  201. __typeof (&*self) __weak weakSelf = self;
  202. [self.menu closeWithCompletion:^{
  203. weakSelf.item.action(weakSelf.item);
  204. }];
  205. } else {
  206. [self.menu close];
  207. self.item.action(self.item);
  208. }
  209. }
  210. }
  211. }
  212. @end