宽容他人,放过自己。

label的长度和宽度的自适应

Posted on By anchoriteFili

071554335803872.png

/**
     label的宽度:label.text.length * label.font
     label的高度就是字号
     sizeToFit方法可以实现有特殊符号的或字母的时候label后边也不回留大片空白
     */
    
#pragma mark 并排两个label间距
    UILabel *label = [[UILabel alloc] init];
    label.text = @"fafa哈哈哈哈/////哈啊。。。。";
    label.font = [UIFont systemFontOfSize:12];
    CGFloat width = label.text.length*12;
    label.frame = CGRectMake(0, 100, width, 20);
    label.backgroundColor = [UIColor greenColor];
    [label sizeToFit]; //防止label后边留有空白
    [self.view addSubview:label];
    
    UILabel *labelOne = [[UILabel alloc] init];
    labelOne.text = @"sdasfafa哈哈哈";
    labelOne.font = [UIFont systemFontOfSize:12];
    CGFloat widthOne = labelOne.text.length*12;
    labelOne.frame = CGRectMake(label.frame.size.width+12, 100, widthOne, 20);
    labelOne.backgroundColor = [UIColor greenColor];
    [labelOne sizeToFit];
    [self.view addSubview:labelOne];