| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- //
- // IGLDropDownItem.m
- // IGLDropDownMenuDemo
- //
- // Created by Galvin Li on 8/30/14.
- // Copyright (c) 2014 Galvin Li. All rights reserved.
- //
- #import "IGLDropDownItem.h"
- @interface IGLDropDownItem ()
- @property (nonatomic, strong) UIImageView *iconImageView;
- @property (nonatomic, strong) UIView *bgView;
- @property (nonatomic, strong) UILabel *textLabel;
- @end
- @implementation IGLDropDownItem
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- [self commonInit];
- }
- return self;
- }
- - (instancetype)initWithCoder:(NSCoder *)aDecoder
- {
- self = [super initWithCoder:aDecoder];
- if (self) {
- [self commonInit];
- }
- return self;
- }
- - (void)commonInit
- {
- _paddingLeft = 5;
- [self initView];
- }
- - (void)setFrame:(CGRect)frame
- {
- [super setFrame:frame];
-
- [self.bgView setFrame:self.bounds];
-
- [self updateLayout];
- }
- - (void)initView
- {
- self.bgView = [[UIView alloc] init];
- self.bgView.userInteractionEnabled = NO;
- self.bgView.backgroundColor = [UIColor whiteColor];
- self.bgView.layer.shadowColor = [UIColor grayColor].CGColor;
- self.bgView.layer.shadowOffset = CGSizeMake(0, 0);
- self.bgView.layer.shadowOpacity = 0.2;
- self.bgView.layer.shouldRasterize = YES;
- [self.bgView setFrame:self.bounds];
- [self addSubview:self.bgView];
-
- self.iconImageView = [[UIImageView alloc] init];
- self.iconImageView.contentMode = UIViewContentModeCenter;
- [self addSubview:self.iconImageView];
-
- self.textLabel = [[UILabel alloc] init];
- [_textLabel setFont:[UIFont fontWithName:@"BauhausITC" size:17.0]];
- self.textLabel.numberOfLines = 1;
- self.textLabel.textColor = [UIColor colorWithRed:((float)38/255) green:(float)82/255 blue:((float)67/255) alpha:1];
- [self addSubview:self.textLabel];
-
- [self updateLayout];
-
- }
- - (void)setIconImage:(UIImage *)iconImage
- {
- _iconImage = iconImage;
- [self.iconImageView setImage:self.iconImage];
-
- [self updateLayout];
- }
- - (void)updateLayout
- {
-
- CGFloat selfWidth = CGRectGetWidth(self.bounds);
- CGFloat selfHeight = CGRectGetHeight(self.bounds);
-
- [self.iconImageView setFrame:CGRectMake(self.paddingLeft, 0, selfHeight, selfHeight)];
- if (self.iconImage) {
- [self.textLabel setFrame:CGRectMake(CGRectGetMaxX(self.iconImageView.frame), 0, selfWidth - CGRectGetMaxX(self.iconImageView.frame), selfHeight)];
- } else {
- [self.textLabel setFrame:CGRectMake(self.paddingLeft, 0, selfWidth-30, selfHeight)];
- }
- }
- - (void)setPaddingLeft:(CGFloat)paddingLeft
- {
- _paddingLeft = paddingLeft;
-
- [self updateLayout];
- }
- - (void)setObject:(id)object
- {
- _object = object;
- }
- - (void)setText:(NSString *)text
- {
- _text = text;
- self.textLabel.text = self.text;
- }
- - (id)copyWithZone:(NSZone *)zone
- {
- IGLDropDownItem *itemCopy = [[IGLDropDownItem alloc] init];
-
- itemCopy.index = _index;
- itemCopy.iconImage = _iconImage;
- itemCopy.object = _object;
- itemCopy.text = _text;
- itemCopy.paddingLeft = _paddingLeft;
-
- return itemCopy;
- }
- @end
|