标签流式布局

本文介绍了一个名为SZTagsView的自定义视图,用于实现标签流式布局。该视图允许设置标签间距、内边距、字体、颜色、图片以及点击事件。通过设置不同的参数,如标签之间的横向和纵向间距,可以灵活地调整标签布局。
摘要由CSDN通过智能技术生成
</pre><pre code_snippet_id="1734307" snippet_file_name="blog_20160628_2_787321" name="code" class="html"><span style="font-family: Arial, Helvetica, sans-serif;">@interface SZTagsView : UIView</span>

/**
 @brief 设置标签等与视图周围内边距,视图view最大宽度
 @param InsetSpace:标签等与视图周围内边距,默认为UIEdgeInsetsZero
 @param ViewTotalWidth:视图view最大宽度,默认为屏幕宽度
 */
-(void)setInsetSpace:(UIEdgeInsets)InsetSpace WithMaxViewTotalWidth:(CGFloat)ViewTotalWidth;


/**
 @brief 设置标签之间横向间距,标签之间纵向行间距
 @param itemHorSpace:标签之间横向间距,默认为0
 @param lineSpace:标签之间纵向行间距,默认为0
 */
-(void)setLabelsWithItemSpace:(CGFloat)itemSpace WithLineSpace:(CGFloat)lineSpace;


/**
 @brief 设置标签字体,标签字体颜色
 @param font:标签字体,默认为系统15号字体
 @param color:标签字体颜色,默认0x333333
 */
-(void)setItemLabelWithFont:(UIFont *)font WithColor:(UIColor *)color;

/**
 @brief 设置标签头部图片,图片尺寸,图片与标签横向间距
 @param image:标签头部图片,默认无图片
 @param imageSize:图片尺寸,若image为空,则忽略imageSize
 @param imageToItemHorSpace:图片与标签横向间距,若image为空,则忽略imageToItemHorSpace
 */
-(void)setImage:(UIImage *)image WithImageSize:(CGSize)imageSize WithimageToItemHorSpace:(CGFloat)imageToItemHorSpace;

//将图片以及标签进行展示
/**
 @brief 设置展示标签数组,并进行展示
 @param tagsArray:标签数组
 */
-(void)setViewWithTagArray:(NSArray<NSString *> *)tagsArray;


/**
 @brief 获取视图所需的高度
 */
-(CGFloat)getViewTotalHeight;


/**
 @brief 点击标签,执行block
 @param completion:执行的block
 */
-(void)didClickOnTag:(void(^)(NSString *str))completion;

@end
</pre><pre code_snippet_id="1734307" snippet_file_name="blog_20160628_5_8770216" name="code" class="html">
<span style="font-family: Arial, Helvetica, sans-serif;">#import "SZTagsView.h"</span>

@implementation SZTagsView
{
    
    UIEdgeInsets _InsetSpace;
    CGFloat _itemSpace;
    CGFloat _lineSpace;
    CGFloat _ViewTotalWidth;
    
    UIFont *_itemLabelFont;
    UIColor *_itemLabelColor;
    
    UIImage *_image;
    CGSize _imageSize;
    
    CGFloat _imageToItemHorSpace;
    
    void(^_didClickTagBlock)(NSString *str);
    
    CGFloat itemHeight;
    CGFloat _ViewTotalHeight;
}

#define imageMaxSize CGSizeMake(30, 30)

-(id)initWithCoder:(NSCoder *)aDecoder{
    
    self = [super initWithCoder:aDecoder];
    if (self) {
        
        _InsetSpace = UIEdgeInsetsZero;
        _itemSpace = 0;
        _lineSpace = 0;
        _ViewTotalWidth = ScreenWidth;
        
        _itemLabelFont = [UIFont systemFontOfSize:15];
        
        
        _image = nil;
        _imageSize = CGSizeMake(0, 0);
        _imageToItemHorSpace = 0;
        
        
        _ViewTotalHeight = 0;
        _didClickTagBlock = nil;
    }
    return self;
}

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        _InsetSpace = UIEdgeInsetsZero;
        _itemSpace = 0;
        _lineSpace = 0;
        _ViewTotalWidth = ScreenWidth;
        
        _itemLabelFont = [UIFont systemFontOfSize:15];
        
        
        _image = nil;
        _imageSize = CGSizeMake(0, 0);
        _imageToItemHorSpace = 0;
        
        
        _ViewTotalHeight = 0;
        _didClickTagBlock = nil;
    }
    return self;
}

