0
votes

This may seem a bit odd, but I have a custom textView I'm creating with a clear background. In addition, I have a custom scroll view I'm creating with horizontal colored lines, and setting this scrollview below the textView. The reason why I'm not adding the lines directly in the textView (which I did do initially) is because I have another view 'sandwhiched' between the top textView, and above the scrollView. So the heirarchy looks like this: Top--->UITextView, Middle--->UIView, Bottom---> UIScrollView.

All of that works perfectly, except that I cannot seem to get the bottom scrollview to scroll with the textView, because the scrollView's content size isn't the same as the textView's and therefore no additional lines are being drawn within the scrollView. I even tried setting the content size of the scrollView to that of the textView, but that doesn't work either.

I'd appreciate any help and tips. I'll add in the code I'm using for creating the custom scrollview.

Code:

LineScrollView.h:

#import <UIKit/UIKit.h>

@interface LineScrollView : UIScrollView

@end

LineScrollView.m:

#import "LineScrollView.h"

@implementation LineScrollView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        self.userInteractionEnabled = NO;
        self.scrollsToTop = NO;
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [UIColor colorWithRed:(95.0f/255.0f) green:(197.0f/255.0f) blue:(236.0f/255.0f) alpha:0.8f].CGColor);
    CGContextSetLineWidth(context, 1.2f);
    CGContextBeginPath(context);

    // Below lineHeight value determined using NSLog on my custom textView    

    CGFloat lineHeight = 21.473999;
    NSUInteger numberOfLines = (self.contentSize.height + self.bounds.size.height) / lineHeight;

    CGFloat baselineOffset = 1.2f;

    for (int x = 1; x < numberOfLines; x++)
    {
        CGContextMoveToPoint(context, self.bounds.origin.x, (lineHeight+9)*x - baselineOffset);
        CGContextAddLineToPoint(context, self.bounds.size.width, (lineHeight+9)*x - baselineOffset);
    }

    CGContextClosePath(context);
    CGContextStrokePath(context);
}

@end

ViewController:

#import "TextView.h"
#import "LineScrollView.h"

@property (nonatomic, strong) TextView *TextView;
@property (nonatomic, strong) UIView *sandWhichedView;
@property (nonatomic, strong) LineScrollView *LineView;

- (void)viewDidLoad
{
   [super viewDidLoad];

   self.sandWhichedView = [[UIView alloc]initWithFrame:initWithFrame:CGRectMake(41.5, 35, 278, 58)];

   self.TextView = [[TextView alloc] initWithFrame:CGRectMake(41.5, 35, 278, 58)];

    self.LineView = [[LineScrollView alloc]initWithFrame:CGRectMake(41.5, 35, 279, 58)];

    self.LineView.delegate = self;

    self.TextView.delegate = self;

    [self.view addSubview:self.LineView];
    [self.view addSubview:self.sandWhichedView];
    [self.view addSubview:self.TextView];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
   if (scrollView == self.TextView)
   {   
       self.LineView.contentSize = self.TextView.contentSize;
       CGPoint offset = self.TextView.contentOffset;
       offset.y = self.TextView.contentOffset.y;
       [self.LineView setContentOffset:offset];
   }
}
1

1 Answers

0
votes

Looks like I found the solution myself, I forgot to add this line in my custom scrollView:

self.contentMode = UIViewContentModeRedraw;

Literally solved all my headaches.