JLChen
2021-10-08 f8457b624a75bf8e41489b74f66009eee6891b8b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//
//  Copyright © 2016 dahua. All rights reserved.
//
 
#import "UILabel+LeChange.h"
#import<CoreText/CoreText.h>
 
@implementation UILabel(LeChange)
 
#pragma mark - Add red dot
 
- (void)addRedDot
{
    NSString *content = [NSString stringWithFormat:@"%@•", self.text];
    
    if (content == nil) {
        return;
    }
    
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:content];
    NSRange range = NSMakeRange(content.length - 1, 1);
    [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
    [string addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:18] range:range];
    
    self.attributedText = string;
}
 
- (void)lc_setAttributedText:(NSString*)text textSize:(CGFloat)textSize lineSpace:(CGFloat)lineSpace {
    
    if (text == nil) {
        return;
    }
    
    NSMutableAttributedString *content = [[NSMutableAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:textSize]}];
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    //行间距
    [paragraphStyle setLineSpacing:lineSpace];
    [content addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [content length])];
    
    [self setAttributedText:content];
}
 
+ (CGFloat)lc_heightOfAttributedText:(NSString*)text textSize:(CGFloat)textSize
                           lineSpace:(CGFloat)lineSpace width:(CGFloat)width {
    
    if (text == nil) {
        return 0;
    }
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:textSize]}];
    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    //行间距
    [paragraphStyle setLineSpacing:lineSpace];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attributedString length])];
    
    CGFloat total_height = 0;
    
    //string 为要计算高度的NSAttributedString
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attributedString);
    CGRect drawingRect = CGRectMake(0, 0, width, 100000);
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, drawingRect);
    CTFrameRef textFrame = CTFramesetterCreateFrame(framesetter,CFRangeMake(0,0), path, NULL);
    CGPathRelease(path);
    CFRelease(framesetter);
    
    NSArray *linesArray = (NSArray *) CTFrameGetLines(textFrame);
    
    CGPoint origins[[linesArray count]];
    CTFrameGetLineOrigins(textFrame, CFRangeMake(0, 0), origins);
    
    //最后一行line的原点y坐标
    int line_y = (int) origins[[linesArray count] -1].y;
    CGFloat ascent;
    CGFloat descent;
    CGFloat leading;
    
    CTLineRef line = (__bridge CTLineRef) [linesArray objectAtIndex:[linesArray count]-1];
    CTLineGetTypographicBounds(line, &ascent, &descent, &leading);
    
    //+1为了纠正descent转换成int小数点后舍去的值
    total_height = 100000 - line_y + descent +1;
    CFRelease(textFrame);
    
    return total_height;
}
 
@end