菜鸟教程小白 发表于 2022-12-12 13:36:26

ios - 绘图不会随着drawRect中的文本滚动


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

<pre><code>- (id)initWithFrame:(CGRect)frame
{
    self = ;
    if (self) {
      ;
    }
    return self;
}

- (void)drawRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 1.0);
    CGContextSetStrokeColorWithColor(context, .CGColor);
    CGRect rectangle = CGRectMake(0,3.5*self.font.lineHeight,self.frame.size.width,self.font.lineHeight);
    CGContextAddRect(context, rectangle);
    CGContextStrokePath(context);
}
</code></pre>

<p>感谢您的帮助。</p>

<p>感谢格雷格的回答。我做了一些测试,下面的代码起到了作用。请注意,上面的代码在 textView.m 中,但以下代码在 ViewController.m 中:</p>

<pre><code>- (void)viewDidLoad
{
    //other inits....
    ;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    ;
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>UITextView 是 UIScrollView 的子类。您必须检测 ScrollView 何时开始滚动,然后动态更新矩形的位置。您可以通过在子类上实现 scrollViewDidScroll: 方法并跟踪/更新矩形的位置来做到这一点</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 绘图不会随着drawRect中的文本滚动,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/18222076/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/18222076/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 绘图不会随着drawRect中的文本滚动