1. 程式人生 > >ios 物流時間軸,自動匹配電話號碼,可點選撥打

ios 物流時間軸,自動匹配電話號碼,可點選撥打

QQ20180312-100810.gif

1.png

本demo使用MJRefresh來做上拉重新整理,MJExtension來做模型解析,YYit做富文字點選事件,話不多說上程式碼

///控制器內陣列新增模型

 NSInteger totalCount = array.count;

    //清空陣列
    [self.logisticArray removeAllObjects];

    for (NSInteger i = 0; i < totalCount; i++) {

        CZHLogisticFrameModel *frameModel = [[CZHLogisticFrameModel alloc] init];

        CZHLogisticModel *model = [CZHLogisticModel czh_parse:array[i]]
; model.indexCount = i; model.totalCount = totalCount; frameModel.model = model; [self.logisticArray addObject:frameModel]; } [self.tableView reloadData];
frameModel實現,計算各個空間的frame以及各控制元件賦值判斷
- (void)setModel:(CZHLogisticModel *)model {
    _model = model;

    _contentString = model.content
; _timeString = model.time; ///判斷顏色 if (model.indexCount == 0) { _contentColor = CZHColor(0xff0000); _timeColor = CZHColor(0xff0000); _leftRoundColor = CZHColor(0xff0000); } else { _contentColor = CZHColor(0x999999); _timeColor = CZHColor(0x999999); _leftRoundColor = CZHColor(0xdfdfdf
); } _contentAttributedString = [[NSMutableAttributedString alloc] initWithString:_contentString]; _contentAttributedString.yy_font = CZHGlobelNormalFont(14); _contentAttributedString.yy_color = _contentColor; _contentAttributedString.yy_lineSpacing = 10; ///匹配電話號碼 NSString *string = _contentString; NSError *error = nil; NSDataDetector * detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber error:&error]; __block NSUInteger count = 0; __block NSString *phoneNumber; [detector enumerateMatchesInString:string options:0 range:NSMakeRange(0, [string length]) usingBlock:^(NSTextCheckingResult * _Nullable match, NSMatchingFlags flags, BOOL * _Nonnull stop) { if (count == 0) *stop = YES; if ([match resultType] == NSTextCheckingTypePhoneNumber) { phoneNumber = [match phoneNumber]; // NSLog(@"phoneNumber:%@", phoneNumber); } }]; if (phoneNumber.length > 0) { [_contentAttributedString yy_setTextHighlightRange:[_contentString rangeOfString:phoneNumber] color:[UIColor blueColor] backgroundColor:nil tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){ NSLog(@"----撥打電話--%@", phoneNumber); NSMutableString *str=[[NSMutableString alloc]initWithFormat:@"tel:%@",phoneNumber]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]]; }]; } ///計算控制元件frame CGSize maxSize = CGSizeMake(ScreenWidth - CZH_ScaleWidth(60), MAXFLOAT); CGSize contentLabelSize = [YYTextLayout layoutWithContainerSize:maxSize text:_contentAttributedString].textBoundingSize; CGFloat contentLabelX = MARGIN_LEFT; CGFloat contentLabelY = CZH_ScaleWidth(15); CGFloat contentLabelW = contentLabelSize.width; CGFloat contentLabelH = contentLabelSize.height; _contentLabelF = CGRectMake(contentLabelX, contentLabelY, contentLabelW, contentLabelH); CGSize timeLabelSize = [_timeString czh_sizeWithFont:CZHGlobelNormalFont(11) maxW:MAX_WIDTH]; CGFloat timeLabelX = MARGIN_LEFT; CGFloat timeLabelY = CGRectGetMaxY(_contentLabelF) + CZH_ScaleWidth(10); CGFloat timeLabelW = timeLabelSize.width; CGFloat timeLabelH = timeLabelSize.height; _timeLabelF = CGRectMake(timeLabelX, timeLabelY, timeLabelW, timeLabelH); CGFloat leftRoundViewX = 0; CGFloat leftRoundViewY = 0; CGFloat leftRoundViewW = 0; CGFloat leftRoundViewH = 0; if (model.indexCount == 0) { leftRoundViewY = CZH_ScaleWidth(15); leftRoundViewW = CZH_ScaleWidth(15); leftRoundViewH = CZH_ScaleWidth(15); } else { leftRoundViewY = CZH_ScaleWidth(19); leftRoundViewW = CZH_ScaleWidth(7); leftRoundViewH = CZH_ScaleWidth(7); } leftRoundViewX = (MARGIN_LEFT - leftRoundViewW) * 0.5; _leftRoundViewF = CGRectMake(leftRoundViewX, leftRoundViewY, leftRoundViewW, leftRoundViewH); _cellHeight = CGRectGetMaxY(_timeLabelF) + CZH_ScaleWidth(15); if (model.indexCount == model.totalCount - 1) { _bottomLineF = CGRectZero; } else { CGFloat bottomLineX = MARGIN_LEFT; CGFloat bottomLineY = _cellHeight - 0.5; CGFloat bottomLineW = ScreenWidth - bottomLineX; CGFloat bottomLineH = 0.5; _bottomLineF = CGRectMake(bottomLineX, bottomLineY, bottomLineW, bottomLineH); } CGFloat leftLineViewW = 1; CGFloat leftLineViewX = (MARGIN_LEFT - leftLineViewW) * 0.5; CGFloat leftLineViewY = 0; CGFloat leftLineViewH = 0; if (model.indexCount == 0) {//第一個 leftLineViewY = CGRectGetMaxY(_leftRoundViewF); leftLineViewH = _cellHeight - leftLineViewY; } else if (model.indexCount == model.totalCount - 1) {//最後一個 leftLineViewY = 0; leftLineViewH = CGRectGetMinY(_leftRoundViewF); } else { leftLineViewY = 0; leftLineViewH = _cellHeight; } _leftLineViewF = CGRectMake(leftLineViewX, leftLineViewY, leftLineViewW, leftLineViewH); }
cell賦值
- (void)setFrameModel:(CZHLogisticFrameModel *)frameModel {
    _frameModel = frameModel;

    [self czh_setData];

}

- (void)czh_setData {


    self.timeLabel.text = _frameModel.timeString;


    self.contentLabel.textColor = _frameModel.contentColor;
    self.timeLabel.textColor = _frameModel.timeColor;
    self.leftRoundView.backgroundColor = _frameModel.leftRoundColor;

    self.contentLabel.attributedText = _frameModel.contentAttributedString;

}



- (void)layoutSubviews {
    [super layoutSubviews];

    self.leftRoundView.frame = _frameModel.leftRoundViewF;
    [self.leftRoundView czh_cornerAllCornersWithCornerRadius:self.leftRoundView.czh_height * 0.5];

    self.leftLineView.frame = _frameModel.leftLineViewF;

    self.contentLabel.frame = _frameModel.contentLabelF;

    self.timeLabel.frame = _frameModel.timeLabelF;

    self.bottomLine.frame = _frameModel.bottomLineF;
}

公司的專案.png

公司的專案,求支援,如果發現什麼問題,可以留言反應,感激不盡