我必须得到一个包含在一个字符串的屏幕上的位置UITextView.为此,我尝试获取相应的UITextPosition(s).问题是该UITextView方法beginningOfDocument返回nil(相同endOfDocuement).有人能解释我为什么吗?我可以使用哪种解决方案.
我的代码看起来像这样:
- (UITextRange *)getRangeOfString:(NSString *)string inTextView:(UITextView *)tv
{
UITextPosition *beginning = [tv positionFromPosition:tv.beginningOfDocument offset:[tv.text rangeOfString:string].location];
UITextPosition *end = [tv positionFromPosition:beginning offset:[tv.text rangeOfString:string].length];
return ([tv textRangeFromPosition:beginning toPosition:end]);
}
Run Code Online (Sandbox Code Playgroud)