Skip to content

Instantly share code, notes, and snippets.

Last active August 29, 2015 14:18
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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
//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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment