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?

4

3 回答 3

3

请从上一篇文章中试试这个

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];

[帖子]:iOS 7 上的 UITextView contentOffset “上一篇文章”\

于 2013-11-10T09:13:23.013 回答
1

我的想法是,根据 UITextView 的内容调整其大小。

就像在这里问的:how-do-i-size-a-uitextview-to-its-content

代码取自 jhibberd 的回答

 - (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;
    }
于 2013-11-08T11:37:52.907 回答
1

我在 iOS7 中使用 UITextField 遇到了一个非常相似的问题 - 看起来几乎完全相同。我的也与内容插图有关。

解决方案(对我来说) - 是从 nib/storyboard 中消除 UITextField 并将其实例化,然后纯粹从代码中将其添加到视图中。行为之后按预期工作。

除了可能是 iOS7 / XCode 5 错误之外,我不知道发生了什么……很想知道这是否也能解决你的问题。

于 2013-11-06T18:10:03.417 回答