使用自定义UITextView自定义UIScrollView滚动

时间:2014-01-27 03:24:07

标签: ios ios7 uiscrollview uitextview

这可能看起来有点奇怪,但我有一个自定义textView我正在创建一个清晰的背景。另外,我有一个自定义滚动视图,我用水平彩色线创建,并在textView下面设置此滚动视图。我之所以没有直接在textView中添加行(我最初做过)是因为我在顶部textView和scrollView之上有另一个视图'sandwhiched'。所以heirarchy看起来像这样:Top ---> UITextView,Middle ---> UIView,Bottom --->的UIScrollView。

所有这一切都很完美,除了我似乎无法使用textView滚动底部scrollview,因为scrollView的内容大小与textView不同,因此在scrollView中没有绘制其他行。我甚至尝试将scrollView的内容大小设置为textView的内容大小,但这也不起作用。

我很感激任何帮助和提示。我将添加我用于创建自定义scrollview的代码。

代码:

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 个答案:

答案 0 :(得分:0)

看起来我自己找到了解决方案,我忘了在我的自定义scrollView中添加这一行:

self.contentMode = UIViewContentModeRedraw;

直截了当地解决了我所有的麻烦。