OGeek|极客世界-中国程序员成长平台

标题: ios - 绘图不会随着drawRect中的文本滚动 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:36
标题: ios - 绘图不会随着drawRect中的文本滚动

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

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

- (void)drawRectCGRect)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)scrollViewDidScrollUIScrollView *)scrollView {
    [textView setNeedsDisplay];
}



Best Answer-推荐答案


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

关于ios - 绘图不会随着drawRect中的文本滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18222076/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4