IBTTextFieldCell.m 2.42 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
//
//  IBTTextFieldCell.m
//  XFFruit
//
//  Created by Lili Wang on 15/4/3.
//  Copyright (c) 2015年 Xummer. All rights reserved.
//

#import "IBTTextFieldCell.h"

@implementation IBTTextFieldCell

#pragma mark - Life Cycle
- (id)initWithStyle:(UITableViewCellStyle)style
    reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self _init];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGRect textFieldFrame = CGRectZero;
    if ([self.textLabel.text length] > 0) {
        self.textLabel.frame = ({
            CGRect frame = self.textLabel.frame;
            frame.size.width = MIN(MAX([self.textLabel sizeThatFits:CGSizeZero].width, JTLeftMinLabelWidth), JTLeftMinLabelWidth);
            frame;
        });
        
        CGFloat textFieldX = CGRectGetMaxX(self.textLabel.frame) + 5;
        textFieldFrame = (CGRect){
            .origin.x = textFieldX,
            .origin.y = 0,
            .size.width = CGRectGetWidth(_textField.superview.bounds) - IBT_GROUP_CELL_LEFT_PADDING - textFieldX,
            .size.height = CGRectGetHeight(_textField.superview.bounds)
        };
    }
    else {
        textFieldFrame = (CGRect){
            .origin.x = IBT_GROUP_CELL_LEFT_PADDING,
            .origin.y = 0,
            .size.width = CGRectGetWidth(_textField.superview.bounds) - IBT_GROUP_CELL_LEFT_PADDING * 2,
            .size.height = CGRectGetHeight(_textField.superview.bounds)
        };
    }
    
    _textField.frame = textFieldFrame;
}

- (void)updateTextIsIllegal:(BOOL)textIsIllegal {
    _textIsIllegal = textIsIllegal;
}

#pragma mark - Setter
- (void)setTextIsIllegal:(BOOL)textIsIllegal {
    _textIsIllegal = textIsIllegal;
    
    if (self.textLabel) {
        self.textLabel.textColor = _textIsIllegal ? [UIColor redColor] : [UIColor blackColor];
        self.textField.textColor = self.textLabel.textColor;
    }
    else {
        self.textField.textColor = _textIsIllegal ? [UIColor redColor] : [UIColor blackColor];
    }
}

#pragma mark - Private Method
- (void)_init {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.textField = [[IBTUITextField alloc] init];
    [self.contentView addSubview:_textField];
    
    [self.contentView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self.textField action:NSSelectorFromString(@"becomeFirstResponder")]];
}

@end