뷰에서 여러 개의 label 가운데 표시

4098 단어
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.textData = @[@"OC", @"Swift", @"      ", @"       ", @"        ", @"         ", @" ", @"           ", @"                      ", @"   ", @"wawahaha", @"ok"];
    
    self.backgroundView = [[UIView alloc] init];
    self.backgroundView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.backgroundView];
    
    [self createLabel];
}

- (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 = [UIColor brownColor];
        centerLabel.layer.borderColor = [UIColor whiteColor].CGColor;
        centerLabel.layer.borderWidth = 0.5;
        centerLabel.layer.cornerRadius = 2;
        centerLabel.layer.masksToBounds = YES;
        [centerLabel sizeToFit];
        
        CGFloat rectW = centerLabel.bounds.size.width + 6.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 + 6.0;
            
            //         icon
            if (i == changeLineIndex) {
                rectY = rectY + 10.0 * 2;
                NSDictionary *secondDic = [self getFirstLabelLeft:changeLineIndex];
                rectX = [secondDic.allValues.firstObject floatValue];
                changeLineIndex = [secondDic.allKeys.firstObject integerValue];
            } else {
                rectX = rectX + _labelWidth + 4.0;
            }
        }
        centerLabel.frame = CGRectMake(rectX, rectY, rectW, centerLabel.bounds.size.height + 10.0 / 2);
        centerLabel.tag = i + 100;
        [self.backgroundView addSubview:centerLabel];
        
        _centerLabelBottom = CGRectGetMaxY(centerLabel.frame);
    }
    
    self.backgroundView.frame = CGRectMake(0.0, 50.0, [UIScreen mainScreen].bounds.size.width, _centerLabelBottom + 10.0);
}

/**
       label x  

 @param index 
 @return 
 */
- (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 = [UIColor whiteColor].CGColor;
        centerLabel.layer.borderWidth = 0.5;
        centerLabel.layer.cornerRadius = 2;
        centerLabel.layer.masksToBounds = YES;
        [centerLabel sizeToFit];
        
        labelWidth += centerLabel.bounds.size.width + 6.0 + 4.0;
        
        if (labelWidth >= [UIScreen mainScreen].bounds.size.width - 10.0 * 6) {
            labelWidth = labelWidth - centerLabel.bounds.size.width - 6.0 - 4.0;
            endIndex = i;
            break ;
        }
    }
    
    return @{[NSString stringWithFormat:@"%ld", endIndex] : [NSNumber numberWithFloat:([UIScreen mainScreen].bounds.size.width - labelWidth) / 2.0]};
}

좋은 웹페이지 즐겨찾기