對于項(xiàng)目中熱詞以及歷史記錄的需求做個封裝,可動態(tài)添加設(shè)置標(biāo)簽步绸,配置好需要配置的參數(shù)即可奸腺。
設(shè)置以及初始化方法如下:
@interface LXTagsView : UIView
typedef void(^itemClickBlock) (NSInteger index);
-(instancetype)initWithFrame:(CGRect)frame ItemClick:(itemClickBlock)click;
@property(nonatomic,strong)UIFont *btnFont;//先賦值脉执;
@property(nonatomic,assign)CGFloat tagInsetSpace;//標(biāo)簽內(nèi)間距 (左右各間距)
@property(nonatomic,assign)CGFloat tagsLineSpace;//標(biāo)簽行間距
@property(nonatomic,assign)CGFloat tagsMargin;//標(biāo)簽之間的間距
@property(nonatomic,assign)CGFloat tagSpace;// 整體左右邊距
@property(nonatomic,strong)NSArray *tagsArray; // 文字標(biāo)簽數(shù)組
@property(nonatomic,assign)CGFloat totalH; //返回總高度
使用方法如下:
LXWS(weakSelf);
self.tagsView =[[LXTagsView alloc]initWithFrame:CGRectMake(0, 100, 300, 40) ItemClick:^(NSInteger index) {
NSLog(@"%ld",index);
UIViewController *vc =[[UIViewController alloc]init];
vc.view.backgroundColor = LXRandomColor;
[weakSelf.navigationController pushViewController:vc animated:YES];
}];
self.tagsView.btnFont =[UIFont systemFontOfSize:16];
self.tagsView.tagSpace = 10;
self.tagsView.tagsMargin = 5;
self.tagsView.tagInsetSpace = 15;
self.tagsView.tagsLineSpace =10;
self.tagsView.tagsArray = @[@"無知安徽噶人噶人家噶加熱機(jī)噶進(jìn)入國阿嘎熱噶人噶熱狗如果",@"風(fēng)云變幻",@"施耐庵",@"唉",@"西門吹雪",@"呵呵噠你沒打答題的啊啊噶而過阿哥人argergaergaergag阿嘎人家居然就噶間距根據(jù)",@"他大舅他二舅都是他就",@"窿窿啦啦",@"火麒麟",@"合歡花",@"暴走大事件",@"非誠勿擾",@"呵呵呵",@"miss",@"我愛你",@"thelife",@"永生",@"不忘",@"你",@"愛好個人拉人給哈爾和老公哈拉爾掛了會考",@"愛人噶人更好惹過哈兒噶爾 ",@"愛人桿兒隔熱管",@"愛人跟人"];
[self.view addSubview:self.tagsView];
.m如下:
#import "LXTagsView.h"
#import "UIColor+Expanded.h"
@interface LXTagsView()
@property(nonatomic,copy)itemClickBlock itemBlock;
@property(nonatomic,strong)NSMutableArray *tagsFrames;
@end
@implementation LXTagsView
-(instancetype)initWithFrame:(CGRect)frame ItemClick:(itemClickBlock)click{
self = [super initWithFrame:frame];
if (self) {
self.itemBlock = click;
_tagsLineSpace = 5;
_tagsMargin = 10;
_tagInsetSpace = 10;
_tagSpace = 10;
_totalH = 0;
}
return self;
}
-(void)setTagsArray:(NSArray *)tagsArray{
_tagsArray = tagsArray;
[self configTagsFrames];
[self setupUI];
}
#pragma mark--- 設(shè)置frame ---
-(void)configTagsFrames{
CGFloat orignHMargin = _tagSpace;// 水平方向左邊距
CGFloat orignVerMargin = 5;//上邊距參考父視圖
CGFloat btnH = 25;
_totalH = 0;
[self.tagsFrames removeAllObjects];
for (int i = 0; i< _tagsArray.count; i++) {
NSString *string = _tagsArray[i];
CGFloat btnW = [self stringSizeWithFont:_btnFont string:string height:btnH].width + 2*_tagInsetSpace;
//增加個判斷塞绿,當(dāng)字符串過長是惨撇,超過屏幕總寬度- 兩側(cè)間距虐秦,重置標(biāo)簽寬度
if (btnW + _tagSpace *2 >= KScreenW) {
btnW = KScreenW - 2 *_tagSpace;
}
if ( orignHMargin + btnW + _tagSpace > KScreenW) {
orignVerMargin = orignVerMargin + btnH + _tagsLineSpace;
orignHMargin = _tagSpace;
}
CGRect frame= CGRectMake(orignHMargin, orignVerMargin, btnW, btnH);
[self.tagsFrames addObject:NSStringFromCGRect(frame)];
//判斷是 最后一個標(biāo)簽的時(shí)候保存其高度;
if (i == _tagsArray.count -1) {
_totalH = orignVerMargin + btnH +10;
}
orignHMargin = orignHMargin + btnW +_tagsMargin;
}
//設(shè)置整體高度
self.height = _totalH;
}
#pragma mark---設(shè)置UI--
-(void)setupUI{
[self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[obj removeFromSuperview];
}];
for (int i = 0; i< _tagsFrames.count; i++) {
CGRect frame = CGRectFromString(_tagsFrames[i]);
LxButton *button =[LxButton LXButtonWithTitle:_tagsArray[i] titleFont:_btnFont Image:nil backgroundImage:nil backgroundColor:[UIColor hexStringToColor:@"f5f5f5"] titleColor:[UIColor blackColor] frame:frame];
//對于寬度的處理
if (frame.size.width == KScreenW - 2 *_tagSpace) {
[button setTitleEdgeInsets:UIEdgeInsetsMake(0, _tagInsetSpace, 0, _tagInsetSpace)];
}
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:button.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(frame.size.height/2, frame.size.height/2)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = button.bounds;
maskLayer.path = maskPath.CGPath;
button.layer.mask = maskLayer;
button.buttonID = i;
[button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
}
}
-(void)btnClick:(LxButton *)button
{
if (_itemBlock) {
_itemBlock(button.buttonID);
}
}
-(void)setBtnFont:(UIFont *)btnFont
{
_btnFont = btnFont;
}
-(void)setTagInsetSpace:(CGFloat)tagInsetSpace
{
_tagInsetSpace = tagInsetSpace;
}
-(void)setTagsLineSpace:(CGFloat)tagsLineSpace
{
_tagsLineSpace = tagsLineSpace;
}
-(void)setTagsMargin:(CGFloat)tagsMargin
{
_tagsMargin = tagsMargin;
}
-(void)setTagSpace:(CGFloat)tagSpace
{
_tagSpace = tagSpace;
}
-(NSMutableArray *)tagsFrames{
if (!_tagsFrames) {
_tagsFrames =[NSMutableArray array];
}
return _tagsFrames;
}
#pragma mark---動態(tài)高度---
-(CGSize)stringSizeWithFont:(UIFont *)font string:(NSString *)string height:(CGFloat)height
{
CGRect rect =[string boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, height) options: NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
return rect.size;
}
@end
demo 地址:熱詞搜索