I think this works fine, but I feel like it has been hacked, and I have not fully disclosed the mechanism of the UITextView class;
you must first add the following to your delegate to force the scroll to be redrawn
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
then we implement drawRect in a subclass, so
- (void)drawRect:(CGRect)rect { // Drawing code // Get the graphics context CGContextRef ctx = UIGraphicsGetCurrentContext(); [super drawRect:rect]; // Get the height of a single text line NSString *alpha = @"ABCD"; CGSize textSize = [alpha sizeWithFont:self.font constrainedToSize:self.contentSize lineBreakMode:UILineBreakModeWordWrap]; NSUInteger height = textSize.height; // Get the height of the view or contents of the view whichever is bigger textSize = [self.text sizeWithFont:self.font constrainedToSize:self.contentSize lineBreakMode:UILineBreakModeWordWrap]; NSUInteger contentHeight = (rect.size.height > textSize.height) ? (NSUInteger)rect.size.height : textSize.height; NSUInteger offset = 6 + height; // MAGIC Number 6 to offset from 0 to get first line OK ??? contentHeight += offset; // Draw ruled lines CGContextSetRGBStrokeColor(ctx, .8, .8, .8, 1); for(int i=offset;i < contentHeight;i+=height) { CGPoint lpoints[2] = { CGPointMake(0, i), CGPointMake(rect.size.width, i) }; CGContextStrokeLineSegments(ctx, lpoints, 2); } }
Still worried about this Magic Number 6
Bean
source share