You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

416 lines
18 KiB

//
// XHAlbumRichTextView.m
// MessageDisplayExample
//
// Created by HUAJIE-1 on 14-5-19.
// Copyright (c) 2014年 曾宪华 开发团队(http://iyilunba.com ) 本人QQ:543413507 本人QQ群(142557668). All rights reserved.
//
#import "XHAlbumRichTextView.h"
#import "XHMessageAvatarFactory.h"
//#import <MessageDisplayKit/XHMessageAvatarFactory.h>
#import "XHAlbumPhotoCollectionViewCell.h"
#import "XHAlbumCollectionViewFlowLayout.h"
#import "XHImageViewer.h"
#import "XHAlbumLikesCommentsView.h"
#import "UIImageView+WebCache.h"
#import "myHttp.h"
#define kXHPhotoCollectionViewCellIdentifier @"XHPhotoCollectionViewCellIdentifier"
@interface XHAlbumCollectionView : UICollectionView
@end
@implementation XHAlbumCollectionView
@end
@interface XHAlbumRichTextView () <UICollectionViewDelegate, UICollectionViewDataSource,XHImageViewerDelegate>
@property (nonatomic, strong) UIImageView *avatarImageView;
@property (nonatomic, strong) UILabel *userNameLabel;
@property (nonatomic, strong) XHAlbumCollectionView *sharePhotoCollectionView;
@property (nonatomic, strong) UILabel *timestampLabel;
@property (nonatomic, strong) UIButton *commentButton;
//@property (nonatomic, strong) UILabel *likeNumLabel;
@property (nonatomic, strong) UIButton *likeButton;
@property (nonatomic, strong) XHAlbumLikesCommentsView *albumLikesCommentsView;
@end
@implementation XHAlbumRichTextView
+ (CGFloat)getRichTextHeightWithText:(NSString *)text {
if (!text || text.length == 0)
return 1;
return [SETextView frameRectWithAttributtedString:[[NSAttributedString alloc] initWithString:text] constraintSize:CGSizeMake(CGRectGetWidth([[UIScreen mainScreen] bounds]) - kXHAvatarImageSize - (kXHAlbumAvatarSpacing * 3), CGFLOAT_MAX) lineSpacing:kXHAlbumContentLineSpacing font:kXHAlbumContentFont].size.height;
}
+ (CGFloat)getSharePhotoCollectionViewHeightWithPhotos:(NSArray *)photos {
// 上下间隔已经在frame上做了
NSInteger row = (photos.count / 3 + (photos.count % 3 ? 1 : 0));
return (row * (kXHAlbumPhotoSize) + ((row - 1) * kXHAlbumPhotoInsets));
}
+ (CGFloat)calculateRichTextHeightWithAlbum:(XHAlbum *)currentAlbum {
CGFloat richTextHeight = kXHAlbumAvatarSpacing * 2;
richTextHeight += kXHAlbumUserNameHeigth;
richTextHeight += kXHAlbumContentLineSpacing;
richTextHeight += [self getRichTextHeightWithText:currentAlbum.albumShareContent];
richTextHeight += kXHAlbumPhotoInsets;
richTextHeight += [self getSharePhotoCollectionViewHeightWithPhotos:currentAlbum.albumSharePhotos];
richTextHeight += kXHAlbumContentLineSpacing;
richTextHeight += kXHAlbumCommentButtonHeight;
// richTextHeight += 4;
// richTextHeight += 14;
richTextHeight += currentAlbum.albumShareComments.count * 16;
return richTextHeight;
}
#pragma mark - Actions
- (void)commentButtonDidClicked:(UIButton *)sender {
if (self.commentButtonDidSelectedCompletion) {
self.commentButtonDidSelectedCompletion(sender);
}
}
- (void)likeButtonDidClicked:(UIButton *)sender {
if (self.likeButtonDidSelectedCompletion) {
self.likeButtonDidSelectedCompletion(sender);
}
}
#pragma mark - Propertys
- (void)setDisplayAlbum:(XHAlbum *)displayAlbum {
if (!displayAlbum)
return;
_displayAlbum = displayAlbum;
self.userNameLabel.text = displayAlbum.userName;
[self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:displayAlbum.profileAvatarUrlString]
placeholderImage:nil
options:SDWebImageRetryFailed
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
self.avatarImageView.image = [myHelper createRoundedRectImage:image
addImage:[myHelper getImageWithName:@"默认头像外框"]
size:CGSizeMake(60, 60)];
}];
self.richTextView.attributedText = [[NSAttributedString alloc] initWithString:displayAlbum.albumShareContent];
self.timestampLabel.text = [displayAlbum getTimeStr];
if ([displayAlbum.isAlbumShareLikeFormMe integerValue] == 1) {
[self.likeButton setTitleColor:[UIColor lightGrayColor]
forState:UIControlStateNormal];
}else if ([displayAlbum.isAlbumShareLikeFormMe integerValue] == 2){
[self.likeButton setTitleColor:[UIColor lightGrayColor]
forState:UIControlStateNormal];
}
[self.likeButton setTitle:[NSString stringWithFormat:@" %@",displayAlbum.albumShareLikeCount]
forState:UIControlStateNormal];
if ([displayAlbum.albumShareCommentCount integerValue] > 0) {
[self.commentButton setTitleColor:[UIColor lightGrayColor]
forState:UIControlStateNormal];
[self.commentButton setTitle:[NSString stringWithFormat:@" %@",displayAlbum.albumShareCommentCount]
forState:UIControlStateNormal];
}else{
[self.commentButton setTitle:@"" forState:UIControlStateNormal];
}
[self.sharePhotoCollectionView reloadData];
self.albumLikesCommentsView.likes = displayAlbum.albumShareLikes;
self.albumLikesCommentsView.comments = displayAlbum.albumShareComments;
[self.albumLikesCommentsView reloadData];
[self setNeedsLayout];
}
- (UIImageView *)avatarImageView {
if (!_avatarImageView) {
_avatarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kXHAlbumAvatarSpacing, kXHAlbumAvatarSpacing, kXHAvatarImageSize, kXHAvatarImageSize)];
}
return _avatarImageView;
}
- (UILabel *)userNameLabel {
if (!_userNameLabel) {
CGFloat userNameLabelX = CGRectGetMaxX(self.avatarImageView.frame) + kXHAlbumAvatarSpacing;
_userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(userNameLabelX, CGRectGetMinY(self.avatarImageView.frame), CGRectGetWidth([[UIScreen mainScreen] bounds]) - userNameLabelX - kXHAlbumAvatarSpacing, kXHAlbumUserNameHeigth)];
_userNameLabel.backgroundColor = [UIColor clearColor];
_userNameLabel.textColor = [UIColor blackColor];
}
return _userNameLabel;
}
- (SETextView *)richTextView {
if (!_richTextView) {
_richTextView = [[SETextView alloc] initWithFrame:self.bounds];
_richTextView.backgroundColor = [UIColor clearColor];
_richTextView.font = self.font;
_richTextView.textColor = self.textColor;
_richTextView.textAlignment = self.textAlignment;
_richTextView.lineSpacing = self.lineSpacing;
}
return _richTextView;
}
- (XHAlbumCollectionView *)sharePhotoCollectionView {
if (!_sharePhotoCollectionView) {
_sharePhotoCollectionView = [[XHAlbumCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[[XHAlbumCollectionViewFlowLayout alloc] init]];
_sharePhotoCollectionView.backgroundColor = [UIColor clearColor];
[_sharePhotoCollectionView registerClass:[XHAlbumPhotoCollectionViewCell class] forCellWithReuseIdentifier:kXHPhotoCollectionViewCellIdentifier];
[_sharePhotoCollectionView setScrollsToTop:NO];
_sharePhotoCollectionView.delegate = self;
_sharePhotoCollectionView.dataSource = self;
}
return _sharePhotoCollectionView;
}
- (UILabel *)timestampLabel {
if (!_timestampLabel) {
_timestampLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_timestampLabel.backgroundColor = [UIColor clearColor];
_timestampLabel.font = [UIFont systemFontOfSize:11];
_timestampLabel.textColor = [UIColor grayColor];
}
return _timestampLabel;
}
- (UIButton *)commentButton {
if (!_commentButton) {
_commentButton = [[UIButton alloc] initWithFrame:CGRectZero];
_commentButton.titleLabel.font = self.font;
[_commentButton setImage:[myHelper getImageWithName:@"评论暗"] forState:UIControlStateNormal];
[_commentButton setImage:[myHelper getImageWithName:@"评论暗"] forState:UIControlStateHighlighted];
[_commentButton addTarget:self action:@selector(commentButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
}
return _commentButton;
}
//- (UILabel *)likeNumLabel {
// if (!_likeNumLabel) {
// _likeNumLabel = [[UILabel alloc] initWithFrame:CGRectZero];
// _likeNumLabel.font = self.font;
// _likeNumLabel.textAlignment = NSTextAlignmentCenter;
// }
// return _likeNumLabel;
//}
- (UIButton *)likeButton {
if (!_likeButton) {
_likeButton = [[UIButton alloc] initWithFrame:CGRectZero];
_likeButton.titleLabel.font = self.font;
[_likeButton setImage:[myHelper getImageWithName:@"未点赞"] forState:UIControlStateNormal];
[_likeButton setImage:[myHelper getImageWithName:@"未点赞"] forState:UIControlStateHighlighted];
[_likeButton addTarget:self action:@selector(likeButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
}
return _likeButton;
}
- (XHAlbumLikesCommentsView *)albumLikesCommentsView {
if (!_albumLikesCommentsView) {
_albumLikesCommentsView = [[XHAlbumLikesCommentsView alloc] initWithFrame:CGRectZero];
}
return _albumLikesCommentsView;
}
#pragma mark - Life Cycle
- (void)setup {
self.font = kXHAlbumContentFont;
self.textColor = [UIColor darkGrayColor];
self.textAlignment = NSTextAlignmentLeft;
self.lineSpacing = kXHAlbumContentLineSpacing;
[self addSubview:self.avatarImageView];
[self addSubview:self.userNameLabel];
[self addSubview:self.timestampLabel];
[self addSubview:self.richTextView];
[self addSubview:self.sharePhotoCollectionView];
[self addSubview:self.commentButton];
// [self addSubview:self.likeNumLabel];
[self addSubview:self.likeButton];
[self addSubview:self.albumLikesCommentsView];
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self setup];
}
return self;
}
- (void)dealloc {
self.font = nil;
self.textColor = nil;
self.richTextView = nil;
_displayAlbum = nil;
self.avatarImageView = nil;
self.userNameLabel = nil;
self.sharePhotoCollectionView.delegate = nil;
self.sharePhotoCollectionView.dataSource = nil;
self.sharePhotoCollectionView = nil;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat richTextViewX = CGRectGetMinX(self.userNameLabel.frame);
CGRect richTextViewFrame = CGRectMake(richTextViewX, CGRectGetMaxY(self.userNameLabel.frame) + kXHAlbumContentLineSpacing,
CGRectGetWidth([[UIScreen mainScreen] bounds]) - richTextViewX - kXHAlbumAvatarSpacing,
[XHAlbumRichTextView getRichTextHeightWithText:self.displayAlbum.albumShareContent]);
self.richTextView.frame = richTextViewFrame;
NSInteger count = self.displayAlbum.albumSharePhotos.count;
if (count > 3) {
count = 3;
}
CGRect sharePhotoCollectionViewFrame = CGRectMake(richTextViewX, CGRectGetMaxY(richTextViewFrame) + kXHAlbumPhotoInsets, kXHAlbumPhotoInsets * (count + 1) + kXHAlbumPhotoSize * count, [XHAlbumRichTextView getSharePhotoCollectionViewHeightWithPhotos:self.displayAlbum.albumSharePhotos]);
self.sharePhotoCollectionView.frame = sharePhotoCollectionViewFrame;
CGRect timestampLabelFrame = self.timestampLabel.frame;
timestampLabelFrame.origin = CGPointMake(richTextViewX, CGRectGetMaxY(sharePhotoCollectionViewFrame) + kXHAlbumContentLineSpacing * 2.5);
timestampLabelFrame.size = CGSizeMake(CGRectGetWidth(self.bounds) - kXHAlbumAvatarSpacing * 3 - kXHAvatarImageSize - kXHAlbumCommentButtonWidth, kXHAlbumCommentButtonHeight);
self.timestampLabel.frame = timestampLabelFrame;
if (!self.displayAlbum.isComment) {
self.commentButton.hidden = NO;
self.likeButton.hidden = NO;
// self.likeNumLabel.hidden = NO;
// self.likeNumLabel.hidden = NO;
CGRect commentButtonFrame = self.commentButton.frame;
commentButtonFrame.origin = CGPointMake(CGRectGetMaxX(richTextViewFrame) - kXHAlbumCommentButtonWidth *1.5,
CGRectGetMinY(timestampLabelFrame) + kXHAlbumContentLineSpacing);
commentButtonFrame.size = CGSizeMake(kXHAlbumCommentButtonWidth, kXHAlbumCommentButtonHeight);
self.commentButton.frame = commentButtonFrame;
if ([self.displayAlbum.albumShareCommentCount integerValue] > 0) {
[_commentButton setImage:[myHelper getImageWithName:@"有评论"] forState:UIControlStateNormal];
[_commentButton setImage:[myHelper getImageWithName:@"有评论"] forState:UIControlStateHighlighted];
}else{
[_commentButton setImage:[myHelper getImageWithName:@"评论暗"] forState:UIControlStateNormal];
[_commentButton setImage:[myHelper getImageWithName:@"评论暗"] forState:UIControlStateHighlighted];
}
[self.commentButton sizeToFit];
CGRect likeButtonFrame = self.likeButton.frame;
likeButtonFrame.origin = CGPointMake(CGRectGetMaxX(richTextViewFrame) - kXHAlbumCommentButtonWidth * 3.5,
CGRectGetMinY(timestampLabelFrame) + kXHAlbumContentLineSpacing);
likeButtonFrame.size = CGSizeMake(kXHAlbumCommentButtonWidth, kXHAlbumCommentButtonHeight);
self.likeButton.frame = likeButtonFrame;
if ([self.displayAlbum.isAlbumShareLikeFormMe integerValue] == 1) {
[_likeButton setImage:[myHelper getImageWithName:@"已点赞"] forState:UIControlStateNormal];
[_likeButton setImage:[myHelper getImageWithName:@"已点赞"] forState:UIControlStateHighlighted];
}else if([self.displayAlbum.isAlbumShareLikeFormMe integerValue] == 2){
[_likeButton setImage:[myHelper getImageWithName:@"未点赞"] forState:UIControlStateNormal];
[_likeButton setImage:[myHelper getImageWithName:@"未点赞"] forState:UIControlStateHighlighted];
}
[self.likeButton sizeToFit];
// CGRect likeLabelFrame = self.likeNumLabel.frame;
// likeLabelFrame.origin = CGPointMake(CGRectGetMaxX(richTextViewFrame) - kXHAlbumCommentButtonWidth * 3,
// CGRectGetMinY(timestampLabelFrame));
// likeLabelFrame.size = CGSizeMake(kXHAlbumCommentButtonWidth, kXHAlbumCommentButtonHeight);
// self.likeNumLabel.frame = likeLabelFrame;
// 这里出现延迟布局的情况,所以就不能正常排版
CGRect likesCommentsViewFrame = self.albumLikesCommentsView.frame;
likesCommentsViewFrame.origin = CGPointMake(CGRectGetMinX(richTextViewFrame), CGRectGetMaxY(commentButtonFrame));
likesCommentsViewFrame.size.width = CGRectGetWidth(richTextViewFrame);
self.albumLikesCommentsView.frame = likesCommentsViewFrame;
CGRect frame = self.frame;
frame.size.height = CGRectGetMaxY(likesCommentsViewFrame) + kXHAlbumAvatarSpacing;
self.frame = frame;
}else{
self.commentButton.hidden = YES;
self.likeButton.hidden = YES;
// self.likeNumLabel.hidden = YES;
// self.likeNumLabel.hidden = YES;
CGRect frame = self.frame;
frame.size.height = CGRectGetMaxY(sharePhotoCollectionViewFrame) + kXHAlbumAvatarSpacing;
self.frame = frame;
}
}
#pragma mark - UICollectionView DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.displayAlbum.albumSharePhotos.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
XHAlbumPhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kXHPhotoCollectionViewCellIdentifier forIndexPath:indexPath];
cell.imageUrl = self.displayAlbum.albumSharePhotos[indexPath.item];
cell.indexPath = indexPath;
return cell;
}
#pragma mark - UICollectionView Delegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[self showImageViewerAtIndexPath:indexPath];
}
- (void)showImageViewerAtIndexPath:(NSIndexPath *)indexPath {
XHAlbumPhotoCollectionViewCell *cell = (XHAlbumPhotoCollectionViewCell *)[self.sharePhotoCollectionView cellForItemAtIndexPath:indexPath];
NSMutableArray *imageViews = [NSMutableArray array];
NSArray *visibleCell = [self.sharePhotoCollectionView visibleCells];
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"indexPath" ascending:YES];
visibleCell = [visibleCell sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
[visibleCell enumerateObjectsUsingBlock:^(XHAlbumPhotoCollectionViewCell *cell, NSUInteger idx, BOOL *stop) {
NSString *originUrl = [cell.imageUrl stringByReplacingOccurrencesOfString:@"normal" withString:@"origin"];
[cell.photoImageView sd_setImageWithURL:[NSURL URLWithString:originUrl]
placeholderImage:cell.photoImageView.image
options:SDWebImageRetryFailed];
[imageViews addObject:cell.photoImageView];
}];
XHImageViewer *imageViewer = [[XHImageViewer alloc] init];
imageViewer.delegate = self;
[imageViewer showWithImageViews:imageViews selectedView:cell.photoImageView];
}
//- (void)imageViewer:(XHImageViewer *)imageViewer didDismissWithSelectedView:(UIImageView *)selectedView{
// [self.sharePhotoCollectionView reloadData];
//}
@end