2

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

Kenshin
  • 1,030
  • 2
  • 12
  • 41
  • Check the value of `textView.contentOffset.y` and see if that accounts for the difference in the `y` values? Assuming here you might have scrolled down 82pts in your example. It seems likely to relate to y offset issues as the wrapping seems correct and it has the x value as 0 on both cases. The height difference could be accounted for by slight font changes in iOS9. – Rory McKinnel Sep 02 '15 at 10:16
  • Also where are you calling this code from? It should be in `viewWillAppear` or later. – Rory McKinnel Sep 02 '15 at 10:35
  • Roy, we tried in viewDidAppear. But the end result is the same. If it's font changes in iOS 9, how do we fix it? How to make this work in iOS 8 and 9. Do you have a fix for this ? I will check and let you know the values of textView.contentOffset.y in iOS 8 and 9 – Kenshin Sep 03 '15 at 06:37
  • I was implying the height difference between 19 and 19.4 might be down to the font being slightly different in iOS9. Not much you can do about that. Depending how you setup the text area, iOS may also scale fonts slightly to fit text. The real issue of y position difference I think will be down to layout. Also just noticed your two images are for iPad Mini on iOS8 and iPad on iOS9. I take it these are real devices you are using? Do you see the same on like for like devices in the simulator: iOS8 iPad versus iOS9 iPad. Would be good to be comparing like for like at the same scroll offsets. – Rory McKinnel Sep 03 '15 at 09:13
  • Roy, `textView.contentOffset.y value is 0` in both iOS 8 and 9. In simulator also the same problem. Device and simulator makes no difference. We are creating the TextView programmatically and not in layout. – Kenshin Sep 04 '15 at 06:13
  • I pasted the textview initialization logic above. Please check and let me know – Kenshin Sep 04 '15 at 06:21
  • You seem to only create the frame to take into account iPhone 4 or 5. There is no iPad, iPhone 6 or 6+ related frame setting. You would be better to use constraints for this and add the UITextView with constraints pinning it to the top layout, bottom layout, leading and trailing edges. Then it will auto size for all devices. I was expecting iPad settings as its iPad screen shots you have labelled in your question. – Rory McKinnel Sep 04 '15 at 10:17
  • If that is the case, how it's working in all devices running iOS 8. – Kenshin Sep 05 '15 at 06:15
  • Not sure, however just pointing out you have coded it with no ipad or large iphone frame. So the fact it is working on iPad in iOS8 might imply its ignoring your frame settings and something else is in play like auto generated constraints. Try forcing a layout before you do the rect request using setNeedsLayout then layoutIfNeeded on the text view and or main view. I have also sometimes found text view can behave oddly if not in edit mode for some operations. Sorry have no answer, just suggestions. – Rory McKinnel Sep 05 '15 at 09:12
  • I tried in xib layout, adjusted auto resizing options, changed to editable field, removed custom font. I think I tried everything. But the behaviour is the same. Nothing changed,. – Kenshin Sep 07 '15 at 06:24
  • Pretty much out of ideas. it would be worth trying to create a totally stripped down version you can post. Then ask as a new question or put a bounty on it to get more attention. Might be worth you raising a defect in case it's a known issue. – Rory McKinnel Sep 07 '15 at 07:07
  • I am also facing same problem. I am getting wrong frame for both iOS 8 and iOS 9. Anyone have solution for this problem? – Dipak Jan 02 '16 at 09:47
  • Yes Deepak, we fixed this problem. I forgot to close this topic. You have to handle iOS8 and 9 with different if condition. I am sure you might have added additional offset to place your textview. This additional offset or adjustment is not required in iOS9. If you want, I can paste the logic here – Kenshin Jan 03 '16 at 19:46

0 Answers0