Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 高效地绘制线号+;带UITextView的光标_Ios_Objective C_Uitextfield_Uikit - Fatal编程技术网

Ios 高效地绘制线号+;带UITextView的光标

Ios 高效地绘制线号+;带UITextView的光标,ios,objective-c,uitextfield,uikit,Ios,Objective C,Uitextfield,Uikit,我正在尝试实现类似于代码编辑器的东西。我的工作是建立在这个基础上的。除了行号和光标绘制之外,一切正常 首先,它有一个bug,它不会擦除以前的光标位置,这会导致绘制多个光标,如下所示: 以下是与此相关的代码: - (void)drawRect:(CGRect)rect { // Drag the line number gutter background. The line numbers them selves are drawn by LineNumberLayoutManager. C

我正在尝试实现类似于代码编辑器的东西。我的工作是建立在这个基础上的。除了行号和光标绘制之外,一切正常

首先,它有一个bug,它不会擦除以前的光标位置,这会导致绘制多个光标,如下所示:

以下是与此相关的代码:

- (void)drawRect:(CGRect)rect
{
//  Drag the line number gutter background.  The line numbers them selves are drawn by LineNumberLayoutManager.
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect bounds = self.bounds;

CGFloat height = MAX(CGRectGetHeight(bounds), self.contentSize.height) + 200;

// Set the regular fill
CGContextSetFillColorWithColor(context, self.gutterBackgroundColor.CGColor);
CGContextFillRect(context, CGRectMake(bounds.origin.x, bounds.origin.y, self.lineNumberLayoutManager.gutterWidth, height));

// Draw line
CGContextSetFillColorWithColor(context, self.gutterLineColor.CGColor);
CGContextFillRect(context, CGRectMake(self.lineNumberLayoutManager.gutterWidth, bounds.origin.y, 0.5, height));

if (_lineCursorEnabled)
{
    self.lineNumberLayoutManager.selectedRange = self.selectedRange;

    NSRange glyphRange = [self.lineNumberLayoutManager.textStorage.string paragraphRangeForRange:self.selectedRange];
    glyphRange = [self.lineNumberLayoutManager glyphRangeForCharacterRange:glyphRange actualCharacterRange:NULL];
    self.lineNumberLayoutManager.selectedRange = glyphRange;
    [self.lineNumberLayoutManager invalidateDisplayForGlyphRange:glyphRange];
}

[super drawRect:rect];
}
我将一行更改为:

[self.lineNumberLayoutManager invalidateDisplayForGlyphRange:NSMakeRange(0, self.text.length)];
它解决了这个问题。但没关系:这两种方法在UITextView滚动时都非常慢。它完全不适合生产,因为使用UITextView很不舒服。我还想在以后添加“错误”功能,这样我就可以在错误发生的地方画红色光标

所以问题是,实现这类功能的最佳和最有效的方法是什么


我为什么要告诉你那只虫子?我这样说是为了强调我的要求是动态定位游标,它只指向当前行,并且您还应该注意,如果行不适合TextView的框架宽度(即第8行和第9行),游标rect可能比常规行宽度长得多。

您可以在我提供的链接中看到完整的代码,是的,这是一个textview的DrawRect,你最终找到了更好的解决方案吗?@Kyle我自己解决了它,但那个项目现在被冻结了,所以我从来没有时间发布它,因为drag-n-droppableNo担心这个问题。事实上,我发现:在问了这个问题之后。它使用了一种完全不同的方法,到目前为止效果非常好。你可以在我提供的链接中看到完整的代码,是的,这是textview的DrawRect。你最终找到了更好的解决方案吗?@Kyle我自己解决了这个问题,但这个项目现在已经冻结,所以我从来没有时间发布它,因为drag-n-droppableNo担心这个问题。事实上,我发现:在问了这个问题之后。它使用了一种完全不同的方法,到目前为止效果相当好。