大佬教程收集整理的这篇文章主要介绍了iphone – 如何在UITextView中找到光标的像素位置?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试在UITextView中计算光标的像素位置.我花了几个星期来设计这个,但我仍然无法想象这样做.
在stackoverflow中,Tony编写了一个很好的算法来查找光标的像素位置.
Pixel-Position of Cursor in UITextView
我通过一些修改实现了它,它几乎可以工作,它给出了正确的光标像素位置.但是,它只适用于英文字母.
如果行尾有@L_944_5@或日文字符,即使汉字之间没有空格,UITextView也会执行字符包装而不是自动换行.我认为Tony的算法在UITextView只执行自动换行(使用英文字母)时有效.
有没有其他方法可以在UITextView中找到光标的像素位置?
或者有没有办法确定一个特定的字符是否像汉字一样包含字符或像英语一样包装?
加成:
这是我基于Tony算法的实现.我在横向模式中放置了一个UITextView,因此它的宽度为1024,我使用了大小为21的自定义字体.您应该适当地更改sizeOfContentWidth和sizeOfContentLine. sizeOfContentWidth小于实际宽度,sizeOfContentLine大于实际字体大小(行高>字体大小).
对于凌乱的代码和评论感到抱歉!还有一些小错误,如果你在行尾输入@L_944_5@字符(没有自动换行),它会给出错误的位置.
#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);
- (CGRect)caretRectForPosition:(UITextPosition *)position;
简短样本:
NSRange range; // target LOCATIOn in text that you should get from somewhere,e.g. textview.SELEctedRange UITextView textview; // the text view UITextPosition *start = [textview positionFromPosition:textview.beginningOfDocument offset:range.LOCATIOn]; CGRect caretRect = [self caretRectForPosition:start]; // caret rect in UITextView
以上是大佬教程为你收集整理的iphone – 如何在UITextView中找到光标的像素位置?全部内容,希望文章能够帮你解决iphone – 如何在UITextView中找到光标的像素位置?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。