Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes crash when highlighted text changed #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions MDHTMLLabel/MDHTMLLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ @implementation MDHTMLLabel
@private
NSAttributedString *_htmlAttributedText;
BOOL _needsFramesetter;
BOOL _needsHighlightFramesetter; // Fixes crash when highlighted text changed
CTFramesetterRef _framesetter;
CTFramesetterRef _highlightFramesetter;
}
Expand Down Expand Up @@ -470,6 +471,7 @@ - (void)setHtmlAttributedText:(NSAttributedString *)htmlAttributedText
- (void)setNeedsFramesetter
{
_needsFramesetter = YES;
_needsHighlightFramesetter = YES;
}

- (CTFramesetterRef)framesetter
Expand Down Expand Up @@ -630,12 +632,13 @@ - (void)drawTextInRect:(CGRect)rect
[highlightAttributedString addAttribute:(__bridge NSString *)kCTForegroundColorAttributeName
value:(id)self.highlightedTextColor.CGColor
range:NSMakeRange(0, highlightAttributedString.length)];

if (!self.highlightFramesetter)
if (!self.highlightFramesetter ||_needsHighlightFramesetter)
{
CTFramesetterRef highlightFramesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)highlightAttributedString);
[self setHighlightFramesetter:highlightFramesetter];
CFRelease(highlightFramesetter);
_needsHighlightFramesetter = NO;
}

[self drawFramesetter:self.highlightFramesetter attributedString:highlightAttributedString textRange:textRange inRect:textRect context:c];
Expand Down Expand Up @@ -1510,6 +1513,13 @@ - (void)setHighlighted:(BOOL)highlighted
[self setNeedsDisplay];
}

- (void)setHighlightedTextColor:(UIColor *)highlightedTextColor
{
[super setHighlightedTextColor:highlightedTextColor];
[self setNeedsFramesetter];
[self setNeedsDisplay];
}

// Fixes crash when loading from a UIStoryboard
- (UIColor *)textColor
{
Expand Down