2
A
回答
2
你需要从光标位置
NSRange cursorPosition = [tf selectedRange];
子做一些计算 找到光标位置使用此子字符串由
sizeWithFont:constrainedToSize:
计算字符串的宽度,然后用宽度除以它你的TextView的宽度..它会给你在哪一行你的光标是...这在逻辑上似乎是正确的......没有尝试过......试试让我知道它是否工作或没有..
1
类似的东西:
- (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;
}