CGRect of firstRectForRange method in UITextView returns wrong frame position in iOS 9

1k Views Asked by At

Running the below code in iPod 5 / iPad with iOS 8 and iOS 9 gives the below output

CGRect textViewFrame;
if(IS_IPHONE_5)
    textViewFrame = CGRectMake(0.0f, 64.0f, 320.0f, 390.0f);
else
    textViewFrame = CGRectMake(0.0f, 64.0f, 320.0f, 320.0f);
textViewText = [[UITextView alloc] initWithFrame:textViewFrame];
textViewText.returnKeyType = UIReturnKeyDone;
textViewText.delegate = self;
textViewText.backgroundColor = [UIColor clearColor];
[self.view addSubview:textViewText];

[self.textViewText setFont:[UIFont fontWithName:@"PT Mono" size:16]];
[self.textViewText setTextColor:[UIColor colorWithRed:32.0f/255.0f green:32.0f/255.0f blue:32.0f/255.0f alpha:1]];    
 UITextPosition *beginning = textView.beginningOfDocument;

UITextPosition *start = [textView positionFromPosition:beginning offset:range.location];
UITextPosition *end = [textView positionFromPosition:start offset:range.length];
UITextRange *textRange = [textView textRangeFromPosition:start toPosition:end];

CGRect rect = [textView firstRectForRange:textRange];
rect.size.width = rect.size.width + 2;
rect.origin.x = rect.origin.x - 1;

return [textView convertRect:rect fromView:textView.textInputView];

CGRect rect = [textView firstRectForRange:textRange]; // Problem here

iOS 8 output: {x=0, y=201, width=50, height=19}

iOS 9 output: {x=0, y=293, width=50, height=19.4}

Y cordinate and height is altered in iOS 9

Anything changed in UITextView ??

Check the attached screenshot Running in iOS 8

Running in iOS 9

0

There are 0 best solutions below