//
|
// HXTagsCell.m
|
// HXTagsView https://github.com/huangxuan518/HXTagsView
|
// 博客地址 http://blog.libuqing.com/
|
// Created by Love on 16/6/30.
|
// Copyright © 2016年 IT小子. All rights reserved.
|
//
|
|
#import "HXTagsCell.h"
|
#import "HXTagCollectionViewCell.h"
|
#import "HXTagAttribute.h"
|
|
@interface HXTagsCell () <UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
|
|
@property (nonatomic,strong) UICollectionView *collectionView;
|
|
@end
|
|
@implementation HXTagsCell
|
|
static NSString * const reuseIdentifier = @"HXTagCollectionViewCellId";
|
|
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
|
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
|
if (self) {
|
[self setup];
|
}
|
|
return self;
|
}
|
|
- (id)initWithCoder:(NSCoder *)aDecoder
|
{
|
if (self = [super initWithCoder:aDecoder]) {
|
[self setup];
|
}
|
|
return self;
|
}
|
|
- (void)setup
|
{
|
//初始化样式
|
_tagAttribute = [HXTagAttribute new];
|
|
_layout = [[HXTagCollectionViewFlowLayout alloc] init];
|
|
[self addSubview:self.collectionView];
|
}
|
|
#pragma mark - UICollectionViewDelegate | UICollectionViewDataSource
|
|
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
|
{
|
return _tags.count;
|
}
|
|
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
|
{
|
HXTagCollectionViewFlowLayout *layout = (HXTagCollectionViewFlowLayout *)collectionView.collectionViewLayout;
|
CGSize maxSize = CGSizeMake(collectionView.frame.size.width - layout.sectionInset.left - layout.sectionInset.right, layout.itemSize.height);
|
|
CGRect frame = [_tags[indexPath.item] boundingRectWithSize:maxSize options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:_tagAttribute.titleSize]} context:nil];
|
|
return CGSizeMake(frame.size.width + _tagAttribute.tagSpace, layout.itemSize.height);
|
}
|
|
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
|
{
|
HXTagCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
|
cell.backgroundColor = _tagAttribute.normalBackgroundColor;
|
cell.layer.borderColor = _tagAttribute.borderColor.CGColor;
|
cell.layer.cornerRadius = _tagAttribute.cornerRadius;
|
cell.layer.borderWidth = _tagAttribute.borderWidth;
|
cell.titleLabel.textColor = _tagAttribute.textColor;
|
cell.titleLabel.font = [UIFont systemFontOfSize:_tagAttribute.titleSize];
|
|
NSString *title = self.tags[indexPath.item];
|
if (_key.length > 0) {
|
cell.titleLabel.attributedText = [self searchTitle:title key:_key keyColor:_tagAttribute.keyColor];
|
} else {
|
cell.titleLabel.text = title;
|
}
|
|
if ([self.selectedTags containsObject:self.tags[indexPath.item]]) {
|
cell.backgroundColor = _tagAttribute.selectedBackgroundColor;
|
}
|
|
return cell;
|
}
|
|
|
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
|
{
|
HXTagCollectionViewCell *cell = (HXTagCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
|
|
if ([self.selectedTags containsObject:self.tags[indexPath.item]]) {
|
cell.backgroundColor = _tagAttribute.normalBackgroundColor;
|
[self.selectedTags removeObject:self.tags[indexPath.item]];
|
}
|
else {
|
if (_isMultiSelect) {
|
cell.backgroundColor = _tagAttribute.selectedBackgroundColor;
|
[self.selectedTags addObject:self.tags[indexPath.item]];
|
} else {
|
[self.selectedTags removeAllObjects];
|
[self.selectedTags addObject:self.tags[indexPath.item]];
|
|
[self reloadData];
|
}
|
}
|
|
if (_completion) {
|
_completion(self.selectedTags,indexPath.item);
|
}
|
}
|
|
// 设置文字中关键字高亮
|
- (NSMutableAttributedString *)searchTitle:(NSString *)title key:(NSString *)key keyColor:(UIColor *)keyColor {
|
|
NSMutableAttributedString *titleStr = [[NSMutableAttributedString alloc] initWithString:title];
|
NSString *copyStr = title;
|
|
NSMutableString *xxstr = [NSMutableString new];
|
for (int i = 0; i < key.length; i++) {
|
[xxstr appendString:@"*"];
|
}
|
|
while ([copyStr rangeOfString:key options:NSCaseInsensitiveSearch].location != NSNotFound) {
|
|
NSRange range = [copyStr rangeOfString:key options:NSCaseInsensitiveSearch];
|
|
[titleStr addAttribute:NSForegroundColorAttributeName value:keyColor range:range];
|
copyStr = [copyStr stringByReplacingCharactersInRange:NSMakeRange(range.location, range.length) withString:xxstr];
|
}
|
return titleStr;
|
}
|
|
- (void)reloadData {
|
[self.collectionView reloadData];
|
}
|
|
- (void)layoutSubviews {
|
[super layoutSubviews];
|
_collectionView.frame = self.bounds;
|
}
|
|
#pragma mark - 懒加载
|
|
- (NSMutableArray *)selectedTags
|
{
|
if (!_selectedTags) {
|
_selectedTags = [NSMutableArray array];
|
}
|
return _selectedTags;
|
}
|
|
- (UICollectionView *)collectionView {
|
if (!_collectionView) {
|
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 362) collectionViewLayout:_layout];
|
_collectionView.delegate = self;
|
_collectionView.dataSource = self;
|
_collectionView.backgroundColor = [UIColor colorWithRed:241/255.0 green:241/255.0 blue:241/255.0 alpha:1.0];
|
[_collectionView registerClass:[HXTagCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
|
}
|
|
_collectionView.collectionViewLayout = _layout;
|
|
if (_layout.scrollDirection == UICollectionViewScrollDirectionVertical) {
|
//垂直
|
_collectionView.showsVerticalScrollIndicator = YES;
|
_collectionView.showsHorizontalScrollIndicator = NO;
|
} else {
|
_collectionView.showsHorizontalScrollIndicator = YES;
|
_collectionView.showsVerticalScrollIndicator = NO;
|
}
|
|
_collectionView.frame = self.bounds;
|
|
return _collectionView;
|
}
|
|
+ (CGFloat)getCellHeightWithTags:(NSArray *)tags layout:(HXTagCollectionViewFlowLayout *)layout tagAttribute:(HXTagAttribute *)tagAttribute width:(CGFloat)width
|
{
|
CGFloat contentHeight = 0;
|
|
if (!layout) {
|
layout = [[HXTagCollectionViewFlowLayout alloc] init];
|
}
|
|
if (tagAttribute.titleSize <= 0) {
|
tagAttribute = [[HXTagAttribute alloc] init];
|
}
|
|
//cell的高度 = 顶部 + 高度
|
contentHeight = layout.sectionInset.top + layout.itemSize.height;
|
|
CGFloat originX = layout.sectionInset.left;
|
CGFloat originY = layout.sectionInset.top;
|
|
NSInteger itemCount = tags.count;
|
|
for (NSInteger i = 0; i < itemCount; i++) {
|
CGSize maxSize = CGSizeMake(width - layout.sectionInset.left - layout.sectionInset.right, layout.itemSize.height);
|
|
CGRect frame = [tags[i] boundingRectWithSize:maxSize options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:tagAttribute.titleSize]} context:nil];
|
|
CGSize itemSize = CGSizeMake(frame.size.width + tagAttribute.tagSpace, layout.itemSize.height);
|
|
if (layout.scrollDirection == UICollectionViewScrollDirectionVertical) {
|
//垂直滚动
|
//当前CollectionViewCell的起点 + 当前CollectionViewCell的宽度 + 当前CollectionView距离右侧的间隔 > collectionView的宽度
|
if ((originX + itemSize.width + layout.sectionInset.right) > width) {
|
originX = layout.sectionInset.left;
|
originY += itemSize.height + layout.minimumLineSpacing;
|
|
contentHeight += itemSize.height + layout.minimumLineSpacing;
|
}
|
}
|
|
originX += itemSize.width + layout.minimumInteritemSpacing;
|
}
|
|
contentHeight += layout.sectionInset.bottom;
|
return contentHeight;
|
}
|
|
@end
|