UITextView beginOfDocument返回nil [英] UITextView beginningOfDocument returns nil
问题描述
我必须在屏幕上获得 UITextView
中包含的字符串的位置。为了做到这一点,我试图得到相应的UITextPosition。问题是 UITextView
方法 startingOfDocument
返回 nil
同样 endOfDocuement
)。有人可以解释我为什么吗?
我的代码如下所示:
- (UITextRange *)getRangeOfString:(NSString *)string inTextView:(UITextView *)tv
{
UITextPosition * beginning = [tv positionFromPosition:tv.beginningOfDocument offset:[tv.text rangeOfString:string ]。位置];
UITextPosition * end = [tv positionFromPosition:begin offset:[tv.text rangeOfString:string] .length];
return([tv textRangeFromPosition:beginning toPosition:end]);
}
,但请务必在Interface Builder中启用可选择属性。
贷记: http://jon-nolen.blogspot.com/2013/10/uitextview-returns-nil-for-uitextinput.html
I have to get the position on the screen of a string contained in a UITextView
. To do that, I tried to get the corresponding UITextPosition(s). The problem is that the UITextView
method beginningOfDocument
returns nil
(same for endOfDocuement
). Could someone explain me why? And which solution I could use.
My code looks like this:
- (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]);
}
This is about a month late, but make sure you have the "selectable" property enabled in Interface Builder.
Credit: http://jon-nolen.blogspot.com/2013/10/uitextview-returns-nil-for-uitextinput.html
这篇关于UITextView beginOfDocument返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!