f0rz
f0rz

Reputation: 1565

UITextView position

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

Upvotes: 2

Views: 823

Answers (2)

Mark Pervovskiy
Mark Pervovskiy

Reputation: 1123

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;
}

Upvotes: 1

Mihir Mehta
Mihir Mehta

Reputation: 13833

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..

Upvotes: 2

Related Questions