ICRPatrolPlanCellContentView.m 8.78 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
//
//  ICRPatrolPlanCellContentView.m
//  XFFruit
//
//  Created by Lili Wang on 15/5/11.
//  Copyright (c) 2015年 Xummer. All rights reserved.
//

#import "ICRPatrolPlanCellContentView.h"
#import "ICRPatrolPlan.h"

#define PATROL_PLAN_CONTENT_LABEL_TITLE_COLOR        [UIColor blackColor]
#define PATROL_PLAN_CONTENT_LABEL_CONTENT_COLOR      [UIColor grayColor]

#define PATROL_PLAN_CONTENT_LABEL_VERTICAL_MARGIN    (10.0f)
#define PATROL_PLAN_CONTENT_LABEL_HORIZON_MARGIN     (15.0f)
#define PATROL_PLAN_CONTENT_LABEL_INNER_GAP          (5.0f)
#define PATROL_PLAN_CONTENT_LABEL_HEIGHT             (16)

@interface ICRPatrolPlanCellContentView ()

@property (strong, nonatomic) UILabel *m_titleLabel;
@property (strong, nonatomic) UILabel *m_typeLabel;
@property (strong, nonatomic) UILabel *m_typeValueLabel;
@property (strong, nonatomic) UILabel *m_startTimeLabel;
@property (strong, nonatomic) UILabel *m_startTimeValueLabel;
@property (strong, nonatomic) UILabel *m_endTimeLabel;
@property (strong, nonatomic) UILabel *m_endTimeValueLabel;
@property (strong, nonatomic) UILabel *m_totalStoresNumLabel;
@property (strong, nonatomic) UILabel *m_surplusStoresNumLabel;

@end

@implementation ICRPatrolPlanCellContentView

#pragma mark - Life Cycle

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // code
        [self _init];
    }
    return self;
}

- (void)layoutSubviews {
    
    CGFloat MaxW = self.width - PATROL_PLAN_CONTENT_LABEL_HORIZON_MARGIN;
    
    CGSize size = CGSizeMake(MaxW - PATROL_PLAN_CONTENT_LABEL_HORIZON_MARGIN, PATROL_PLAN_CONTENT_LABEL_HEIGHT);
    size = [_m_titleLabel widthLimitedSizeThatFits:size];
    _m_titleLabel.frame = ((CGRect){
        .origin.x = PATROL_PLAN_CONTENT_LABEL_HORIZON_MARGIN,
        .origin.y = PATROL_PLAN_CONTENT_LABEL_VERTICAL_MARGIN,
        .size = size
    });
    
    size = CGSizeMake(MaxW - PATROL_PLAN_CONTENT_LABEL_HORIZON_MARGIN, PATROL_PLAN_CONTENT_LABEL_HEIGHT);
    size = [_m_typeLabel widthLimitedSizeThatFits:size];
    _m_typeLabel.frame = ((CGRect){
        .origin.x = _m_titleLabel.left,
        .origin.y = _m_titleLabel.bottom + PATROL_PLAN_CONTENT_LABEL_INNER_GAP,
        .size = size
    });
    
    size = CGSizeMake(MaxW - _m_typeLabel.right, PATROL_PLAN_CONTENT_LABEL_HEIGHT);
    size = [_m_typeValueLabel widthLimitedSizeThatFits:size];
    _m_typeValueLabel.frame = ((CGRect){
        .origin.x = _m_typeLabel.right,
        .origin.y = _m_typeLabel.top,
        .size = size
    });
    
    size = CGSizeMake(MaxW - PATROL_PLAN_CONTENT_LABEL_HORIZON_MARGIN, PATROL_PLAN_CONTENT_LABEL_HEIGHT);
    size = [_m_startTimeLabel widthLimitedSizeThatFits:size];
    _m_startTimeLabel.frame = ((CGRect){
        .origin.x = _m_typeLabel.left,
        .origin.y = _m_typeValueLabel.bottom + PATROL_PLAN_CONTENT_LABEL_INNER_GAP,
        .size = size
    });
    
    size = CGSizeMake(MaxW - _m_startTimeLabel.right, PATROL_PLAN_CONTENT_LABEL_HEIGHT);
    size = [_m_startTimeValueLabel widthLimitedSizeThatFits:size];
    _m_startTimeValueLabel.frame = ((CGRect){
        .origin.x = _m_startTimeLabel.right,
        .origin.y = _m_startTimeLabel.top,
        .size = size
    });
    
    size = CGSizeMake(MaxW - PATROL_PLAN_CONTENT_LABEL_HORIZON_MARGIN, PATROL_PLAN_CONTENT_LABEL_HEIGHT);
    size = [_m_endTimeLabel widthLimitedSizeThatFits:size];
    _m_endTimeLabel.frame = ((CGRect){
        .origin.x = _m_startTimeLabel.left,
        .origin.y = _m_startTimeLabel.bottom + PATROL_PLAN_CONTENT_LABEL_INNER_GAP,
        .size = size
    });
    
    size = CGSizeMake(MaxW - _m_endTimeLabel.right, PATROL_PLAN_CONTENT_LABEL_HEIGHT);
    size = [_m_endTimeValueLabel widthLimitedSizeThatFits:size];
    _m_endTimeValueLabel.frame = ((CGRect){
        .origin.x = _m_endTimeLabel.right,
        .origin.y = _m_endTimeLabel.top,
        .size = size
    });
    
    if (_m_bHiddenNumLabel) {
        
        size = CGSizeMake((MaxW - PATROL_PLAN_CONTENT_LABEL_HORIZON_MARGIN ) * 0.5, PATROL_PLAN_CONTENT_LABEL_HEIGHT);
        size = [_m_totalStoresNumLabel widthLimitedSizeThatFits:size];
        _m_totalStoresNumLabel.frame = ((CGRect){
            .origin.x = _m_endTimeLabel.left,
            .origin.y = _m_endTimeLabel.bottom + PATROL_PLAN_CONTENT_LABEL_INNER_GAP,
            .size = size
        });
        
        size = CGSizeMake((MaxW - PATROL_PLAN_CONTENT_LABEL_HORIZON_MARGIN ) * 0.5, PATROL_PLAN_CONTENT_LABEL_HEIGHT);
        size = [_m_surplusStoresNumLabel widthLimitedSizeThatFits:size];
        _m_surplusStoresNumLabel.frame = ((CGRect){
            .origin.x = self.width * 0.5,
            .origin.y = _m_totalStoresNumLabel.top,
            .size = size
        });
    }
    
    
    //self.height = _m_endTimeValueLabel.bottom + PATROL_PLAN_CONTENT_LABEL_VERTICAL_MARGIN;
}

