Skip to content

Commit

Permalink
[attributedlabel] Avoid crashes when calculating size of nil attribut…
Browse files Browse the repository at this point in the history
…ed string.
  • Loading branch information
jverkoey committed Nov 7, 2012
1 parent e245a7a commit 8f0e1ef
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/attributedlabel/src/NIAttributedLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,24 @@
static const CGFloat kTouchGutter = 22;

CGSize NISizeOfAttributedStringConstrainedToSize(NSAttributedString *attributedString, CGSize size, NSInteger numberOfLines) {
if (nil == attributedString) {
return CGSizeZero;
}

CFAttributedStringRef attributedStringRef = (__bridge CFAttributedStringRef)attributedString;
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attributedStringRef);
CFRange range = CFRangeMake(0, 0);

// This logic adapted from @mattt's TTTAttributedLabel
// https://github.com/mattt/TTTAttributedLabel

if (numberOfLines > 0) {
if (numberOfLines > 0 && nil != framesetter) {
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, CGRectMake(0, 0, size.width, size.height));
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
CFArrayRef lines = CTFrameGetLines(frame);

if (CFArrayGetCount(lines) > 0) {
if (nil != lines && CFArrayGetCount(lines) > 0) {
NSInteger lastVisibleLineIndex = MIN(numberOfLines, CFArrayGetCount(lines)) - 1;
CTLineRef lastVisibleLine = CFArrayGetValueAtIndex(lines, lastVisibleLineIndex);

Expand Down

0 comments on commit 8f0e1ef

Please sign in to comment.