IGLDropDownItem.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // IGLDropDownItem.m
  3. // IGLDropDownMenuDemo
  4. //
  5. // Created by Galvin Li on 8/30/14.
  6. // Copyright (c) 2014 Galvin Li. All rights reserved.
  7. //
  8. #import "IGLDropDownItem.h"
  9. @interface IGLDropDownItem ()
  10. @property (nonatomic, strong) UIImageView *iconImageView;
  11. @property (nonatomic, strong) UIView *bgView;
  12. @property (nonatomic, strong) UILabel *textLabel;
  13. @end
  14. @implementation IGLDropDownItem
  15. - (id)initWithFrame:(CGRect)frame
  16. {
  17. self = [super initWithFrame:frame];
  18. if (self) {
  19. [self commonInit];
  20. }
  21. return self;
  22. }
  23. - (instancetype)initWithCoder:(NSCoder *)aDecoder
  24. {
  25. self = [super initWithCoder:aDecoder];
  26. if (self) {
  27. [self commonInit];
  28. }
  29. return self;
  30. }
  31. - (void)commonInit
  32. {
  33. _paddingLeft = 5;
  34. [self initView];
  35. }
  36. - (void)setFrame:(CGRect)frame
  37. {
  38. [super setFrame:frame];
  39. [self.bgView setFrame:self.bounds];
  40. [self updateLayout];
  41. }
  42. - (void)initView
  43. {
  44. self.bgView = [[UIView alloc] init];
  45. self.bgView.userInteractionEnabled = NO;
  46. self.bgView.backgroundColor = [UIColor whiteColor];
  47. self.bgView.layer.shadowColor = [UIColor grayColor].CGColor;
  48. self.bgView.layer.shadowOffset = CGSizeMake(0, 0);
  49. self.bgView.layer.shadowOpacity = 0.2;
  50. self.bgView.layer.shouldRasterize = YES;
  51. [self.bgView setFrame:self.bounds];
  52. [self addSubview:self.bgView];
  53. self.iconImageView = [[UIImageView alloc] init];
  54. self.iconImageView.contentMode = UIViewContentModeCenter;
  55. [self addSubview:self.iconImageView];
  56. self.textLabel = [[UILabel alloc] init];
  57. [_textLabel setFont:[UIFont fontWithName:@"BauhausITC" size:17.0]];
  58. self.textLabel.numberOfLines = 1;
  59. self.textLabel.textColor = [UIColor colorWithRed:((float)38/255) green:(float)82/255 blue:((float)67/255) alpha:1];
  60. [self addSubview:self.textLabel];
  61. [self updateLayout];
  62. }
  63. - (void)setIconImage:(UIImage *)iconImage
  64. {
  65. _iconImage = iconImage;
  66. [self.iconImageView setImage:self.iconImage];
  67. [self updateLayout];
  68. }
  69. - (void)updateLayout
  70. {
  71. CGFloat selfWidth = CGRectGetWidth(self.bounds);
  72. CGFloat selfHeight = CGRectGetHeight(self.bounds);
  73. [self.iconImageView setFrame:CGRectMake(self.paddingLeft, 0, selfHeight, selfHeight)];
  74. if (self.iconImage) {
  75. [self.textLabel setFrame:CGRectMake(CGRectGetMaxX(self.iconImageView.frame), 0, selfWidth - CGRectGetMaxX(self.iconImageView.frame), selfHeight)];
  76. } else {
  77. [self.textLabel setFrame:CGRectMake(self.paddingLeft, 0, selfWidth-30, selfHeight)];
  78. }
  79. }
  80. - (void)setPaddingLeft:(CGFloat)paddingLeft
  81. {
  82. _paddingLeft = paddingLeft;
  83. [self updateLayout];
  84. }
  85. - (void)setObject:(id)object
  86. {
  87. _object = object;
  88. }
  89. - (void)setText:(NSString *)text
  90. {
  91. _text = text;
  92. self.textLabel.text = self.text;
  93. }
  94. - (id)copyWithZone:(NSZone *)zone
  95. {
  96. IGLDropDownItem *itemCopy = [[IGLDropDownItem alloc] init];
  97. itemCopy.index = _index;
  98. itemCopy.iconImage = _iconImage;
  99. itemCopy.object = _object;
  100. itemCopy.text = _text;
  101. itemCopy.paddingLeft = _paddingLeft;
  102. return itemCopy;
  103. }
  104. @end