ICRAttachmentUnit.m 6.88 KB
Newer Older
mei's avatar
mei committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
//
//  ICRAttachmentUnit.m
//  XFFruit
//
//  Created by Xummer on 4/19/15.
//  Copyright (c) 2015 Xummer. All rights reserved.
//

#import "ICRAttachmentUnit.h"

#define IBT_ATTACH_IMAGE_CORNER_RADIUS          (4)
#define IBT_ATTACH_TOP_PADDING                  (10)
#define IBT_ATTACH_AUDIO_HEIGHT                 (30)

@interface ICRAttachmentUnit ()
{
    AttachmentUnitType m_eCurAttachType;
}
@property (strong, nonatomic) UIImageView *m_imageView;
@property (strong, nonatomic) UILabel *m_titleLabel;
@property (strong, nonatomic) UIImageView *m_iconMask;
@end

@implementation ICRAttachmentUnit

#pragma mark - Life Cycle
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (!self) {
        return nil;
    }
    
    [self initSubviews];
    
    return self;
}

- (void)didMoveToSuperview {
    [super didMoveToSuperview];
    
    if (self.superview) {
        [self bringSubviewToFront:_m_titleLabel];
        
        [self.superview addSubview:_m_closeButton];
    }
}

- (void)removeFromSuperview {
    [_m_closeButton removeFromSuperview];
    
    [super removeFromSuperview];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    _m_titleLabel.hidden = [_m_titleLabel.text length] == 0;
    
    if (_m_eCurFileType == kICRTypeAudio) {
        _m_imageView.frame = (CGRect){
            .origin.x = 0,
            .origin.y = IBT_ATTACH_TOP_PADDING,
            .size.width = IBT_ATTACH_UNIT_DEFAULT_WIDTH + 20,
            .size.height = IBT_ATTACH_AUDIO_HEIGHT
        };
        
        _m_titleLabel.frame = (CGRect){
            .origin.x = 0,
            .origin.y = 0,
            .size.width = IBT_ATTACH_UNIT_DEFAULT_WIDTH - 10,
            .size.height = IBT_ATTACH_AUDIO_HEIGHT
        };
        
        CGFloat h = 16;
        _m_iconMask.frame = (CGRect){
            .origin.x = CGRectGetMaxX(_m_titleLabel.frame),
            .origin.y = (CGRectGetHeight(_m_imageView.bounds) - h) * .5f,
            .size.width = h,
            .size.height = h
        };
        
        _m_iconMask.layer.cornerRadius = 0;
    }
    else {
        _m_imageView.frame = (CGRect){
            .origin.x = 0,
            .origin.y = IBT_ATTACH_TOP_PADDING,
            .size.width = IBT_ATTACH_UNIT_DEFAULT_WIDTH,
            .size.height = IBT_ATTACH_UNIT_DEFAULT_WIDTH
        };
        
        _m_iconMask.frame = _m_imageView.bounds;
        
        CGFloat h = IBT_ATTACH_UNIT_TITLE_HEIGHT - 10;
        CGFloat dy = 10;
        switch (_m_eCurDisplayType) {
            case kATTDisplayOutTitle:
            {
                dy = CGRectGetMaxY(_m_imageView.frame);
            }
                break;
            case kATTDisplayInnerTitle:
            {
                dy = CGRectGetHeight(_m_imageView.bounds) - h;
            }
                break;
            default:
                break;
        }
        
        _m_titleLabel.frame = (CGRect){
            .origin.x = 0,
            .origin.y = dy,
            .size.width = CGRectGetWidth(_m_imageView.bounds),
            .size.height = h
        };
        
        _m_iconMask.layer.cornerRadius = _m_imageView.layer.cornerRadius;
    }
    
    CGFloat w = 22;
    CGFloat delta = - (1 - 0.618f) * w;
    _m_closeButton.frame = (CGRect){
        .origin.x = CGRectGetMaxX(self.frame) - 0.618f * w,
        .origin.y = CGRectGetMinY(self.frame) + CGRectGetMinY(_m_imageView.frame) + delta ,
        .size.width = w,
        .size.height = w
    };
}

