|
| 1 | +// |
| 2 | +// MRPRCommentCell.m |
| 3 | +// Coding_iOS |
| 4 | +// |
| 5 | +// Created by Ease on 15/6/1. |
| 6 | +// Copyright (c) 2015年 Coding. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#define kMRPRCommentCell_FontContent [UIFont systemFontOfSize:15] |
| 10 | + |
| 11 | +#import "MRPRCommentCell.h" |
| 12 | +#import "UICustomCollectionView.h" |
| 13 | +#import "MRPRCommentCCell.h" |
| 14 | +#import "MJPhotoBrowser.h" |
| 15 | + |
| 16 | +@interface MRPRCommentCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout> |
| 17 | +@property (strong, nonatomic) UIImageView *ownerIconView; |
| 18 | +@property (strong, nonatomic) UILabel *timeLabel; |
| 19 | +@property (strong, nonatomic) UICustomCollectionView *imageCollectionView; |
| 20 | + |
| 21 | +@end |
| 22 | + |
| 23 | +@implementation MRPRCommentCell |
| 24 | +- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier |
| 25 | +{ |
| 26 | + self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; |
| 27 | + if (self) { |
| 28 | + // Initialization code |
| 29 | + CGFloat curBottomY = 10; |
| 30 | + if (!_ownerIconView) { |
| 31 | + _ownerIconView = [[UIImageView alloc] initWithFrame:CGRectMake(kPaddingLeftWidth, curBottomY, 33, 33)]; |
| 32 | + [_ownerIconView doCircleFrame]; |
| 33 | + [self.contentView addSubview:_ownerIconView]; |
| 34 | + } |
| 35 | + CGFloat curWidth = kScreen_Width - 40 - 2*kPaddingLeftWidth; |
| 36 | + if (!_contentLabel) { |
| 37 | + _contentLabel = [[UITTTAttributedLabel alloc] initWithFrame:CGRectMake(kPaddingLeftWidth + 40, curBottomY, curWidth, 30)]; |
| 38 | + _contentLabel.textColor = [UIColor colorWithHexString:@"0x555555"]; |
| 39 | + _contentLabel.font = kMRPRCommentCell_FontContent; |
| 40 | + _contentLabel.linkAttributes = kLinkAttributes; |
| 41 | + _contentLabel.activeLinkAttributes = kLinkAttributesActive; |
| 42 | + [self.contentView addSubview:_contentLabel]; |
| 43 | + } |
| 44 | + CGFloat commentBtnWidth = 40; |
| 45 | + if (!_timeLabel) { |
| 46 | + _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPaddingLeftWidth +40, 0, curWidth- commentBtnWidth, 20)]; |
| 47 | + _timeLabel.textColor = [UIColor colorWithHexString:@"0x999999"]; |
| 48 | + _timeLabel.font = [UIFont systemFontOfSize:12]; |
| 49 | + [self.contentView addSubview:_timeLabel]; |
| 50 | + } |
| 51 | + if ([reuseIdentifier isEqualToString:kCellIdentifier_MRPRCommentCell_Media]) { |
| 52 | + if (!self.imageCollectionView) { |
| 53 | + UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; |
| 54 | + self.imageCollectionView = [[UICustomCollectionView alloc] initWithFrame:CGRectMake(kPaddingLeftWidth + 40, 0, curWidth, 43) collectionViewLayout:layout]; |
| 55 | + self.imageCollectionView.scrollEnabled = NO; |
| 56 | + [self.imageCollectionView setBackgroundView:nil]; |
| 57 | + [self.imageCollectionView setBackgroundColor:[UIColor clearColor]]; |
| 58 | + [self.imageCollectionView registerClass:[MRPRCommentCCell class] forCellWithReuseIdentifier:kCCellIdentifier_MRPRCommentCCell]; |
| 59 | + self.imageCollectionView.dataSource = self; |
| 60 | + self.imageCollectionView.delegate = self; |
| 61 | + [self.contentView addSubview:self.imageCollectionView]; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + return self; |
| 66 | +} |
| 67 | + |
| 68 | +- (void)setCurItem:(MRPRCommentItem *)curItem{ |
| 69 | + _curItem = curItem; |
| 70 | + |
| 71 | + if (!_curItem) { |
| 72 | + return; |
| 73 | + } |
| 74 | + CGFloat curBottomY = 10; |
| 75 | + CGFloat curWidth = kScreen_Width - 40 - 2*kPaddingLeftWidth; |
| 76 | + [_ownerIconView sd_setImageWithURL:[_curItem.author.avatar urlImageWithCodePathResizeToView:_ownerIconView] placeholderImage:kPlaceholderMonkeyRoundView(_ownerIconView)]; |
| 77 | + [_contentLabel setLongString:_curItem.content withFitWidth:curWidth]; |
| 78 | + |
| 79 | + for (HtmlMediaItem *item in _curItem.htmlMedia.mediaItems) { |
| 80 | + if (item.displayStr.length > 0 && !(item.type == HtmlMediaItemType_Code ||item.type == HtmlMediaItemType_EmotionEmoji)) { |
| 81 | + [_contentLabel addLinkToTransitInformation:[NSDictionary dictionaryWithObject:item forKey:@"value"] withRange:item.range]; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + curBottomY += [_curItem.content getHeightWithFont:kMRPRCommentCell_FontContent constrainedToSize:CGSizeMake(curWidth, CGFLOAT_MAX)] + 5; |
| 86 | + |
| 87 | + NSInteger imagesCount = _curItem.htmlMedia.imageItems.count; |
| 88 | + if (imagesCount > 0) { |
| 89 | + self.imageCollectionView.hidden = NO; |
| 90 | + [self.imageCollectionView setFrame:CGRectMake(kPaddingLeftWidth +40, curBottomY, curWidth, [MRPRCommentCell imageCollectionViewHeightWithCount:imagesCount])]; |
| 91 | + [self.imageCollectionView reloadData]; |
| 92 | + }else{ |
| 93 | + self.imageCollectionView.hidden = YES; |
| 94 | + } |
| 95 | + |
| 96 | + curBottomY += [MRPRCommentCell imageCollectionViewHeightWithCount:imagesCount]; |
| 97 | + |
| 98 | + [_timeLabel setY:curBottomY]; |
| 99 | + _timeLabel.text = [NSString stringWithFormat:@"%@ %@", _curItem.author.name, [_curItem.created_at stringTimesAgo]]; |
| 100 | +} |
| 101 | + |
| 102 | ++ (CGFloat)cellHeightWithObj:(id)obj{ |
| 103 | + CGFloat cellHeight = 0; |
| 104 | + if ([obj isKindOfClass:[MRPRCommentItem class]]) { |
| 105 | + MRPRCommentItem *curItem = (MRPRCommentItem *)obj; |
| 106 | + CGFloat curWidth = kScreen_Width - 40 - 2*kPaddingLeftWidth; |
| 107 | + cellHeight += 10 +[curItem.content getHeightWithFont:kMRPRCommentCell_FontContent constrainedToSize:CGSizeMake(curWidth, CGFLOAT_MAX)] + 5 +20 +10; |
| 108 | + cellHeight += [self imageCollectionViewHeightWithCount:curItem.htmlMedia.imageItems.count]; |
| 109 | + } |
| 110 | + return cellHeight; |
| 111 | +} |
| 112 | + |
| 113 | ++ (CGFloat)imageCollectionViewHeightWithCount:(NSInteger)countNum{ |
| 114 | + if (countNum <= 0) { |
| 115 | + return 0; |
| 116 | + } |
| 117 | + CGFloat curWidth = kScreen_Width - 40 - 2*kPaddingLeftWidth; |
| 118 | + NSInteger numInOneLine = floorf((curWidth +5)/(33 + 5)); |
| 119 | + NSInteger numOfline = ceilf(countNum/(float)numInOneLine); |
| 120 | + return (43 *numOfline); |
| 121 | +} |
| 122 | + |
| 123 | +#pragma mark Collection M |
| 124 | +- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ |
| 125 | + return _curItem.htmlMedia.imageItems.count; |
| 126 | +} |
| 127 | + |
| 128 | +// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath: |
| 129 | +- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ |
| 130 | + MRPRCommentCCell *ccell = [collectionView dequeueReusableCellWithReuseIdentifier:kCCellIdentifier_MRPRCommentCCell forIndexPath:indexPath]; |
| 131 | + ccell.curMediaItem = [_curItem.htmlMedia.imageItems objectAtIndex:indexPath.row]; |
| 132 | + return ccell; |
| 133 | +} |
| 134 | +- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ |
| 135 | + return [MRPRCommentCCell ccellSize]; |
| 136 | +} |
| 137 | +- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{ |
| 138 | + return UIEdgeInsetsZero; |
| 139 | +} |
| 140 | +- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{ |
| 141 | + return 10; |
| 142 | +} |
| 143 | +- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{ |
| 144 | + return 5; |
| 145 | +} |
| 146 | + |
| 147 | +- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ |
| 148 | + // 显示大图 |
| 149 | + int count = (int)_curItem.htmlMedia.imageItems.count; |
| 150 | + NSMutableArray *photos = [NSMutableArray arrayWithCapacity:count]; |
| 151 | + for (int i = 0; i<count; i++) { |
| 152 | + HtmlMediaItem *imageItem = [_curItem.htmlMedia.imageItems objectAtIndex:i]; |
| 153 | + MJPhoto *photo = [[MJPhoto alloc] init]; |
| 154 | + photo.url = [NSURL URLWithString:imageItem.src]; // 图片路径 |
| 155 | + [photos addObject:photo]; |
| 156 | + } |
| 157 | + // 2.显示相册 |
| 158 | + MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init]; |
| 159 | + browser.currentPhotoIndex = indexPath.row; // 弹出相册时显示的第一张图片是? |
| 160 | + browser.photos = photos; // 设置所有的图片 |
| 161 | + [browser show]; |
| 162 | +} |
| 163 | + |
| 164 | +@end |
0 commit comments