寬高自適應的標籤tips
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self createView];
}
returnself;
}
- (void)createView{
self.backgroundColor = [UIColorwhiteColor];
self.titleLabel = [[UILabelalloc] initWithFrame:CGRectMake(15, 10, 100, 30)];
self.titleLabel.textColor = [
self.titleLabel.text = @"Knowledge";
[selfaddSubview:self.titleLabel];
///
self.line = [[UIViewalloc] initWithFrame:CGRectMake(0, 50, self.frame.size.width, 0.5)];
[selfaddSubview:self.line];
self.line.backgroundColor = [UIColorlightGrayColor];
///
self.knowledgeScroll = [[UIScrollViewalloc]
[selfaddSubview:self.knowledgeScroll];
NSMutableArray *arr = [NSMutableArrayarrayWithObjects:@"語文",@"的方化學阿化學阿傳送到傳送到傳送到傳送到式發順豐阿傳送到傳送到傳送到傳送到式發順豐數學數學",@"阿達",@"化學阿傳送到傳送到",@"物理搜尋",@"生物",@"地理",@"的化學阿傳送到傳送到方式發順化學阿傳送到傳送到化學阿傳送到傳送到豐數學",
[selfrefreshWithArr:arr];
}
- (void)refreshWithArr:(NSMutableArray *)arr{
/// 標籤與標籤的間距
CGFloat magin = 5;
CGFloat y = 10;
CGFloat x = 15;
CGFloat current_w = 0;
UIView *current = nil;
if (arr.count <= 0) {
}else {
for (int i = 0; i < arr.count; i++) {
NSString *str = arr[i];
NSMutableDictionary *fontDic=[NSMutableDictionarydictionaryWithObjectsAndKeys:[UIFontsystemFontOfSize:15],NSFontAttributeName,nil];
/// 一行的標準高度
CGFloat h1 = [@"aa"boundingRectWithSize:CGSizeMake(Width-30-20, 0) options:NSStringDrawingUsesLineFragmentOriginattributes:fontDic context:nil].size.height;
CGFloat w = 0;
/// 先固定寬算高度
CGFloat h = [str boundingRectWithSize:CGSizeMake(Width-30-20, 0) options:NSStringDrawingUsesLineFragmentOriginattributes:fontDic context:nil].size.height;
if (h>h1) {
/// 高於一行 高為h, 寬為 Width-30-20
w = Width-30-20;
}else{
/// 不高於一行 固定高算寬
w = [str boundingRectWithSize:CGSizeMake(0, h1) options:NSStringDrawingUsesLineFragmentOriginattributes:fontDic context:nil].size.width;
///高為h1, 寬為 w
}
if (Width < w+20+current_w+magin+x) {
/// 一行的標籤寬和間隙總和大於螢幕寬,,進行換行
x = 15;
y = y+h+13+magin;
}else{
/// 一行的標籤寬和間隙總和不大於螢幕寬
if (i != 0) {
x = current.frame.origin.x+current.frame.size.width+magin;
}
}
UIView *view = [[UIView alloc] init];
if (h>h1) {
// 多行顯示
view.frame = CGRectMake(x, y-(h/h1-1)*h1, w+20, h + 13);
}else{
// 一行顯示
view.frame = CGRectMake(x, y, w+20, h + 13);
}
[self.knowledgeScroll addSubview:view];
view.layer.cornerRadius = 5;
view.layer.masksToBounds = YES;
view.backgroundColor = [UIColororangeColor];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 6.5, w, h)];
title.text = [NSString stringWithFormat:@"%@",str];
title.textColor = [UIColor whiteColor];
title.numberOfLines = 0;
title.textAlignment = NSTextAlignmentLeft;
[view addSubview:title];
title.font = [UIFont systemFontOfSize:15];
current = view;
current_w = current.frame.origin.x+current.frame.size.width;
CGFloat height = y;
if (current.frame.size.height > 0) {
height = current.frame.origin.y+current.frame.size.height;
}
if (height+10 < self.frame.size.height) {
self.knowledgeScroll.contentSize = CGSizeMake(Width, self.frame.size.height-50);
}else {
self.knowledgeScroll.contentSize = CGSizeMake(Width, height+10);
}
}
}
}