多个label在view中居中显示

原创 2018年04月16日 10:36:52
- (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 <#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 = [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]};
}

github:https://github.com/struggleAnt/CenterLabelOnView

巧算星期几

 巧算星期几 基姆。拉尔森基姆拥有计算机学科的博士学位。他对数据库,算法和数据结构有着浓厚的兴趣。他的联系地址是            (原文为丹麦文--译者注) 31,DK-5270,Odense ...
  • ycrao
  • ycrao
  • 2000-11-24 16:32:00
  • 8269

自定义view(五):自定义view使文字居中对齐显示

接着上一篇文章 自定义view(四):自定义view实现动态按钮 今天在自定义view中加上一个文字显示。 本以为,这个挺简单的,但是没想到文字加上后,发现不居中。设置了 mPain...
  • sinat_26710701
  • sinat_26710701
  • 2017-04-15 14:12:41
  • 541

label文字居中

height: height,//"20px"                 "line-height": height,                 autosize:true, "te...
  • u010082526
  • u010082526
  • 2017-04-11 14:36:34
  • 1202

Qt 根据Label大小无形变的自适应居中显示图像

效果图: 实现代码: QImage ScaleImage2Label(QImage qImage, QLabel* qLabel) { QImage qScaledImage; QSi...
  • chengzhongxuyou
  • chengzhongxuyou
  • 2017-06-26 22:25:52
  • 2767

asp.net中设置label.text居中

可以把label放在table中进行设置。 (注:我想把label放在页面中作为标题显示) &nbsp;       我爱你  ...
  • NETWORK0502_LIUNA
  • NETWORK0502_LIUNA
  • 2009-12-07 21:32:00
  • 2570

如何让AutoSize为true的label在picturebox上面居中显示

private void Form1_Load(object sender, EventArgs e) { PictureBox pic = new Pictu...
  • mngzilin
  • mngzilin
  • 2012-05-16 07:29:25
  • 2893

label和input垂直居中

label和input垂直居中 1. 为input元素设置vertical-align:middle; 2. 为label元素设置vertical-align:middle;display:i...
  • yipanbo
  • yipanbo
  • 2016-04-19 18:00:26
  • 10896

delphi中Label文字居中之方法

lbl_System为Label控件,按如下方法进行设置肯定OKlbl_System.AutoSize     := False;lbl_System.WordWrap  := False;lbl_S...
  • duck04551
  • duck04551
  • 2010-05-22 00:04:00
  • 7458

在TableViewCell里面放一个自定义Label

首先创建一个UILabel,然后addSubView到cell中即可 代码: UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(...
  • isammys
  • isammys
  • 2011-08-01 17:12:23
  • 4120

iOS之label居中,内容按中间两边延伸

[self.timeLabel setTextAlignment:NSTextAlignmentCenter];  [self.timeLabel setCenter:CGPointMake(1...
  • u011199592
  • u011199592
  • 2013-10-25 12:44:29
  • 1885
收藏助手
不良信息举报
您举报文章:多个label在view中居中显示
举报原因:
原因补充:

(最多只允许输入30个字)