不定数量小标签

image
-(UIView *)text{
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, YGScreenWidth, 80)];
    footerView.backgroundColor = [UIColor whiteColor];
    NSInteger toLeft = 10;
//    NSArray *array = @[@"液晶电视",@"茶水饮品",@"电脑",@"茶水饮品",@"茶水饮品",@"茶水饮品",@"茶水饮品",@"茶水饮品",@"茶水饮品",@"设备物品",@"全面配套服务",@"价格体系公道",@"电脑",@"音响",@"电视",@"音响",@"电视",@"投影仪",@"麦克风"];
    
    NSMutableArray *array = [NSMutableArray array];
    for (int i = 0; i<self.equipsMutableArray.count; i++) {
        [array addObject:[self.equipsMutableArray[i] valueForKey:@"equipName"]];
    }
    int k = 0;
    CGFloat widthCount = 0.0f;
    int j = 0;
    for (int i = 0;i<array.count;i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:array[i] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:YGFontSizeSmallOne];
        [button.titleLabel sizeToFit];
        [button setTitleColor:colorWithDeepGray forState:UIControlStateNormal];
        [button setTitleColor:colorWithMainColor forState:UIControlStateSelected];
        button.backgroundColor = [UIColor clearColor];
        button.tag = 100+i;
        [button addTarget:self action:@selector(titleChooseChangeContentAction:) forControlEvents:UIControlEventTouchUpInside];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 24)];
        label.font = [UIFont systemFontOfSize:YGFontSizeSmallOne];
        label.text = array[i];
        label.textColor = colorWithMainColor;

        [label sizeToFit];
        
        CGFloat labeWidth = label.width+20;
        button.frame = CGRectMake(toLeft+widthCount+k*5, 10+30*j, labeWidth-10, 24) ;
        
        
        widthCount = widthCount +labeWidth;
        
        if (widthCount>(YGScreenWidth-toLeft-10-k*5)) {
            j=j+1;
            k=0;
            widthCount = 0.0f;
            button.frame = CGRectMake(toLeft+widthCount+k*5, 10+30*j, labeWidth-10, 24);
            widthCount = widthCount +labeWidth;
        }
        
        button.layer.cornerRadius = 3;
        button.layer.borderColor = colorWithLightGray.CGColor;
        button.layer.borderWidth = 0.8;
        k++;
        
        [footerView addSubview:button];
        
    }
    footerView.frame = CGRectMake(0, 30, YGScreenWidth, 10+30*(j+1));
    return footerView;
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容