ICRQuestionHelperView.m 8.49 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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
//
//  ICRQuestionHelperView.m
//  XFFruit
//
//  Created by Xummer on 15/6/3.
//  Copyright (c) 2015年 Xummer. All rights reserved.
//

#import "ICRQuestionHelperView.h"
#import "ICRAttachmentView.h"
#import "LBorderView.h"
#import "IBTCustomButtom.h"

#import "ICRQuestionManager.h"

#define LEFT_MARGIN                 (15.0f)
#define HELPER_VIEW_MAX_HEIGHT      (330)
#define HELPER_VIEW_R_MAX_HEIGHT    (210)
#define HELPER_VIEW_MIN_HEIGHT      (150)
#define HELPER_ATTACH_HEIGHT        (60)

@interface ICRQuestionHelperView ()
{
    BOOL m_bIsResult;
}

@property (strong, nonatomic) IBTUILabel *m_titleLable;
@property (strong, nonatomic) UIButton *m_changeBtn;

@property (strong, nonatomic) LBorderView *m_attachBGView;

@property (strong, nonatomic) UIView *m_attachInputView;

@end

@implementation ICRQuestionHelperView

+ (UIView *)TextFWithLeftLabel:(NSString *)nsLeftLabel
                         textF:(UITextField * __autoreleasing *)textFPointer
{
    UIView *v = [[UIView alloc] initWithFrame:(CGRect){
        .origin.x = 0,
        .origin.y = 0,
        .size.width = 300,
        .size.height = 40
    }];
    v.backgroundColor = [UIColor clearColor];
    
    IBTUILabel *leftLabel = [[IBTUILabel alloc] init];
    leftLabel.font = [UIFont systemFontOfSize:16];
    leftLabel.textColor = [UIColor lightGrayColor];
    leftLabel.text = nsLeftLabel;
    [leftLabel sizeToFit];
    leftLabel.x = 0;
    
    UIView *labelContainer = [[UIView alloc] init];
    labelContainer.backgroundColor = [UIColor clearColor];
    labelContainer.frame = (CGRect){
        .origin.x = 0,
        .origin.y = 0,
        .size.width = leftLabel.width + 2 * 5,
        .size.height = leftLabel.height
    };
    [labelContainer addSubview:leftLabel];
    
    UITextField *txtF = [[UITextField alloc] initWithFrame:(CGRect){
        .origin.x = 0,
        .origin.y = 0,
        .size.width = v.width,
        .size.height = v.height
    }];
    txtF.backgroundColor = [UIColor clearColor];
    txtF.leftViewMode = UITextFieldViewModeAlways;
    txtF.leftView = labelContainer;
    [txtF autoresizingWithStrechFullSize];
    
    UIImageView *txtFBG = [[UIImageView alloc] initWithFrame:(CGRect){
        .origin.x = labelContainer.width,
        .origin.y = 0,
        .size.width = txtF.width - labelContainer.width,
        .size.height = txtF.height
    }];
    txtFBG.userInteractionEnabled = YES;
    txtFBG.image =
    [[UIImage imageNamed:@"LoginInputBG"] stretchableImageWithLeftCapWidth:10
                                                              topCapHeight:25];
    txtFBG.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin;
    [v addSubview:txtFBG];
    [v addSubview:txtF];

    
    if (textFPointer) {
        *textFPointer = txtF;
    }
    
    return v;
}

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

- (instancetype)initWithFrame:(CGRect)frame isResult:(BOOL)bIsResult {
    self = [super initWithFrame:frame];
    if (!self) {
        return nil;
    }
    
    m_bIsResult = bIsResult;
    
    [self initSubviews];
    
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat fH = 35;
    CGFloat fW = self.width - 2 * LEFT_MARGIN;
    
    _m_changeBtn.frame = (CGRect){
        .origin.x = self.width - LEFT_MARGIN - fH,
        .origin.y = 0,
        .size.width = fH,
        .size.height = fH
    };
    
    _m_titleLable.frame = (CGRect){
        .origin.x = LEFT_MARGIN,
        .origin.y = 0,
        .size.width = fW - _m_changeBtn.width,
        .size.height = fH
    };
    
    _m_nextBtn.frame = (CGRect){
        .origin.x = _m_titleLable.left,
        .origin.y = self.height - 65,
        .size.width = fW,
        .size.height = 42
    };
    
    _m_attachBGView.frame = (CGRect){
        .origin.x = _m_titleLable.left,
        .origin.y = _m_titleLable.bottom,
        .size.width = fW,
        .size.height = _m_nextBtn.top - 13 - _m_titleLable.bottom
    };
    
    CGFloat fGap = 5;
    _m_attachInputView.frame = (CGRect){
        .origin.x = fGap,
        .origin.y = 0,
        .size.width = _m_attachBGView.width - 2 * fGap,
        .size.height = 30
    };
    
    CGFloat fDy = _m_attachInputView.bottom;
    
    if (_m_photoAttachView) {
        _m_photoAttachView.frame = (CGRect){
            .origin.x = fGap,
            .origin.y = fDy,
            .size.width = _m_attachInputView.width,
            .size.height = HELPER_ATTACH_HEIGHT
        };
        
        fDy = _m_photoAttachView.bottom;
    }
    
    if (_m_taskAttachView) {
        _m_taskAttachView.frame = (CGRect){
            .origin.x = fGap,
            .origin.y = fDy,
            .size.width = _m_attachInputView.width,
            .size.height = HELPER_ATTACH_HEIGHT
        };
        
        fDy = _m_taskAttachView.bottom;
    }
    
    if (_m_voiceAttachView) {
        _m_voiceAttachView.frame = (CGRect){
            .origin.x = fGap,
            .origin.y = fDy,
            .size.width = _m_attachInputView.width,
            .size.height = HELPER_ATTACH_HEIGHT
        };
    }
}

