标签流式布局
2016-06-28 21:32
246 查看
</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
相关文章推荐
- mysql 主键选择
- JavaSE 基础 第03节 详解第一个Java程序
- Java连接MongoDB样例
- CF 632F Magic Matrix
- BMP格式图像文件详析
- 内核级HOOK的几种实现与应用
- C++ Coding Tricks Note
- 团体程序设计天梯赛L2-001 紧急救援(最短路)
- latex字体设置
- CI框架快速入门1--目录结构分析
- 【城会玩】hrbust 1341Who will be punished【思维】
- HDU-2553 N皇后问题(暴力回溯法)
- HDU 3339 In Action 最短路+01背包
- JavaSE 基础 第02节 搭建Java开发环境
- 机器学习:正则化
- 如何重装win7
- Java回调机制
- Android利用AsyncTask异步加载网络图片,详细教程,图+代码+文字+项目
- Android获取读取内部存储-初级
- 服务级后门自己做——创建服务