iOS-设置文本行高并计算高度

时间:2023-02-01 08:34:21

通过NSMutableParagraphStyle可以设置行与行之间的间隔,代码如下:

NSMutableAttributedString *string1 = [[NSMutableAttributedString alloc]initWithString:string];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    [paragraphStyle setLineSpacing:4];
    [string1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,string.length)];
    CGRect sizestring = [string boundingRectWithSize:CGSizeMake(kScreen_Width - 30,1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:FontSize(14),NSParagraphStyleAttributeName:paragraphStyle} context:nil];