如题,进来都有分

解决方案 »

  1.   

    http://blog.csdn.net/liyanbo1984/article/details/5807095
    没测试,仅供参考
      

  2.   


    - (int)getCursorPointY:(UITextView *)curtextView
    {
        CGPoint cursorPoint;
        int Len = curtextView.selectedRange.location;
        NSString *stringToCursor = [curtextView.text substringToIndex:Len];
        NSArray *partStrArr = [stringToCursor componentsSeparatedByString:@"\n"];
        int row = [partStrArr count];
        CGSize fontsize = [@"我" sizeWithFont:[curtextView font]];
        for (NSString *partStr in partStrArr) 
        {
            CGSize partStrSize = [partStr sizeWithFont:[curtextView font] constrainedToSize:curtextView.contentSize lineBreakMode:UILineBreakModeCharacterWrap];
            NSLog(@"partStrSize.width = %f,partStrSize.height = %f",partStrSize.width,partStrSize.height);
            int rowG = partStrSize.height/fontsize.height;
            if (rowG >1) {
                rowG -=1;
                row += rowG;
            }
            
        }
        
        cursorPoint.y = (row -1)*fontsize.height;
        
        return (int)(cursorPoint.y);
    }以上这段代码在4.3里可以准确定位,但是在5.1里每次定位都会产生误差,并且误差是累积的。
      

  3.   

    看看这个行不行:
    UITextRange *startTextRange = [textview characterRangeAtPoint:CGPointZero];
        CGRect caretRect = [textview caretRectForPosition:startTextRange.end];
        CGFloat topMargin = CGRectGetMinY(caretRect);
        CGFloat lineHeight = CGRectGetHeight(caretRect);
        
        caretRect = [textview caretRectForPosition:textview.selectedTextRange.end];
        CGFloat caretTop = CGRectGetMinY(caretRect);
        NSInteger lineIndex = (caretTop - topMargin) / lineHeight;
      

  4.   

    额,这是ios5的方法,不能用于ios4, 看来你需要综合一下 
      

  5.   

    使用 uitextview 的selectedRange属性可以得到光标的位置
      

  6.   

    1, 用selectedRange 获取光标位置,从而获得高度
    2.   用sizeWithFont可以算出行高
    3。 两者相除
      

  7.   


    用selectedRange如何得出高度啊?只能得出光标在字符中的位置,并不是坐标
      

  8.   

    使用 uitextview 的selectedRange属性可以得到光标的位置 
      

  9.   

    比如说一个聊天软件,提供三行的UITextview,但是,当用户的光标到达第二行或第三行时,UITextview自动向上滚动