iOS开发物流信息

176 阅读3分钟
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

typedef enum : NSUInteger {
    WuliuCellPositionMid,
    WuliuCellPositionTop,
    WuliuCellPositionTail,
} WuliuCellPosition;

@interface WuliuModel : NSObject

///时间
@property (nonatomic, copy) NSString *time;
///物流
@property (nonatomic, copy) NSString *context;
///行高
@property (nonatomic, assign) CGFloat rowHeight;
///区分第一行还是最后一行,默认中间
@property (nonatomic, assign) WuliuCellPosition wuliuCellPosition;

@end

NS_ASSUME_NONNULL_END
///=================================================
#import "WuliuModel.h"

@implementation WuliuModel

- (void)setContext:(NSString *)context {
    _context = context;
        
    CGSize size = CGSizeMake([UIScreen mainScreen].bounds.size.width - 50, MAXFLOAT);
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:13],
    NSForegroundColorAttributeName: [UIColor blackColor]};
    _rowHeight = [context boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attributes context:nil].size.height + 31;
}

@end
#import <UIKit/UIKit.h>
#import "WuliuModel.h"

NS_ASSUME_NONNULL_BEGIN

@interface WuliuCell : UITableViewCell

@property (nonatomic, strong) WuliuModel *model;

@end

NS_ASSUME_NONNULL_END
///=================================================
#import "WuliuCell.h"
#import <Masonry/Masonry.h>
#import <YYText/YYText.h>

@interface WuliuCell ()

@property (nonatomic, strong) UIView *topLine;///上段线
@property (nonatomic, strong) UIView *bottomLine;///下段线
@property (nonatomic, strong) UIView *cirlePoint;///时间线上的小圆点
@property (nonatomic, strong) YYLabel *contextLabel;///物流
@property (nonatomic, strong) UILabel *timeLabel;///时间

@end

@implementation WuliuCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self.contentView addSubview:self.topLine];
        [self.contentView addSubview:self.bottomLine];
        [self.contentView addSubview:self.cirlePoint];
        [self.contentView addSubview:self.contextLabel];
        [self.contentView addSubview:self.timeLabel];
        [self p_addMasonry];
    }
    return self;
}

#pragma mark - # Private Methods
- (void)p_addMasonry {
    [self.topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(self.contentView.mas_centerY);
        make.left.mas_equalTo(20);
        make.width.mas_equalTo(1);
    }];
    
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView.mas_centerY);
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo(20);
        make.width.mas_equalTo(1);
    }];

    [self.cirlePoint mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.topLine.mas_centerX);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.width.height.mas_equalTo(6);
    }];
    
    [self.contextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.top.mas_equalTo(8);
        make.right.mas_equalTo(-10);
    }];

    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(40);
        make.top.mas_equalTo(self.contextLabel.mas_bottom);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(18);
    }];
}

#pragma mark - # Getter
- (UIView *)topLine {
    if (!_topLine) {
        _topLine = [[UIView alloc] init];
        _topLine.backgroundColor = [UIColor colorWithRed:231.0f/255.0f green:231.0f/255.0f blue:231.0f/255.0f alpha:1.0f];
    }
    return _topLine;
}

- (UIView *)bottomLine {
    if (!_bottomLine) {
        _bottomLine = [[UIView alloc] init];
        _bottomLine.backgroundColor = [UIColor colorWithRed:231.0f/255.0f green:231.0f/255.0f blue:231.0f/255.0f alpha:1.0f];
    }
    return _bottomLine;
}

- (UIView *)cirlePoint {
    if (!_cirlePoint) {
        _cirlePoint = [[UIView alloc] init];
        _cirlePoint.backgroundColor = [UIColor colorWithRed:231.0f/255.0f green:231.0f/255.0f blue:231.0f/255.0f alpha:1.0f];
        _cirlePoint.layer.cornerRadius = 3;
        _cirlePoint.layer.masksToBounds = YES;
    }
    return _cirlePoint;
}

- (YYLabel *)contextLabel {
    if (!_contextLabel) {
        _contextLabel = [[YYLabel alloc] init];
        _contextLabel.font = [UIFont systemFontOfSize:13];
        _contextLabel.numberOfLines = 0;
    }
    return _contextLabel;
}

- (UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.font = [UIFont systemFontOfSize:11];
        _timeLabel.textColor = [UIColor colorWithRed:153.0f/255.0f green:153.0f/255.0f blue:153.0f/255.0f alpha:1];
    }
    return _timeLabel;
}