#pragma mark - Private Method
- (void)initSubviews {
    
    self.backgroundColor = [UIColor whiteColor];
    
    ICRQuestionManager *mgr = [ICRQuestionManager sharedManager];
    
    self.m_titleLable = [[IBTUILabel alloc] init];
    _m_titleLable.font = [UIFont systemFontOfSize:14];
    _m_titleLable.textColor = ICR_TINTCOLOR;
    _m_titleLable.text = @"附加信息:";
    
    [self addSubview:_m_titleLable];
    
    self.m_changeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_m_changeBtn setImage:[UIImage imageNamed:@"AttachHide"] forState:UIControlStateNormal];
    [_m_changeBtn setImage:[UIImage imageNamed:@"AttachDisplay"] forState:UIControlStateSelected];
    [_m_changeBtn addTarget:self
                     action:@selector(onChangeBtnTapped:)
           forControlEvents:UIControlEventTouchUpInside];
    _m_changeBtn.selected = mgr.m_bIsHelpViewAttachHide;
    
    [self addSubview:_m_changeBtn];
    
    self.m_attachBGView = [[LBorderView alloc] init];
    _m_attachBGView.cornerRadius = IBT_DEFAULT_CORNER_RADIUS;
    _m_attachBGView.borderType = BorderTypeDashed;
    _m_attachBGView.borderWidth = .5f;
    _m_attachBGView.borderColor = [UIColor lightGrayColor];
    _m_attachBGView.dashPattern = 4;
    _m_attachBGView.spacePattern = 2;
    [self addSubview:_m_attachBGView];
    
    // attachViews
    UITextField *txtF = nil;
    self.m_attachInputView =
    [[self class] TextFWithLeftLabel:@"备注:" textF:&txtF];
    
    self.m_inputTxtF = txtF;
    
    [_m_attachBGView addSubview:_m_attachInputView];
    
    if (!m_bIsResult) {
        self.m_photoAttachView = [[ICRAttachmentView alloc] initWithType:kAttViewImage];
        _m_photoAttachView.m_uiMaxAttCount = 1;
        
        [_m_attachBGView addSubview:_m_photoAttachView];
        
        
        
        self.m_voiceAttachView = [[ICRAttachmentView alloc] initWithType:kAttViewVoice];
        _m_voiceAttachView.m_uiMaxAttCount = 1;
        
        [_m_attachBGView addSubview:_m_voiceAttachView];
    }
    
    self.m_taskAttachView = [[ICRAttachmentView alloc] initWithType:kAttViewTask];
    _m_taskAttachView.m_uiMaxAttCount = 1;
    
    [_m_attachBGView addSubview:_m_taskAttachView];
    
    
    self.m_nextBtn = [IBTCustomButtom buttonWithTitle:@"下一项" color:ICR_ORANGE_BTN_COLOR target:nil action:nil];
    [self addSubview:_m_nextBtn];
    
    [self updateViewRect:_m_changeBtn.selected];
    
}

- (void)updateViewRect:(BOOL)bHide {
    
    CGFloat fH = bHide ? HELPER_VIEW_MIN_HEIGHT : (m_bIsResult ? HELPER_VIEW_R_MAX_HEIGHT : HELPER_VIEW_MAX_HEIGHT) ;
    
    self.m_photoAttachView.hidden = bHide;
    self.m_taskAttachView.hidden = bHide;
    self.m_voiceAttachView.hidden = bHide;
    
    if (self.height == fH) {
        return;
    }
    else {
        self.frame = (CGRect){
            .origin.x = 0,
            .origin.y = self.bottom - fH,
            .size.width = self.width,
            .size.height = fH
        };
    }
    
}

#pragma mark - Actions
- (void)onChangeBtnTapped:(id)sender {
    [_m_inputTxtF resignFirstResponder];
    
    _m_changeBtn.selected = ! _m_changeBtn.selected;
    
    if (!m_bIsResult) {
        ICRQuestionManager *mgr = [ICRQuestionManager sharedManager];
        mgr.m_bIsHelpViewAttachHide = _m_changeBtn.selected;
    }
    
    [self updateViewRect:_m_changeBtn.selected];
}

@end