Label高度自适应,伴随设置字行间距,字间距


    NSString *text = @"如果options参数为,那么整个文本将以每行组成的矩形为单位计算整个文本的尺寸。(在这里有点奇怪,因为字体高度大概是13.8,textView中大概有10行文字,此时用该选项计算出来的只有5行,即高度为69。而同时使用却可以得出文字刚好有10行,即高度为138,这里要等iOS7官方的文档出来再看看选项的说明,因为毕竟以上文档是iOS6的东西)";
    NSMutableParagraphStyle *stype = [[NSMutableParagraphStyle alloc] init];
    stype.lineSpacing = 3;
    NSDictionary *dic = @{NSParagraphStyleAttributeName:stype, NSKernAttributeName:@1.f};
    NSMutableAttributedString *att_string = [[NSMutableAttributedString alloc] initWithString:text attributes:dic];

    CGSize size = [text boundingRectWithSize:CGSizeMake(Main_Width - 100, MAXFLOAT) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14], NSParagraphStyleAttributeName:stype, NSKernAttributeName:@1.f} context:nil].size;

    label.frame = CGRectMake(50, 100, size.width, size.height);
    label.attributedText = att_string;
    [self.view addSubview:label];

这里写图片描述

猜你喜欢

转载自blog.csdn.net/renjie_Yan/article/details/81673146