- (void)setModel:(WuliuModel *)model {
    _model = model;
    
    self.timeLabel.text = model.time;
    UIColor *contextColor;
    if (model.wuliuCellPosition == WuliuCellPositionTop) {
        contextColor = [UIColor colorWithRed:60.0f/255.0f green:60.0f/255.0f blue:60.0f/255.0f alpha:1];
    }else {
        contextColor = [UIColor colorWithRed:153.0f/255.0f green:153.0f/255.0f blue:153.0f/255.0f alpha:1];
    }
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:model.context attributes:@{NSForegroundColorAttributeName:contextColor}];
    NSArray * phoneArray = [self getPhoneNumbersFromString:model.context];
    for (NSString *phoneStr in phoneArray) {
        [text addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:251.0f/255.0f green:162.0f/255.0f blue:44.0f/255.0f alpha:1.0f] range:[model.context rangeOfString:phoneStr]];
        YYTextHighlight *highlight = [YYTextHighlight new];
        highlight.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
            NSString *str=[[NSString alloc]initWithFormat:@"tel:%@",phoneStr];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
        };
        [text yy_setTextHighlight:highlight range:[model.context rangeOfString:phoneStr]];
    }
    self.contextLabel.attributedText = text;
    [self.contextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(model.rowHeight-26);
    }];
    
    if (model.wuliuCellPosition == WuliuCellPositionTop) {
        self.topLine.hidden = YES;
        self.bottomLine.hidden = NO;
    }else if (model.wuliuCellPosition == WuliuCellPositionTail){
        self.topLine.hidden = NO;
        self.bottomLine.hidden = YES;
    }else {
        self.topLine.hidden = NO;
        self.bottomLine.hidden = NO;
    }
}

- (NSArray *) getPhoneNumbersFromString:(NSString *)str {
    NSError* error = nil;
    NSString* regulaStr = @"(([0-9]{11})|((400|800)([0-9\\-]{7,10})|(([0-9]{4}|[0-9]{3})(-| )?)?([0-9]{7,8})((-| |转)*([0-9]{1,4}))?))";
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regulaStr
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];
    NSArray *arrayOfAllMatches = [regex matchesInString:str options:0 range:NSMakeRange(0, [str length])];
    NSMutableArray* numbers = [NSMutableArray arrayWithCapacity:arrayOfAllMatches.count];
    for (NSTextCheckingResult *match in arrayOfAllMatches){
        NSString* substringForMatch = [str substringWithRange:match.range];
        [numbers addObject:substringForMatch];
    }
    return numbers;
}

@end
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end
///=================================================
#import "ViewController.h"
#import "WuliuModel.h"
#import "WuliuCell.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation ViewController

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [[NSMutableArray alloc] init];
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
    [self.view addSubview:self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WuliuCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WuliuCell class])];
    WuliuModel *model = self.dataSource[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 0) {
        model.wuliuCellPosition = WuliuCellPositionTop;
    }else if (indexPath.row == self.dataSource.count - 1){
        model.wuliuCellPosition = WuliuCellPositionTail;
    }else {
        model.wuliuCellPosition = WuliuCellPositionMid;
    }
    cell.model = model;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    WuliuModel *model = self.dataSource[indexPath.row];
    return model.rowHeight;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 0;
        _tableView.sectionHeaderHeight = 0.0;
        _tableView.sectionFooterHeight = 0.0;
        _tableView.estimatedSectionHeaderHeight = 0.0;
        _tableView.estimatedSectionFooterHeight = 0.0;
        [_tableView registerClass:[WuliuCell class] forCellReuseIdentifier:NSStringFromClass([WuliuCell class])];
    }
    return _tableView;
}

- (void)loadData {
    NSArray *dataArray = @[@{@"time": @"2018-07-31 12:49:46",@"context": @"吉安市|吉安市【安福县】,洋溪百世13970646811 已签收"},
                           @{@"time": @"2018-07-31 12:30:14",@"context": @"吉安市|吉安市【安福县】,【李铁洪/13970646811】正在派件13000001234"},
                           @{@"time": @"2018-07-30 17:26:58",@"context": @"吉安市|吉安市【吉安集散仓】,正发往【安福县】"},
                           @{@"time": @"2018-07-30 17:01:50",@"context": @"吉安市|到吉安市【吉安集散仓】"},
                           @{@"time": @"2018-07-30 11:26:46",@"context": @"南昌市|南昌市【南昌转运中心】,正发往【吉安集散仓】 咱的电话是0796-1344459"},
                           @{@"time": @"2018-07-30 09:26:22",@"context": @"南昌市|到南昌市【南昌转运中心】"},
                           @{@"time": @"2018-07-30 02:53:12",@"context": @"武汉市|武汉市【武汉转运中心】,正发往【南昌转运中心】电话13479681234",},
                           @{@"time": @"2018-07-30 00:59:26",@"context": @"武汉市|到武汉市【武汉转运中心】"},
                           @{@"time": @"2018-07-29 21:19:22",@"context": @"荆州市|荆州市【荆州集散中心】,正发往【武汉转运中心】"},
                           @{@"time": @"2018-07-29 19:10:42",@"context": @"荆州市|到荆州市【荆州集散中心】"},
                           @{@"time": @"2018-07-29 15:43:46",@"context": @"荆州市|荆州市【沙市区八部】,【朱军子/0716-4176402】已揽收"}];
    for (NSInteger i = 0; i < dataArray.count; i++) {
        WuliuModel *model = [[WuliuModel alloc] init];
        model.time = dataArray[i][@"time"];
        model.context = dataArray[i][@"context"];
        [self.dataSource addObject:model];
    }
}

@end