1

I'm facing a problem which takes me too long to solve. I built a view, which contains a ScrollView. in the ScrollView, there's a view, which contains an image, and a UITextView. the textview should be in a dynamic height, with scrolling disabled. the textview gets all the text, but cut it off, and shows only the text that fits the height. in addition, the ScrollView doesn't changes.

- (void)viewDidLoad

 ....
 //sets the text to the textview
 [self.contentArticle setText:[NSString stringWithString:xmlParser.articleContent]];
 //configures the scrollView
 [self configureScrollView];

 ....

- (void)configureScrollView {

[self.contentView addSubview:self.contentArticle];
[self.scrollView addSubview:self.contentView];

CGRect frame = self.contentView.frame;
frame.size.height = self.contentArticle.contentSize.height;
self.scrollView.frame = frame;

[self.contentView sizeToFit];
[self.scrollView sizeToFit];

self.scrollView.contentSize = self.contentView.frame.size;
self.contentArticle.editable=NO;
self.contentArticle.scrollEnabled=NO;

 //enable zoomIn
self.scrollView.delegate=self;
self.scrollView.minimumZoomScale=1;
self.scrollView.maximumZoomScale=7;

I did so many changes, and im not sure what is going on in there anymore!...

help would be sooo nice :)

UPDATE-

- (void)configureScrollView {

[self.contentView addSubview:self.contentArticle];
[self.scrollView addSubview:self.contentView];

CGRect textViewFrame = self.contentArticle.frame;
textViewFrame.size = [self.contentArticle contentSize];
self.contentArticle.frame = textViewFrame;

[self.scrollView setContentSize:textViewFrame.size];
self.contentArticle.editable=NO;
self.contentArticle.scrollEnabled=NO;
}

screen shot of the View

AndroAid
  • 77
  • 1
  • 8
  • See also http://stackoverflow.com/questions/27652334/uitextview-inside-uiscrollview-with-autolayout/30633898#30633898 – Suragch Jun 04 '15 at 02:24

3 Answers3

3

Try

- (void)configureScrollView{

    self.contentView.autoresizingMask = UIViewAutoresizingNone;
    self.contentArticle.autoresizingMask = UIViewAutoresizingNone;

    CGRect textViewFrame = self.contentArticle.frame;
    textViewFrame.size = [self.contentArticle contentSize];
    self.contentArticle.frame = textViewFrame;

    CGRect contentViewFrame = self.contentView.frame;
    contentViewFrame.size.height = textViewFrame.origin.y+textViewFrame.size.height;
    self.contentView.frame = contentViewFrame;

    [self.scrollView setContentSize:contentViewFrame.size];

    self.contentArticle.editable=NO;
    self.contentArticle.scrollEnabled=NO;

    //enable zoomIn
    self.scrollView.delegate=self;
    self.scrollView.minimumZoomScale=1;
    self.scrollView.maximumZoomScale=7;

}

Source code

Anupdas
  • 10,211
  • 2
  • 35
  • 60
  • 1
    I updated my question... part of the code really helped me, still the height of the scrollview doesn't fit, it remains the same. when I used your code, without changes, the textview started at the middle of the view... any idea? – AndroAid Jun 09 '13 at 21:10
  • @AndroAid If you are resizing your views programmatically, don't set autolayouts/autoresizing masks.If you are setting, set within the containerView You can refer to the source code provided. – Anupdas Jun 10 '13 at 01:21
  • I can't figure it out... I have a UIView, that contains a UIScrollView. the scrollView contains the contentView, which contains the textview. the autoResize subviews is checked in all the views. but still NADA. my project is almost done, and this is the last main issue :/ – AndroAid Jun 10 '13 at 09:33
  • added screenshot of the problematic view :) – AndroAid Jun 10 '13 at 09:38
  • @AndroAid One reason I can think about is using the `AutoLayout`. The source code I provided don't use autolayout. – Anupdas Jun 10 '13 at 09:42
0

You need to set self.contentView height first Use this code:

- (void)configureScrollView {

    [self.contentView addSubview:self.contentArticle];
    CGRect frame = self.contentArticle.frame;
    frame.size.height = self.contentArticle.contentSize.height;
    self.contentArticle.frame = frame;

    [self.scrollView addSubview:self.contentView];
    frame = self.contentView.frame;
    frame.size.height += self.contentArticle.frame.height;
    self.contentView.frame = frame;

    self.scrollView.contentSize = self.contentView.frame.size;
    self.contentArticle.editable=NO;
    self.contentArticle.scrollEnabled=NO;

    //enable zoomIn
    self.scrollView.delegate=self;
    self.scrollView.minimumZoomScale=1;
    self.scrollView.maximumZoomScale=7;
}
Gajendra K Chauhan
  • 3,387
  • 7
  • 40
  • 55
Prateek Prem
  • 1,544
  • 11
  • 14
  • thanks for the answer. as I wrote to Anupdas, the code seems right, but the text in the textview starts at the middle of the view. there's lots of scrolling until it gets to the text, and then the text cuts off in the middle. – AndroAid Jun 09 '13 at 21:14
0

had same issue , tried to find solution for many days and finally i got it working. I have a textview inside a scrollview.

  1. disabled autolayout from storyboards
  2. ive added the textview to scrollview with addsubview
  3. and finally set the content of the scrollview, to the textview frame height.

Below my code:

_textView.text = stripped; //(some string ,yours: contentArticle)
[_textView sizeToFit];
CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.scrollEnabled = NO;
_textView.frame = frame;

[self.scrollView addSubview:_textView];
[self.scrollView setContentSize:CGSizeMake(320,frame.size.height)];

Hope it helps!

Kleon
  • 1
  • 1