5

For some strange reason, my UITextView's text appears cropped when the scrollofset is set.

Here's what it looks like: enter image description here

This happens after doing:

        textview.contentInset = UIEdgeInsetsZero;
        [textview setContentOffset:CGPointMake(0, startypos + offset_yshift) animated:NO];

I tried manually setting contentSize.height, but that introduced another strange behavior, where the content offset seems to be ignored..

''Edit'': This is the code used to instantiate the textfield:

  CGRect myImageRect = CGRectMake(-50.0f, -50.0f, 40.0f, 40.0f);
  textview = [[UITextView alloc] initWithFrame: myImageRect];
  textview.autocapitalizationType = UITextAutocapitalizationTypeNone;
  [textview setScrollEnabled:YES];
  textview.hidden = YES;
  textview.contentInset = UIEdgeInsetsZero;
  textview.opaque = NO;
  textview.backgroundColor = [UIColor clearColor];
  textview.textColor = [UIColor colorWithWhite:1 alpha:1];
  textview.textAlignment = NSTextAlignmentCenter;
  textview.frame = CGRectMake(0, 0, 250, 30);
  textview.scrollEnabled = NO;

And the "update" code that checks the content positioning every frame:

  // setting the actual size here
  UITextPosition * pos = [textview positionFromPosition: textview.endOfDocument offset:nil];
  CGRect therect = [textview caretRectForPosition:pos];

  CGRect frame = textview.frame;

  if([textview.text length] == 0){
     frame.size.height = 30;
  } else {
     frame.size.height = therect.origin.y + therect.size.height;
  }

  // and here, we're changing the frame variable's height to max to 50
  if(frame.size.height > 50){
     frame.size.height = 50;
  }
  frame.size.width = desiredwidth; // some other variable

  textview.frame = frame;

  /*

     ... snip, unrelated code ...

  */

  // later on

        textview.contentInset = UIEdgeInsetsZero;
        [textview setContentOffset:CGPointMake(0, startypos + offset_yshift) animated:NO];

As you can imagine, the setContentOffset bit there is what's causing the issue.

What is going on?

kamziro
  • 7,882
  • 9
  • 55
  • 78

3 Answers3

3

please try this from prev post

CGPoint offset = CGPointMake(0, self.textView.contentSize.height - self.textView.frame.size.height);
[self.textView setContentOffset: CGPointMake(0,0) animated:NO]; 
or
[self.textView setContentOffset:bottomOffset animated:YES];

[post]: UITextView contentOffset on iOS 7 "prev post"\

Community
  • 1
  • 1
Muthu Ram
  • 993
  • 6
  • 15
  • Doesn't seem to work :/ Using the above code as it is, the second setContentOffset seems like it does nothing (it's still scrolled at the top), even while the text cursor is all the way at the bottom. Changing animated:YES to animated:NO nullifies the first setContentOffset. – kamziro Nov 10 '13 at 13:34
1

I ran into a very similar issue in iOS7 with UITextField - looks almost exactly the same. Mine had to do with content insets as well.

The solution (for me) - was to eliminate the UITextField from nib/storyboard and instantiate it and add it to the view purely from code. Behavior worked as expected afterwards.

I don't know what was up other than it probably being a iOS7 / XCode 5 bug... Curious to know if that solves it for you too.

Taylor Halliday
  • 640
  • 1
  • 8
  • 19
  • Hmm, unfortunately my UITextField was instantiated from code as well :/ – kamziro Nov 08 '13 at 04:32
  • Huh - Would you mind throwing all the code you use to instantiate it up? Would love to try and reproduce. I swear I had the same issue a couple of days ago; and thought I fixed it by taking it out of the nib... maybe I'm not recalling what I did. – Taylor Halliday Nov 08 '13 at 15:13
1

My thougts would be, to adjust the size of the UITextView based on its content.

Like asked here: how-do-i-size-a-uitextview-to-its-content

Code taken from jhibberd ´s answer

 - (void)textViewDidChange::(UITextView *)textView
    {
        CGFloat fixedWidth = textView.frame.size.width;
        CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
        CGRect newFrame = textView.frame;
        newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
        textView.frame = newFrame;
    }
Community
  • 1
  • 1
CodeFanatic
  • 11,434
  • 1
  • 20
  • 38
  • @jhibberd , create your own Answer and i will remove mine, hence it is your solution i presented – CodeFanatic Nov 08 '13 at 11:41
  • That's my current situation, but the thing is, I want to dynamically scroll down if the contents of the textview is too large. Currently the textbox just resizes as large as it needs to, and it's a problem because some text is way too big and takes all the screen. And if I put a limit on the frame size, well.. the thing I described happens. – kamziro Nov 08 '13 at 14:29