开发者

UITextView position

开发者 https://www.devze.com 2022-12-26 21:44 出处:网络
Is there a way to find ypos in a UITextView, or/and find which row that currently 开发者_Python百科marker is active?you need to do some calculation

Is there a way to find ypos in a UITextView, or/and find which row that currently 开发者_Python百科marker is active?


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


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

精彩评论

暂无评论...
验证码 换一张
取 消