#pragma mark - Setter
- (void)setM_eCurDisplayType:(AttachmentDisplayType)eCurDisplayType {
    if (_m_eCurDisplayType == eCurDisplayType) {
        return;
    }
    
    _m_eCurDisplayType = eCurDisplayType;
    
    switch (_m_eCurDisplayType) {
        case kATTDisplayOutTitle:
        {
            self.m_titleLabel.backgroundColor = [UIColor clearColor];
            self.m_titleLabel.textColor = [UIColor grayColor];
            [self addSubview:_m_titleLabel];
        }
            break;
        case kATTDisplayInnerTitle:
        {
            if (_m_eCurFileType == kICRTypeAudio) {
                self.m_titleLabel.backgroundColor = [UIColor clearColor];
            }
            else {
                self.m_titleLabel.backgroundColor = [UIColor colorWithW:1 a:.8];
            }
            
            self.m_titleLabel.textColor = [UIColor whiteColor];
            [self.m_imageView addSubview:_m_titleLabel];
        }
            break;
        default:
            break;
    }
    
    [self setNeedsLayout];
}

#pragma mark - Public Method
- (void)updateWithType:(AttachmentUnitType)type
                masker:(UIImage *)masker
           placeHolder:(UIImage *)phImage
                 image:(id)image
                 title:(NSString *)title
{
    BOOL haveCloseBtn = NO;
    switch (type) {
        case kATTNormal:
        {
            haveCloseBtn = NO;
        }
            break;
        case kATTCloseBtn:
        {
            haveCloseBtn = YES;
        }
            break;
            
        default:
            break;
    }
    
    _m_closeButton.hidden = !haveCloseBtn;
    self.clipsToBounds = _m_closeButton.hidden;
    self.highLightWhenTapped = _m_iconMask.hidden;
    self.m_iconMask.image = masker;
    
    if ([image isKindOfClass:[UIImage class]]) {
        _m_imageView.image = image;
    }
    else if ([image isKindOfClass:[NSURL class]]) {
        [_m_imageView sd_setImageWithURL:image placeholderImage:phImage];
    }
    else {
        _m_imageView.image = phImage;
    }
    
    _m_titleLabel.text = title;
    
    switch (_m_eCurDisplayType) {
        case kATTDisplayInnerTitle:
        {
            if (_m_eCurFileType == kICRTypeAudio) {
                self.m_titleLabel.backgroundColor = [UIColor clearColor];
            }
            else {
                self.m_titleLabel.backgroundColor = [UIColor colorWithW:1 a:.8];
            }
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - Private Method
- (void)initSubviews {
    self.backgroundColor = [UIColor clearColor];
    self.highLightWhenTapped = YES;
    
    self.m_imageView = [[UIImageView alloc] init];
    _m_imageView.layer.cornerRadius = IBT_ATTACH_IMAGE_CORNER_RADIUS;
    _m_imageView.layer.masksToBounds = YES;
    [self addSubview:_m_imageView];
    
    self.m_iconMask = [[UIImageView alloc] init];
    _m_iconMask.layer.masksToBounds = YES;
    _m_iconMask.contentMode = UIViewContentModeCenter;
    [_m_imageView addSubview:_m_iconMask];
    
    self.m_titleLabel = [[UILabel alloc] init];
    _m_titleLabel.textColor = [UIColor grayColor];
    _m_titleLabel.textAlignment = NSTextAlignmentCenter;
    _m_titleLabel.font = [UIFont systemFontOfSize:12];
    [self addSubview:_m_titleLabel];
    
    self.m_closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_m_closeButton setImage:[UIImage imageNamed:@"attachment_delete_btn"]
                  forState:UIControlStateNormal];
    self.m_closeButton.hidden = YES;
}

@end