#pragma mark - Private Method

- (void)_init {
    
    self.m_titleLabel = [[self class] customLabelWithTextColor:PATROL_PLAN_CONTENT_LABEL_TITLE_COLOR];
    [self addSubview:_m_titleLabel];
    
    self.m_typeLabel = [[self class] customLabelWithTextColor:PATROL_PLAN_CONTENT_LABEL_TITLE_COLOR];
    _m_typeLabel.text = [IBTCommon localizableString:@"Type:"];
    [self addSubview:_m_typeLabel];
    
    self.m_typeValueLabel = [[self class] customLabelWithTextColor:PATROL_PLAN_CONTENT_LABEL_CONTENT_COLOR];
    [self addSubview:_m_typeValueLabel];
    
    self.m_startTimeLabel = [[self class] customLabelWithTextColor:PATROL_PLAN_CONTENT_LABEL_TITLE_COLOR];
    _m_startTimeLabel.text = [IBTCommon localizableString:@"Start Time:"];
    [self addSubview:_m_startTimeLabel];
    
    self.m_startTimeValueLabel = [[self class] customLabelWithTextColor:PATROL_PLAN_CONTENT_LABEL_CONTENT_COLOR];
    [self addSubview:_m_startTimeValueLabel];
    
    self.m_endTimeLabel = [[self class] customLabelWithTextColor:PATROL_PLAN_CONTENT_LABEL_TITLE_COLOR];
    _m_endTimeLabel.text = [IBTCommon localizableString:@"End Time:"];
    [self addSubview:_m_endTimeLabel];
    
    self.m_endTimeValueLabel = [[self class] customLabelWithTextColor:PATROL_PLAN_CONTENT_LABEL_CONTENT_COLOR];
    [self addSubview:_m_endTimeValueLabel];
    
    self.m_totalStoresNumLabel = [[self class] customLabelWithTextColor:PATROL_PLAN_CONTENT_LABEL_CONTENT_COLOR];
    [self addSubview:_m_totalStoresNumLabel];
    
    self.m_surplusStoresNumLabel = [[self class] customLabelWithTextColor:PATROL_PLAN_CONTENT_LABEL_CONTENT_COLOR];
    [self addSubview:_m_surplusStoresNumLabel];
}

#pragma mark - Class Method

+ (IBTUILabel *)customLabelWithTextColor:(UIColor *)textColor {
    
    IBTUILabel *label = [[IBTUILabel alloc] init];
    label.textAlignment = NSTextAlignmentLeft;
    label.textColor = textColor;
    label.font = [UIFont systemFontOfSize:15];
    
    return label;
}

@end

@implementation ICRPatrolPlanCellContentView (Configure)

- (void)updateValueLabelWithData:(id)data {
    
    if ([data isKindOfClass:[ICRPatrolPlan class]]) {
        
        ICRPatrolPlan *patrol = data;
        
        _m_titleLabel.text = patrol.name;
        _m_typeValueLabel.text = patrol.category;
        _m_startTimeValueLabel.text = patrol.beginDate ;
        _m_endTimeValueLabel.text = patrol.endDate  ;
        
        
        if (_m_bHiddenNumLabel) {
            
            NSUInteger uiTotal = patrol.inspectors.count;
            NSUInteger uiSuplus = 0;
            
            if( patrol.inspectors.count > 0 ) {
                for (NSDictionary *inspector in patrol.inspectors) {
                    if ([inspector[ @"state" ] integerValue] == 0) {
                        uiSuplus ++;
                    }
                }
            }
            
            NSString *nsHLText = [NSString stringWithFormat:@"%@",@( uiSuplus )];
        
            NSString *nsText = [NSString stringWithFormat:@"剩余门店:%@",nsHLText];
        
            NSDictionary *attrs = @{
                                    NSForegroundColorAttributeName : _m_surplusStoresNumLabel.textColor,
                                    NSFontAttributeName : _m_surplusStoresNumLabel.font
                                    };
            
            NSMutableAttributedString *attributedText =
            [[NSMutableAttributedString alloc] initWithString:nsText
                                                   attributes:attrs];
        
            NSRange highlightTextRange = [nsText rangeOfString:nsHLText];// * Notice that usage of rangeOfString in this case may cause some bugs - I use it here only for demonstration
            [attributedText setAttributes:@{ NSForegroundColorAttributeName : [UIColor redColor] }
                                    range:highlightTextRange];

            
            _m_totalStoresNumLabel.text = [NSString stringWithFormat:@"总门店数:%@",@( uiTotal )];
            _m_surplusStoresNumLabel.attributedText = attributedText;
        
        }
        
    }
    
    [self setNeedsLayout];

}

@end