Skip to content

Commit

Permalink
Include -attributedTruncationToken in -renderedAttributedText. Sw…
Browse files Browse the repository at this point in the history
…itch back to using `-framesetter` in `-sizeThatFits:`
  • Loading branch information
dillan committed Nov 17, 2016
1 parent c95c754 commit 4279e3d
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions TTTAttributedLabel/TTTAttributedLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,14 @@ - (void)setAttributedText:(NSAttributedString *)text {

- (NSAttributedString *)renderedAttributedText {
if (!_renderedAttributedText) {
self.renderedAttributedText = NSAttributedStringBySettingColorFromContext(self.attributedText, self.textColor);
NSMutableAttributedString *fullString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];

if (self.attributedTruncationToken) {
[fullString appendAttributedString:self.attributedTruncationToken];
}

NSAttributedString *string = [[NSAttributedString alloc] initWithAttributedString:fullString];
self.renderedAttributedText = NSAttributedStringBySettingColorFromContext(string, self.textColor);
}

return _renderedAttributedText;
Expand Down Expand Up @@ -1321,20 +1328,11 @@ - (CGSize)sizeThatFits:(CGSize)size {
if (!self.attributedText) {
return [super sizeThatFits:size];
} else {
NSMutableAttributedString *fullString = [[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText];
NSAttributedString *string = [self renderedAttributedText];

if (self.attributedTruncationToken) {
[fullString appendAttributedString:self.attributedTruncationToken];
}

NSAttributedString *string = [[NSAttributedString alloc] initWithAttributedString:fullString];
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)string);

CGSize labelSize = CTFramesetterSuggestFrameSizeForAttributedStringWithConstraints(framesetter, string, size, (NSUInteger)self.numberOfLines);
CGSize labelSize = CTFramesetterSuggestFrameSizeForAttributedStringWithConstraints([self framesetter], string, size, (NSUInteger)self.numberOfLines);
labelSize.width += self.textInsets.left + self.textInsets.right;
labelSize.height += self.textInsets.top + self.textInsets.bottom;

CFRelease(framesetter);

return labelSize;
}
Expand Down

0 comments on commit 4279e3d

Please sign in to comment.