iOS UITextView:如何创建一个双倍行距的输入文本字段?

时间:2013-10-16 22:45:51

标签: ios objective-c uitextview textkit

这是this question的后续行动。我正在尝试创建一个双倍行距的UITextView来输入文本。

在我的自定义UITextView中,我覆盖了-caretRectForPosition:限制光标的高度。但是现在当我选择文本时,选择矩形的高度仍然太高: red selection rectangle is too tall

我试图通过覆盖UIView的-selectionRectsForRange来克服这个问题。不幸的是,返回数组中的对象是UITextSelectionRects,它是一个不可变的子类。所以我创建了自己的子类,使高度可变,并在循环中改变它:

- (NSArray *)selectionRectsForRange:(UITextRange *)range
{
    NSArray *originalSectionRects = [super selectionRectsForRange:range];
    NSMutableArray *newSelectionRects = [[NSMutableArray alloc] initWithCapacity:originalSectionRects.count];

    // For all the UITextSelectionRects
    for (UITextSelectionRect *selectionRect in originalSectionRects) {
        // Use one with a custom height
        DCTextSelectionRect *newSelectionRect = [[DCTextSelectionRect alloc] initWithTextSelectionRect:selectionRect rectHeight:self.font.lineHeight];
        [newSelectionRects addObject:newSelectionRect];
    }
    return newSelectionRects.copy;
}

现在选择矩形的大小合适,但是用于生长的蓝色手柄和缩小选择区域被困在左上角。

handles are trapped in the top left. selection height is correct

知道是什么原因导致手柄处于错误的位置吗?我可以尝试将它们放在正确的位置?

0 个答案:

没有答案
相关问题