12

I am facing an issue of resizing UITextView in pinch gesture recognizer method. This gesture recognizer is for another view. I want that textview should also be resized along with that view.

I successfully able to resize UITextView according to requirement. But I have issue with text displayed in textview. After zooming in and zooming out for few times. contents inside UITextView does not come properly (see below image "After zooming in/out"). It decreases content width. I checked content size of UITextView, but it gives same width before resizing and after resizing.

Here, I have set text alignment to center for UITextView. I have also applied contentInset to UIEdgeInsetZero.

Any help will be appreciated.

Before zooming in/out Before zooming in/out

After zooming in/out After zooming in/out

jigneshbrahmkhatri
  • 3,627
  • 2
  • 21
  • 33
  • Just for clarification, are you setting the contentSize on every resize accordingly ? – Roshit Mar 31 '12 at 11:54
  • yes, I tried that too.but it is not working as expected. – jigneshbrahmkhatri Mar 31 '12 at 12:00
  • This may be helpful to you sir (If I guessing it right :)). http://stackoverflow.com/questions/7501023/how-to-size-uitextviews-width-to-fit-its-content-without-wrapping – Kuldeep Apr 04 '12 at 08:47
  • Hi Kuldeep, thanks for sharing link, but it still did not solve my problem. Anyways, I have applied other logic, i.e. making custom scrollview and on touch of scroll, I allocate textview and become its first responder. when keyboard dis appears, I deallocate textview. and assign text to UIlabel inside scrollview. – jigneshbrahmkhatri Apr 06 '12 at 04:53
  • were you able to find a solution to this? – SarpErdag Mar 22 '13 at 23:11

3 Answers3

2

Are you calling setNeedsLayout on the parent view after you have altered the frame?

Try logging the bounds of the text view like so:

NSLog(@"Bounds: %@", NSStringFromCGRect(textView.bounds));
Sam
  • 2,579
  • 17
  • 27
  • Thanks for your answer, I have tried setNeedsLayout after setting frames, but still it does not give correct output. I have also checked bounds, everything is ok with bounds and frames, even with contensize, it gives correct, but when it draws text, it is like screenshot I attached. – jigneshbrahmkhatri Mar 30 '12 at 10:04
  • Try resizing the `textView.frame`. – Greg Apr 06 '12 at 04:18
1

I won't profess to be an expert on this, but it seems that UITextView wants to have a baseline of CGRectZero to work with, I have run into this very issue and my findings were that the problem presented itself when shrinking the UITextView, not when enlarging, so by overriding - (void)layoutSubviews like this:

- (void)layoutSubviews {
    CGRect bounds = self.bounds;
    self.bounds = CGRectZero;
    self.bounds = bounds;
}

in a subclass of UITextView this issue will be resolved. This definitely isn't the most elegant thing I've ever seen, but it's the only solution I've come up with.

Trey
  • 356
  • 2
  • 10
0

Trey's answer works but it causes too much CPU usage. Try implementing the same method using KVO.

[self addObserver:self forKeyPath:@"bounds" options:NSKeyValueObservingOptionOld context:NULL];

and then,

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

    if([keyPath isEqualToString:@"bounds"]) {

        [self removeObserver:self forKeyPath:@"bounds"];

        CGRect bounds = self.bounds;
        self.bounds = CGRectZero;
        self.bounds = bounds;

        [self addObserver:self forKeyPath:@"bounds" options:NSKeyValueObservingOptionOld context:NULL];
    }
}
SarpErdag
  • 781
  • 1
  • 8
  • 19