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

iphone – 如何在UITextView中找到光标的像素位置?,第1张

概述我正在为iPad开发一个简单的写作应用程序. 我正在尝试在UITextView中计算光标的像素位置.我花了几个星期来设计这个,但我仍然无法想象这样做. 在stackoverflow中,Tony编写了一个很好的算法来查找光标的像素位置. Pixel-Position of Cursor in UITextView 我通过一些修改实现了它,它几乎可以工作,它给出了正确的光标像素位置.但是,它只适用于英 我正在为iPad开发一个简单的写作应用程序.

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

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

Pixel-Position of Cursor in UITextView

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

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

有没有其他方法可以在UITextVIEw中找到光标的像素位置?

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

加成:

这是我基于Tony算法的实现.我在横向模式中放置了一个UITextVIEw,因此它的宽度为1024,我使用了大小为21的自定义字体.您应该适当地更改sizeOfContentWIDth和sizeOfContentline. sizeOfContentWIDth小于实际宽度,sizeOfContentline大于实际字体大小(行高>字体大小).

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

#define sizeOfContentWIDth 1010#define sizeOfContentHeight 1000#define sizeOfContentline 25    // Stores the original position of the cursorNSRange originalposition = textVIEw.selectedRange;    // Computes textVIEw's originCGPoint origin = textVIEw.frame.origin;// Checks whether a character right to the current cursor is a non-space characterunichar 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 wordif(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 locationint deviationLocation = textVIEw.selectedRange .location - originalposition.location;// Substrings the part before the cursor positionNsstring* head = [textVIEw.text substringToIndex:textVIEw.selectedRange.location];// Gets the size of this partCGSize initialSize = [head sizeWithFont:textVIEw.Font constrainedToSize:CGSizeMake(sizeOfContentWIDth,sizeOfContentHeight)];// Gets the length of the headNSUInteger startOfline = [head length];// The first lineBOol 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 positionNsstring* tail;if(isFirstline == NO)    tail = [head substringFromIndex:(startOfline + deviationLocation)];else {    tail = [head substringToIndex:(startOfline - deviationLocation)];}// Gets the size of this partCGSize linesize = [tail sizeWithFont:textVIEw.Font forWIDth:sizeOfContentWIDth lineBreakMode:UIlineBreakModeWorDWrap];// Gets the cursor position in coordinateCGPoint cursor = origin;    cursor.x += linesize.wIDth;cursor.y += initialSize.height - linesize.height;// Back to the original positiontextVIEw.selectedRange = originalposition;// DeBUGprintf("x: %f,y: %f\n",cursor.x,cursor.y);
解决方法 如果您只针对IOS7,则可以使用
UITextVIEw方法:

- (CGRect)caretRectForposition:(UITextposition *)position;

简短样本:

NSRange range; // target location in text that you should get from somewhere,e.g. textvIEw.selectedRangeUITextVIEw textvIEw; // the text vIEwUITextposition *start = [textvIEw positionFromposition:textvIEw.beginningOfdocument offset:range.location];CGRect caretRect = [self caretRectForposition:start]; // caret rect in UITextVIEw
总结

以上是内存溢出为你收集整理的iphone – 如何在UITextView中找到光标的像素位置?全部内容,希望文章能够帮你解决iphone – 如何在UITextView中找到光标的像素位置?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1086246.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存