使用NSMutableParagraphStyle 的 paragraphSpacingBefore属性设置富文本的段落间距

注意: paragraphSpacingBefore 属性必须和换行符'\n'配合使用才能起作用 

    self.label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 100, 120)];;
    self.label.numberOfLines = 0;
    [self.view addSubview:self.label];
    NSMutableAttributedString *attributed = [[NSMutableAttributedString alloc] initWithString:@"哈哈哈哈哈哈哈3423p4ou123piuppoiu" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[UIColor redColor]}];

    self.label.attributedText = attributed;
    self.label.backgroundColor = [UIColor cyanColor];
    
    self.label2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 300, 100, 220)];
    self.label2.numberOfLines = 0;
    [self.view addSubview:self.label2];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineBreakMode = NSLineBreakByTruncatingTail;
    style.paragraphSpacingBefore = 40;
    NSAttributedString *attributedst = [[NSAttributedString alloc] initWithString:@"\n哈哈哈哈哈哈哈3423p4ou123piuppoiu" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[UIColor redColor],NSParagraphStyleAttributeName:style}];
    [attributed appendAttributedString:attributedst];
    self.label2.backgroundColor = [UIColor greenColor];
    self.label2.attributedText = attributed;

效果图

猜你喜欢

转载自blog.csdn.net/LIUXIAOXIAOBO/article/details/115001853