Skip to content

Instantly share code, notes, and snippets.

@agibson73
Last active August 29, 2015 14:18
Show Gist options
  • Save agibson73/ee0dd4167284c335f4ae to your computer and use it in GitHub Desktop.
Save agibson73/ee0dd4167284c335f4ae to your computer and use it in GitHub Desktop.
UITextViewWithLines **Use with PSPDFTextView
// I have used this with PSPDFTextView
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
//Get the current drawing context
CGContextRef context = UIGraphicsGetCurrentContext();
//Set the line color and width
CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.2f].CGColor);
CGContextSetLineWidth(context, 1.0f);
//Start a new Path
CGContextBeginPath(context);
//Find the number of lines
NSUInteger numberOfLines = (self.contentSize.height + self.bounds.size.height) / self.font.leading;
//Set the line offset. Better way probably exists
CGFloat baselineOffset = 6.0f;
//iterate over lines
for (int x = 1; x < numberOfLines; x++) {
//0.5f offset lines up line with pixel boundary
CGContextMoveToPoint(context, self.bounds.origin.x, self.font.leading*x + 0.5f + baselineOffset);
CGContextAddLineToPoint(context, self.bounds.size.width, self.font.leading*x + 0.5f + baselineOffset);
}
//Close our Path and Stroke (draw) it
CGContextClosePath(context);
CGContextStrokePath(context);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment