REMenuItem.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // REMenuItem.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 "REMenuItem.h"
  26. #import "REMenuItemView.h"
  27. @interface REMenuItem ()
  28. @property (assign, nonatomic) REMenuItemView *itemView;
  29. @end
  30. @implementation REMenuItem
  31. - (id)initWithTitle:(NSString *)title image:(UIImage *)image highlightedImage:(UIImage *)highlightedImage action:(void (^)(REMenuItem *item))action
  32. {
  33. self = [super init];
  34. if (self) {
  35. _title = title;
  36. _image = image;
  37. _highlightedImage = highlightedImage;
  38. _action = action;
  39. _textAlignment = -1;
  40. _subtitleTextAlignment = -1;
  41. }
  42. return self;
  43. }
  44. - (id)initWithTitle:(NSString *)title subtitle:(NSString *)subtitle image:(UIImage *)image highlightedImage:(UIImage *)highlightedImage action:(void (^)(REMenuItem *item))action
  45. {
  46. self = [super init];
  47. if (self) {
  48. _title = title;
  49. _subtitle = subtitle;
  50. _image = image;
  51. _highlightedImage = highlightedImage;
  52. _action = action;
  53. _textAlignment = -1;
  54. _subtitleTextAlignment = -1;
  55. }
  56. return self;
  57. }
  58. - (id)initWithCustomView:(UIView *)customView action:(void (^)(REMenuItem *item))action
  59. {
  60. self = [super init];
  61. if (self) {
  62. _customView = customView;
  63. _action = action;
  64. }
  65. return self;
  66. }
  67. - (id)initWithCustomView:(UIView *)customView
  68. {
  69. self = [super init];
  70. if (self) {
  71. _customView = customView;
  72. }
  73. return self;
  74. }
  75. - (NSString *)description
  76. {
  77. return [NSString stringWithFormat:@"<title: %@; subtitle: %@; tag: %li>", self.title, self.subtitle, (long)self.tag];
  78. }
  79. - (void)setTitle:(NSString *)title
  80. {
  81. _title = title;
  82. self.itemView.titleLabel.text = title;
  83. self.itemView.accessibilityLabel = title;
  84. }
  85. - (void)setSubtitle:(NSString *)subtitle
  86. {
  87. _subtitle = subtitle;
  88. self.itemView.subtitleLabel.text = subtitle;
  89. self.itemView.accessibilityLabel = [NSString stringWithFormat:@"%@, %@", self.itemView.titleLabel.text, subtitle];
  90. }
  91. - (void)setImage:(UIImage *)image
  92. {
  93. _image = image;
  94. self.itemView.imageView.image = image;
  95. }
  96. - (void)setHighlightedImage:(UIImage *)highlightedImage
  97. {
  98. _highlightedImage = highlightedImage;
  99. self.itemView.imageView.highlightedImage = highlightedImage;
  100. }
  101. - (void)setNeedsLayout
  102. {
  103. [self.itemView layoutSubviews];
  104. }
  105. @end