美文网首页
iOS-标签从中间往两边布局

iOS-标签从中间往两边布局

作者: Simple_Code | 来源:发表于2019-06-04 11:00 被阅读0次
    - (void)createLabel {
        
        NSDictionary *dic = [self getFirstLabelLeft:0];
        NSInteger changeLineIndex = [dic.allKeys.firstObject integerValue];
        CGFloat rectX = [dic.allValues.firstObject floatValue];
        CGFloat rectY = 10.0;
        
        // 标签按钮
        for (int i = 0; i < self.textData.count; i ++) {
            NSString *labelText = self.textData[i];
            
            UILabel *centerLabel = [[UILabel alloc] init];
            centerLabel.font = [UIFont systemFontOfSize:10];
            centerLabel.textAlignment = NSTextAlignmentCenter;
            centerLabel.text = labelText;
            centerLabel.textColor = HYBrownColor;
            centerLabel.layer.borderColor = HYBrownColor.CGColor;
            centerLabel.layer.borderWidth = 0.5;
            centerLabel.layer.masksToBounds = YES;
            [centerLabel sizeToFit];
            
            CGFloat rectW = centerLabel.bounds.size.width + 16.0;
            
            if (i > 0) {
                // 前一个label
                NSString *foreLabelText = self.textData[i - 1];
                UILabel *foreLabel = [[UILabel alloc] init];
                foreLabel.font = [UIFont systemFontOfSize:10];
                foreLabel.textAlignment = NSTextAlignmentCenter;
                foreLabel.text = foreLabelText;
                [foreLabel sizeToFit];
                _labelWidth = foreLabel.frame.size.width + 16.0;
                
                // 判断是否是换行的icon
                if (i == changeLineIndex) {
                    rectY = rectY + 10.0 * 2 + 5;
                    NSDictionary *secondDic = [self getFirstLabelLeft:changeLineIndex];
                    rectX = [secondDic.allValues.firstObject floatValue];
                    changeLineIndex = [secondDic.allKeys.firstObject integerValue];
                } else {
                    rectX = rectX + _labelWidth + 12.0;
                }
            }
            centerLabel.frame = CGRectMake(rectX, rectY, rectW, centerLabel.bounds.size.height + 10.0/2.f);
            centerLabel.tag = i + 100;
            centerLabel.layer.cornerRadius = centerLabel.bounds.size.height/2.f;
            [self.backgroundView addSubview:centerLabel];
            
            _centerLabelBottom = CGRectGetMaxY(centerLabel.frame);
        }
        
        self.backgroundView.frame = CGRectMake(0.0, self.titleLabel.ge_maxY, [UIScreen mainScreen].bounds.size.width, _centerLabelBottom + 10.0);
    }
    
    /**
     获取每行首个label的x坐标
     
     @param index <#index description#>
     @return <#return value description#>
     */
    - (NSDictionary *)getFirstLabelLeft:(NSInteger)index {
        CGFloat labelWidth = 0.0;
        NSInteger endIndex = 0;
        for (NSInteger i = index; i < self.textData.count; i ++) {
            NSString *labelText = self.textData[i];
            
            UILabel *centerLabel = [[UILabel alloc] init];
            centerLabel.font = [UIFont systemFontOfSize:10];
            centerLabel.textAlignment = NSTextAlignmentCenter;
            centerLabel.text = labelText;
            centerLabel.textColor = [UIColor brownColor];
            centerLabel.layer.borderColor = HYBrownColor.CGColor;
            centerLabel.layer.borderWidth = 0.5;
            centerLabel.layer.cornerRadius = 2;
            centerLabel.layer.masksToBounds = YES;
            [centerLabel sizeToFit];
            
            labelWidth += centerLabel.bounds.size.width + 16.0 + 12.0;
            
            if (labelWidth >= [UIScreen mainScreen].bounds.size.width - 10.0 * 6) {
                labelWidth = labelWidth - centerLabel.bounds.size.width - 16.0 - 12.0;
                endIndex = i;
                break ;
            }
        }
        
        return @{[NSString stringWithFormat:@"%zd", endIndex] : [NSNumber numberWithFloat:([UIScreen mainScreen].bounds.size.width - labelWidth) / 2.0]};
    }
    
    

    相关文章

      网友评论

          本文标题:iOS-标签从中间往两边布局

          本文链接:https://www.haomeiwen.com/subject/anklxctx.html