-(void)setInsetSpace:(UIEdgeInsets)InsetSpace WithMaxViewTotalWidth:(CGFloat)ViewTotalWidth{
    
    _InsetSpace = InsetSpace;
    
    if (ViewTotalWidth > 0) {
        _ViewTotalWidth = ViewTotalWidth;
    }
    
}

-(void)setLabelsWithItemSpace:(CGFloat)itemSpace WithLineSpace:(CGFloat)lineSpace{
    
    if (itemSpace > 0) {
        _itemSpace = itemSpace;
    }
    
    if (lineSpace > 0) {
        _lineSpace = lineSpace;
    }
}

-(void)setItemLabelWithFont:(UIFont *)font WithColor:(UIColor *)color{
    if (font) {
        _itemLabelFont = font;
    }
    
    if (color) {
        _itemLabelColor = color;
    }
}

-(void)setImage:(UIImage *)image WithImageSize:(CGSize)imageSize WithimageToItemHorSpace:(CGFloat)imageToItemHorSpace{
    
    if (image) {
        _image = image;
        _imageSize = imageSize;
        
        if (imageToItemHorSpace) {
            _imageToItemHorSpace = imageToItemHorSpace;
        }
    }
}

-(void)setViewWithTagArray:(NSArray<NSString *> *)tagsArray{
    for (UIView *subView in self.subviews) {
        if (subView) {
            [subView removeFromSuperview];
        }
    }
    
    _ViewTotalHeight = 0;
    if (!tagsArray || tagsArray.count <= 0) {
        return;
    }
    
    if (_itemLabelFont) {
        itemHeight = (NSInteger)(_itemLabelFont.lineHeight + 3);
    } else {
        itemHeight = 15;
    }
    
    if (_image) {

        if (_imageSize.height > itemHeight) {
            itemHeight = _imageSize.height;
        }
        
    }
    
    UIView *beforeView = nil;
    
    
    for (NSInteger i = 0; i < tagsArray.count; i++) {
        
        UILabel *tagLabel = [[UILabel alloc] init];
        if (_itemLabelFont) {
            tagLabel.font = _itemLabelFont;
        } else {
            tagLabel.font = [UIFont systemFontOfSize:15];
        }
        
        if (_itemLabelColor) {
            tagLabel.textColor = _itemLabelColor;
        } else {
            tagLabel.textColor = UIColorFromRGB_hex(0x333333);
        }
        
        tagLabel.textAlignment = NSTextAlignmentCenter;
        tagLabel.text = [tagsArray objectAtIndex:i];
        tagLabel.backgroundColor = [UIColor greenColor];
        tagLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        tagLabel.numberOfLines = 1;
        [self addSubview:tagLabel];
        
        tagLabel.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapTag = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapTag:)];
        [tagLabel addGestureRecognizer:tapTag];
        
        //此处需要更改
        CGFloat itemWidth = [tagLabel.text sizeWithAttributes:@{NSFontAttributeName:tagLabel.font}].width;
        
        
        //标签前有图片
        if (_image) {
            
            UIImageView *imageView = nil;
            
            imageView = [[UIImageView alloc] init];
            imageView.image = _image;
            [self addSubview:imageView];
            
            //单独一行中标签的最大宽度
            if (itemWidth > (_ViewTotalWidth - _InsetSpace.left - _InsetSpace.right - _imageSize.width - _imageToItemHorSpace)) {
                itemWidth = _ViewTotalWidth - _InsetSpace.left - _InsetSpace.right - _imageSize.width - _imageToItemHorSpace;
            }
            
            //现行中已有标签
            if (beforeView) {
                CGFloat curMaxWidth = CGRectGetMaxX(beforeView.frame);
                CGFloat curMinHeight = CGRectGetMinY(beforeView.frame);
                CGFloat curMaxHeight = CGRectGetMaxY(beforeView.frame);
                
                //现行中可以放开此标签
                if (curMaxWidth + _itemSpace + _imageSize.width + _imageToItemHorSpace + itemWidth + _InsetSpace.right <= _ViewTotalWidth) {
                    imageView.frame = CGRectMake(curMaxWidth + _itemSpace, curMinHeight, _imageSize.width, _imageSize.height);
                    
                    curMaxWidth = CGRectGetMaxX(imageView.frame);
                    
                    tagLabel.frame = CGRectMake(curMaxWidth + _imageToItemHorSpace, curMinHeight, itemWidth, itemHeight);
                    
                }
                
                //现行中放不开此标签,需另起一行
                else {
                    
                    imageView.frame = CGRectMake(_InsetSpace.left, curMaxHeight + _lineSpace, _imageSize.width, _imageSize.height);
                    
                    curMaxWidth = CGRectGetMaxX(imageView.frame);
                    
                    tagLabel.frame = CGRectMake(curMaxWidth + _imageToItemHorSpace, curMaxHeight + _lineSpace, itemWidth, itemHeight);
                }
                
                imageView.center = CGPointMake(imageView.center.x, tagLabel.center.y);
            }
            
            //现行中没有标签,一般为第一行
            else {
                imageView.frame = CGRectMake(_InsetSpace.left, _InsetSpace.top, _imageSize.width, _imageSize.height);
                
                if (_imageSize.height > itemHeight) {
                    
                    tagLabel.frame = CGRectMake(CGRectGetMaxX(imageView.frame) + _imageToItemHorSpace, _InsetSpace.top, itemWidth, _imageSize.height);
                } else {
                    tagLabel.frame = CGRectMake(CGRectGetMaxX(imageView.frame) + _imageToItemHorSpace, _InsetSpace.top, itemWidth, itemHeight);
                    
                    imageView.center = CGPointMake(imageView.center.x, tagLabel.center.y);
                }
            }
            
        }
        
        //无前面图片
        else {
            
            if (itemWidth > (_ViewTotalWidth - _InsetSpace.left - _InsetSpace.right)) {
                itemWidth = _ViewTotalWidth - _InsetSpace.left - _InsetSpace.right;
            }
            
            
            //现行中已有标签
            if (beforeView) {
                CGFloat curMaxWidth = CGRectGetMaxX(beforeView.frame);
                CGFloat curMinHeight = CGRectGetMinY(beforeView.frame);
                CGFloat curMaxHeight = CGRectGetMaxY(beforeView.frame);
                
                //现行中可以放开此标签
                if (curMaxWidth + _itemSpace + itemWidth + _InsetSpace.right <= _ViewTotalWidth) {
                    
                    tagLabel.frame = CGRectMake(curMaxWidth + _itemSpace, curMinHeight, itemWidth, itemHeight);
                    
                }
                
                //现行中放不开此标签,需另起一行
                else {
                    
                    tagLabel.frame = CGRectMake(_InsetSpace.left, curMaxHeight + _lineSpace, itemWidth, itemHeight);
                }
            }
            
            //现行中没有标签,一般为第一行
            else {
                tagLabel.frame = CGRectMake(_InsetSpace.left, _InsetSpace.top, itemWidth, itemHeight);
            }
        }
        
        beforeView = tagLabel;
    }
    
    if (beforeView) {
        _ViewTotalHeight = CGRectGetMaxY(beforeView.frame) + _InsetSpace.bottom;
    }
    
}

-(CGFloat)getViewTotalHeight{
    return _ViewTotalHeight;
}



-(void)didTapTag:(UITapGestureRecognizer *)tap{
    
    UILabel *tapLabel = (UILabel *)tap.view;
    if (_didClickTagBlock) {
        _didClickTagBlock(tapLabel.text);
    }
}

-(void)didClickOnTag:(void(^)(NSString *str))completion{
    _didClickTagBlock = [completion copy];
}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值