在IOS 7中,UITextView offsetFromPosition:toPosition:始终返回0,其中包含从UITextView characterRangeAtPoint返回的UITextPositions的参数

时间:2013-09-25 10:25:53

标签: iphone ios ios6 uitextview ios7

我尝试在代码中使用UITextView characterRangeAtPoint在{text}中的某些UITextRanges获取CGPoints。然后我使用UITextPositions作为参数的UITextRanges并使用UITextView offsetFromPosition: toPosition:来获取偏移量。它在iOS 6中运行良好。但是当我在iOS 7中尝试它时,无论我在textview中尝试过什么offset 0,它总是返回CGPoint

以下是代码:

// init the textview
textview = [[UITextView alloc] initWithFrame:CGRectMake(10, 50, 300, 500)];
textview.backgroundColor = [UIColor whiteColor];
[self.view addSubview:textview];

// load the txt file for testing  
NSError *error;
NSString *content = [[NSString alloc] initWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"test" ofType:@"txt"] encoding:NSUTF8StringEncoding error:&error];
if (error) {
    NSLog(@"load string error:%@", [error localizedFailureReason]);
    return;
}

// set the text and font
textview.text = content;
UIFont *font = [UIFont systemFontOfSize:18];
textview.font = font;

// get text range at point (100, 100) and string offset from the beginning to this point 
UITextRange *textrange = [textview characterRangeAtPoint: CGPointMake(100, 100)];
NSInteger offset = [textview offsetFromPosition:textview.beginningOfDocument toPosition:textrange.start];
NSLog(@"offset = %d", offset);

它返回的offset始终为0。为了测试它是否是因为UITextView offsetFromPosition: toPosition:,我尝试了以下代码:

NSInteger offset1 = [textview offsetFromPosition:textview.beginningOfDocument toPosition:textview.endOfDocument];
NSLog(@"offset1 = %d", offset1);

它给了我offset1 = 5264,根据我使用的test.txt这是正确的。所以问题是从UITextRange返回的textview characterRangeAtPoint。似乎在IOS 7中,characterRangeAtPoint没有在给定的CGPoint返回正确的文本范围。所以textview offsetFromPosition: toPosition:无法返回正确的偏移量。

但我在IOS 6中使用了相同的代码并且效果非常好。我还检查了characterRangeAtPoint的引用,但没有找到有用的信息。

有谁知道IOS 7中characterRangeAtPoint发生了什么?为了使其在IOS 6中发挥作用,我该怎么办?

4 个答案:

答案 0 :(得分:1)

设置textview的setContentInset

试试这段代码

UITextRange *textrange = [textview characterRangeAtPoint: CGPointMake(100, 100)];
NSInteger offset = [textview offsetFromPosition:textview.beginningOfDocument toPosition:textrange.start];

[textview setContentInset:UIEdgeInsetsMake(-45, 0, 5,0)];

NSLog(@"offset = %d", offset);

答案 1 :(得分:1)

我遇到了同样的问题。我发现characterRangeAtPoint除非在IOS 7的TextView中更改或选择了至少一次文本,否则不会返回值。 我不能使这个功能正常工作所以我不得不避免使用它(我试图设置setContentInset但没有运气)。这是我的解决方法。我相信你可以做出类似的事情来适合你的情况。

// get the beginning position of the current line.
NSRange pos = [textView.text rangeOfString:@"\n" options:NSBackwardsSearch range:NSMakeRange(0,textView.selectedRange.location)];
if (pos.location == NSNotFound) pos.location = 0; // if the text has only one line.
NSLog(@"pos = %d",pos.location);

答案 2 :(得分:1)

我找到了此错误的解决方法,并发布了一个解决方案作为对this question的回复。

这是复制粘贴:


这是一个iOS 7错误。 characterRangeAtPoint:总是返回nil,除非事先选择了文本。

作为解决方法,您可以事先致电setSelectedRange:setSelectedTextRange:,但之后textView会突出显示您的选择。我提出了以下解决方案:

[textView select:textView];
[textView setSelectedTextRange:nil];

调用这两种方法后,characterRangeAtPoint:将按预期开始工作。

请注意,您只需在textView初始化后再拨打一次。

答案 3 :(得分:0)

我遇到了最近针对7.1.1开发的相同问题(仍然没有修复......)并希望分享我的简单解决方案。

我尝试了什么Exile.90做了什么,它没有用。

相反,我在storyboard中启用了文本选择,并在viewDidLoad期间禁用了它。没有选择,但受影响的功能现在对我来说非常合适。