views:

78

answers:

2

Is there a way to find ypos in a UITextView, or/and find wich row that currently marker is active?

Regards.

+2  A: 

you need to do some calculation find cursor position by

NSRange cursorPosition = [tf selectedRange];

substring from cursor position use this sub string to calculate width of the string by

sizeWithFont:constrainedToSize: 

and then divide it by width of the your TextView width.. it will give you at which line your cursor is... It's logically seems correct... haven't tried it... try it let me know if it is working or not..

mihirpmehta
A: 

Something like that:

- (int) getCurrentLine: (UITextView *) textView{
    int pos = textView.selectedRange.location;
    NSString *str =textView.text;
    NSCharacterSet *charset = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    int num = pos;
    for (; num< str.length; num++){
        unichar c = [str characterAtIndex:num];
        if ([charset characterIsMember:c]) break;
    }
    str = [str substringToIndex:num];

    CGSize tallerSize =  CGSizeMake(textView.frame.size.width - 16, 999999);
    CGSize lc1 = [@"Yyg" sizeWithFont: textView.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap];
    CGSize lc = [str sizeWithFont: textView.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap];
    lc.height = lc.height / lc1.height;
    return lc.height;
}
Mark Pervovskiy