绘图不会在drawRect中滚动文本

时间:2013-08-14 02:02:32

标签: ios scroll drawrect

作为iOS编程的新手,如果这个问题听起来很愚蠢,我很抱歉。 我绘制了一些rects来标记UITextView中的一些文本。唯一的问题是rects不会滚动文本。他们就呆在那里。 这是代码:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setContentMode:UIViewContentModeRedraw];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 1.0);
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    CGRect rectangle = CGRectMake(0,3.5*self.font.lineHeight,self.frame.size.width,self.font.lineHeight);
    CGContextAddRect(context, rectangle);
    CGContextStrokePath(context);
}

感谢您的帮助。

感谢格雷格的回答。我做了一点测试,以下代码完成了这个伎俩。请注意上面的代码在textView.m中,但以下代码在ViewController.m中:

- (void)viewDidLoad
{
    //other inits....
    [textView setDelegate:self];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [textView setNeedsDisplay];
}

1 个答案:

答案 0 :(得分:1)

UITextView是UIScrollView的子类。您必须检测滚动视图何时开始滚动,然后动态更新矩形的位置。您可以通过在子类上实现scrollViewDidScroll:方法并跟踪/更新rect的位置来实现此目的