-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpacingLabel.m
171 lines (129 loc) · 4.64 KB
/
SpacingLabel.m
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//
// SpacingLabel.m
//
// Created by Cyril Chandelier on 07/06/15.
// Copyright (c) 2015 Cyril Chandelier. All rights reserved.
//
#import "SpacingLabel.h"
#import <CoreText/CoreText.h>
@interface SpacingLabel ()
// Drawing
@property (nonatomic, strong) UILabel *drawingLabel;
// Fonts
@property (nonatomic, strong) UIFont *firstLineFont;
@property (nonatomic, strong) UIFont *lastLineFont;
@end
@implementation SpacingLabel
#pragma mark - View lifecycle
- (void)awakeFromNib
{
[super awakeFromNib];
// Ensure the view is not clipping bounds
self.clipsToBounds = NO;
}
- (void)reset
{
// Find biggest font of both first and last line (could be the same)
NSArray *lines = [self getLinesArrayOfStringInLabel];
if ([lines count])
{
self.firstLineFont = [self biggestFontOfLine:[lines firstObject]];
self.lastLineFont = [self biggestFontOfLine:[lines lastObject]];
}
else
{
self.firstLineFont = nil;
self.lastLineFont = nil;
}
}
#pragma mark - Drawing
- (void)drawRect:(CGRect)rect
{
[self invalidateIntrinsicContentSize];
CGRect drawingRect = [self.attributedText boundingRectWithSize:CGSizeMake(CGRectGetWidth(self.bounds), CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin)
context:nil];
self.drawingLabel.attributedText = self.attributedText;
self.drawingLabel.frame = CGRectMake(0, -(self.firstLineFont.ascender - self.firstLineFont.capHeight), CGRectGetWidth(rect), ceil(CGRectGetHeight(drawingRect)));
}
#pragma mark - Layout
- (CGSize)sizeThatFits:(CGSize)size
{
// Get the size the label should have normally
CGRect rect = [self.attributedText boundingRectWithSize:size
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
context:nil];
// Basically, the desired size is the default size minus space on top of the first line and space under the last line
return CGSizeMake(size.width, ceil(rect.size.height) - (self.firstLineFont.ascender - self.firstLineFont.capHeight) + self.lastLineFont.descender);
}
- (CGSize)intrinsicContentSize
{
CGSize sizeThatFits = [self sizeThatFits:CGSizeMake(CGRectGetWidth(self.bounds), CGFLOAT_MAX)];
return CGSizeMake(UIViewNoIntrinsicMetric, sizeThatFits.height);
}
+ (BOOL)requiresConstraintBasedLayout
{
return YES;
}
#pragma - Lazy loading
- (UILabel *)drawingLabel
{
if (!_drawingLabel)
{
_drawingLabel = [[UILabel alloc] init];
_drawingLabel.numberOfLines = self.numberOfLines;
[self addSubview:_drawingLabel];
}
return _drawingLabel;
}
#pragma mark - Setters
- (void)setHighlighted:(BOOL)highlighted
{
// Oups, this don't support highligthed state
}
- (void)setText:(NSString *)text
{
[super setText:text];
[self reset];
}
- (void)setAttributedText:(NSAttributedString *)attributedText
{
[super setAttributedText:attributedText];
[self reset];
}
#pragma mark - Utils
- (NSArray *)getLinesArrayOfStringInLabel
{
NSAttributedString *text = [self attributedText];
CGRect rect = [self frame];
CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)self.attributedText);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, CGRectMake(0, 0, rect.size.width, CGFLOAT_MAX));
CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, NULL);
NSArray *lines = (__bridge NSArray *)CTFrameGetLines(frame);
NSMutableArray *linesArray = [[NSMutableArray alloc] init];
for (id line in lines)
{
CTLineRef lineRef = (__bridge CTLineRef )line;
CFRange lineRange = CTLineGetStringRange(lineRef);
NSRange range = NSMakeRange(lineRange.location, lineRange.length);
NSAttributedString *lineString = [text attributedSubstringFromRange:range];
[linesArray addObject:lineString];
}
return (NSArray *)linesArray;
}
- (UIFont *)biggestFontOfLine:(NSAttributedString *)line
{
__block UIFont *font = nil;
[line enumerateAttribute:NSFontAttributeName
inRange:NSMakeRange(0, line.length)
options:0
usingBlock:^(UIFont *value, NSRange range, BOOL *stop) {
if (value.lineHeight > font.lineHeight)
{
font = value;
}
}];
return font;
}
@end