
有朋友让帮他写一个封装的字数不一的多标签视图,所以今天将代码展示一下,供大家学习
代码中封装了两种方法,分别是:1.传递数组,数组中是NSString类型的方法;2.传递数组,数组中是NSDictionary类型的方法
首先介绍:1.传递数组,数组中是NSString类型的方法;
//初始化数组中是NSString类型的标签视图
-(instancetype)initWithFrame:(CGRect)frame textArray:(NSArray *)aArray textFont:(UIFont *)aFont
{
self = [super initWithFrame:frame];
if (self)
{
//首先获取宽、高,确定显示的位置
CGFloat widthF = frame.size.width;
//CGFloat heightF = frame.size.height;
//然后创建标签视图,注意:这个方法传过来的数组中就是NSString类型,可以直接显示
CGFloat labelWidthF = 0.0f;
CGFloat labelHeightF = 0.0f;
for (int i = 0; i < aArray.count; i++)
{
NSString *str = aArray[i];
CGSize strSize = [self sizeWithFont:str font:aFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
//labelWidthF=labelWidthF+strSize.width+10;
//判断宽、高
if (widthF < labelWidthF+strSize.width+10)
{
labelWidthF=0.0f;
labelHeightF = labelHeightF+strSize.height+10;
}
UILabel *labelL = [[UILabel alloc] initWithFrame:CGRectMake(labelWidthF, labelHeightF, strSize.width+10, strSize.height+10)];
labelL.text = str;
labelL.textAlignment = NSTextAlignmentCenter;
labelL.backgroundColor = [UIColor lightGrayColor];
labelL.font = aFont;
[self addSubview:labelL];
labelWidthF=labelWidthF+strSize.width+10;
}
}
return self;
}
记住一定要计算控件的宽高,否则容易无法多行显示
第二种方式:2.传递数组,数组中是NSDictionary类型的方法
//初始化数组中是NSDictionary类型的标签视图
-(instancetype)initWithFrame:(CGRect)frame textArray:(NSArray *)aArray keyStr:(NSString *)aKeyStr textFont:(UIFont *)aFont
{
self = [super initWithFrame:frame];
if (self)
{
//首先获取宽、高,确定显示的位置
CGFloat widthF = frame.size.width;
//CGFloat heightF = frame.size.height;
//然后创建标签视图,注意:这个方法传过来的数组中就是NSString类型,可以直接显示
CGFloat labelWidthF = 0.0f;
CGFloat labelHeightF = 0.0f;
for (int i = 0; i < aArray.count; i++)
{
NSDictionary *dic = aArray[i];
NSString *str = dic[aKeyStr];
CGSize strSize = [self sizeWithFont:str font:aFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
//labelWidthF=labelWidthF+strSize.width+10;
//判断宽、高
if (widthF < labelWidthF+strSize.width+10)
{
labelWidthF=0.0f;
labelHeightF = labelHeightF+strSize.height+10;
}
UILabel *labelL = [[UILabel alloc] initWithFrame:CGRectMake(labelWidthF, labelHeightF, strSize.width+10, strSize.height+10)];
labelL.text = str;
labelL.textAlignment = NSTextAlignmentCenter;
labelL.backgroundColor = [UIColor lightGrayColor];
labelL.font = aFont;
[self addSubview:labelL];
labelWidthF=labelWidthF+strSize.width+10;
}
}
return self;
}
方法设定完成,进行调用
NSArray *arr1 = @[@"哈哈哈",@"呵呵",@"嘻嘻嘻嘻",@"嘿",@"么么么么么么",@"面朝大海,春暖花开",@"好好学习,天天向上,加油!"];
BHMoreLabelView *bhMoreLV1 = [[BHMoreLabelView alloc] initWithFrame:CGRectMake(0, 170, self.view.frame.size.width, 100) textArray:arr1 textFont:[UIFont systemFontOfSize:15]];
//bhMoreLV1.backgroundColor = [UIColor cyanColor];
[self.view addSubview:bhMoreLV1];
NSArray *arr2 = @[@{@"name":@"哈哈哈"},@{@"name":@"呵呵"},@{@"name":@"嘻嘻嘻嘻"},@{@"name":@"嘿"},@{@"name":@"么么么么么么"},@{@"name":@"面朝大海,春暖花开"}];
BHMoreLabelView *bhMoreLV2 = [[BHMoreLabelView alloc] initWithFrame:CGRectMake(0, 300, self.view.frame.size.width, 100) textArray:arr2 keyStr:@"name" textFont:[UIFont systemFontOfSize:15]];
//bhMoreLV2.backgroundColor = [UIColor redColor];
[self.view addSubview:bhMoreLV2];
效果展示图:
源码下载(有问题欢迎大家留言交流......):http://download.****.net/detail/hbblzjy/9665255