Created
April 2, 2015 14:52
-
-
Save odrobnik/70b62ec803c49aaa7169 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)drawRect:(CGRect)rect | |
{ | |
[super drawRect:rect]; | |
CGRect lastLineFragmentRect = CGRectZero; | |
CGFloat lastBaselineOrigin = 0; | |
CGFloat lineY = 0; | |
CGFloat lineHeight = 0; | |
for (NSInteger i=0; i<self.text.length; i++) | |
{ | |
NSUInteger glyphIndex = [self.layoutManager glyphIndexForCharacterAtIndex:i]; | |
CGRect lineFragmentRect = [self.layoutManager lineFragmentRectForGlyphAtIndex:glyphIndex effectiveRange:NULL]; | |
lineFragmentRect.origin.y += self.textContainerInset.top; | |
lineFragmentRect.origin.x += self.textContainerInset.left; | |
if (CGRectEqualToRect(lineFragmentRect, lastLineFragmentRect)) | |
{ | |
continue; | |
} | |
CGFloat baselineOrigin = CGRectGetMaxY(lineFragmentRect)+self.font.descender; | |
if (baselineOrigin != lastBaselineOrigin) | |
{ | |
if (lineY) | |
{ | |
lineHeight = baselineOrigin - lastBaselineOrigin; | |
break; | |
} | |
else | |
{ | |
lineY = baselineOrigin; | |
} | |
} | |
lastLineFragmentRect = lineFragmentRect; | |
lastBaselineOrigin = baselineOrigin; | |
} | |
if (!lineHeight) | |
{ | |
lineHeight = self.font.lineHeight; | |
} | |
if (!lineY) | |
{ | |
lineY = self.textContainerInset.top + self.font.ascender; | |
} | |
// draw lines in dirty area | |
CGContextRef context = UIGraphicsGetCurrentContext(); | |
CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.2f].CGColor); | |
CGContextSetLineWidth(context, 1.0f); | |
CGContextBeginPath(context); | |
while (lineY < CGRectGetMaxY(rect)) | |
{ | |
if (lineY< CGRectGetMinX(rect)) | |
{ | |
// optimize, this line is not visible | |
continue; | |
} | |
CGContextMoveToPoint(context, self.bounds.origin.x, lineY); | |
CGContextAddLineToPoint(context, self.bounds.size.width, lineY); | |
lineY += lineHeight; | |
} | |
CGContextClosePath(context); | |
CGContextStrokePath(context); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment