Я разрабатываю простое приложение для письма для iPad.
Я пытаюсь вычислить пиксельную позицию курсора в UITextView
. Я потратил несколько недель на разработку этого, но все еще не мог понять, как это сделать.
В stackoverflow Тони написал один хороший алгоритм для определения позиции курсора в пикселях.
Позиция курсора в пикселях. в UITextView
я реализовал это с небольшой модификацией, и почти работает то, что он дает правильную позицию курсора в пикселях. Однако он работает только с английскими алфавитами.
Если в конце строки есть китайский или японский символ, UITextView
выполняет перенос символов вместо переноса слов, даже если между ними нет пробела Китайские иероглифы. Я думаю, что алгоритм Тони работает, когда UITextView
выполняет только перенос слов (с английскими алфавитами).
Есть ли другой способ найти пиксельную позицию курсора в UITextView
?
Или есть способ определить, следует ли для конкретного символа перенос символов, как китайские символы, или перенос слов, как английский?
Дополнение:
Вот моя реализация, основанная на алгоритме Тони. Я поместил один UITextView
в альбомный режим, поэтому его ширина равна 1024, и я использовал собственный шрифт с размером 21. Вам следует изменить sizeOfContentWidth
и sizeOfContentLine
соответствующим образом. . 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);