如何在UITextView中找到光标的像素位置?

pnm*_*nmn 6 iphone cursor uitextview ipad ios

我正在为iPad开发一个简单的写作应用程序.

我正在尝试计算光标的像素位置UITextView.我花了几个星期来设计这个,但我仍然无法想象这样做.

在stackoverflow中,Tony编写了一个很好的算法来查找光标的像素位置.

UITextView中光标的像素位置

我通过一些修改实现了它,它几乎可以工作,它给出了正确的光标像素位置.但是,它只适用于英文字母.

如果行尾有中文或日文字符UITextView,即使汉字之间没有空格,也要进行字符包装而不是自动换行.我认为Tony的算法UITextView只能执行自动换行(使用英文字母).

有没有其他方法可以找到光标的像素位置UITextView

或者有没有办法确定一个特定的字符是否像汉字一样包含字符或像英语一样包装?

加成:

这是我基于Tony算法的实现.我一个放置UITextView在一个横向模式,因此,其宽度为1024,我使用具有尺寸21的自定义字体你应该改变sizeOfContentWidthsizeOfContentLine适当.sizeOfContentWidth小于实际宽度,并且sizeOfContentLine大于实际字体大小(行高>字体大小).

对于杂乱的代码和评论抱歉!还有一些小错误,如果你在行尾输入中文字符(没有自动换行),它会给出错误的位置.

#define sizeOfContentWidth 1010
#define sizeOfContentHeight 1000
#define sizeOfContentLine 25

    // Stores the original position of the cursor
NSRange originalPosition = textView.selectedRange;    

// Computes textView's origin
CGPoint origin = textView.frame.origin;

// Checks whether a character right to the current cursor is a non-space character
unichar c = ' ';

if(textView.selectedRange.location != [textView.text length])
    c = [textView.text characterAtIndex:textView.selectedRange.location];

// If it's a non-space or newline character, then the current cursor moves to the end of that word
if(c != 32 && c != 10){
    NSRange delimiter = [textView.text rangeOfCharacterFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]
                                                       options:NSLiteralSearch
                                                         range:NSMakeRange(textView.selectedRange.location, [textView.text length] - textView.selectedRange.location)];

    if(delimiter.location == NSNotFound){
        delimiter.location = [textView.text length];
    }

    textView.selectedRange = delimiter;
}

// Deviation between the original cursor location and moved location
int deviationLocation = textView.selectedRange .location - originalPosition.location;

// Substrings the part before the cursor position
NSString* head = [textView.text substringToIndex:textView.selectedRange.location];

// Gets the size of this part
CGSize initialSize = [head sizeWithFont:textView.font constrainedToSize:CGSizeMake(sizeOfContentWidth, sizeOfContentHeight)];

// Gets the length of the head
NSUInteger startOfLine = [head length];

// The first line
BOOL isFirstLine = NO;

if(initialSize.height / sizeOfContentLine == 1){
    isFirstLine = YES;
}

while (startOfLine > 0 && isFirstLine == NO) {
    // 1. Adjusts startOfLine to the beginning of the first word before startOfLine
    NSRange delimiter = [head rangeOfCharacterFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet] options:NSBackwardsSearch range:NSMakeRange(0, startOfLine)];

    // Updates startsOfLine
    startOfLine = delimiter.location;

    // 2. Check if drawing the substring of head up to startOfLine causes a reduction in height compared to initialSize. 
    NSString *tempHead = [head substringToIndex:startOfLine];

    // Gets the size of this temp head
    CGSize tempHeadSize = [tempHead sizeWithFont:textView.font constrainedToSize:CGSizeMake(sizeOfContentWidth, sizeOfContentHeight)];

    // Counts the line of the original
    int beforeLine = initialSize.height / sizeOfContentLine;

    // Counts the line of the one after processing
    int afterLine = tempHeadSize.height / sizeOfContentLine;

    // 3. If so, then you've identified the start of the line containing the cursor, otherwise keep going.
    if(beforeLine != afterLine)
        break;
}

// Substrings the part after the cursor position
NSString* tail;

if(isFirstLine == NO)
    tail = [head substringFromIndex:(startOfLine + deviationLocation)];
else {
    tail = [head substringToIndex:(startOfLine - deviationLocation)];
}

// Gets the size of this part
CGSize lineSize = [tail sizeWithFont:textView.font forWidth:sizeOfContentWidth lineBreakMode:UILineBreakModeWordWrap];

// Gets the cursor position in coordinate
CGPoint cursor = origin;    
cursor.x += lineSize.width;
cursor.y += initialSize.height - lineSize.height;

// Back to the original position
textView.selectedRange = originalPosition;

// Debug
printf("x: %f,   y: %f\n", cursor.x, cursor.y);
Run Code Online (Sandbox Code Playgroud)

gda*_*vis 0

这可能效率相当低,但是您可以采用与您发布的代码相同的基本原理并获取光标所在的文本行,然后循环遍历每个单独的字符并计算[NSString sizeWithFont:forWidth:lineBreakMode:]每个字符的宽度,然后您可以添加所有字符那些适合你的 x 职位?只是一个想法,但可能有助于解决自动换行的问题。