Dynamically layout scrollview contentSize with AutoLayout - ios

I have a simple case:
I have the following views hierarchy:
View
ScrollView
View
Label
My label is positioned at the bottom of it's super view, and it's height is changed dynamically, depending ot the size of the text that it is rendering.
My aim is to adjust the label size depending on the text, so that no text is truncated, and with the growth of the lines of the label, to grow the scrollview's content size, so that the label always is positioned at the bottom.
How can I do that with autolayouts, preferably from IB only?

If you want the UILabel to stick at the bottom of its parent UIView using autolayout, then that view won't expand its height when the UILabel height increase, what really happens is that the UILabel will move up to occupy more area.
I f you wish your UIView to expand, then don't use autolayout in that UIView, and position your UILabel at a constant origin, then change the view's height & the scrollview height as per the UILabel text.
you can get the UILabel size using the below line of code:
CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font
constrainedToSize:maximumLabelSize
lineBreakMode:yourLabel.lineBreakMode];

Related

Unable to increase the height of UIView in relation to TextView

I used the vertical stack view to hold two UIView.Second UIView has a textArea.But I could not find a way to change the UIview height when the height of TextView change

how make the UIView height equal to its subview's additional height, so that it can easily scroll till the end

I have a situation where i have following view layout.
-UIViewcontroller
-SCrollView
-UIView(outer)
-buttons
-labels
-UIView(inner)
-labels
-buttons
-buttons
the inner UIView height can be any longer as content inside is dynamically added.
So the main issue is i can not scroll till the end of the content, instead i can only scroll till the height of inner UIView.
NOTE - I am using auto layout
Any help is much appreciated.
Thanks
You should change the frame of your inner view dynamically based on the content you are adding. According to your inner view hierarchy, you have label and button. You might be setting some dynamic text on label and also doing some manipulation with button . Use the following method to get the height of the text ,
- (CGSize)getHeightForText:(NSString *)text havingWidth:(CGFloat)widthValue andFont:(UIFont *)font {
CGSize size = CGSizeZero;
if (text) {
CGRect frame = [text boundingRectWithSize:CGSizeMake(widthValue, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:#{ NSFontAttributeName:font } context:nil];
size = CGSizeMake(frame.size.width, frame.size.height);
}
return size;
}
Here font is the font you set to button and label. Call this method by passing the text added on label and button. CGSize has a height property , add height value of label and button. Now set this sum height to innerView by calling innerView.frame.size.height = <height>. This increase the height of your innerView based on content inside that.
Put all your buttons in the bottom inside a container view and add the following constraints to the outerView (assuming that the new container you've added is called "buttonContainer"):
A vertical spacing constraint between the bottom of innerView and the top of the buttonContainer.
A vertical spacing constraint between the bottom of the buttonContainer and the bottom of the outerView.
Horizontal spacing constraints between between the buttonContainer and outerView to force it to full width.
When using AutoLayout, you have to have enough constraints to properly define the contentSize of the scrollView.
For more information about using AutoLayout with UIScrollView, check out the following links by Apple:
Working with Scroll Views
UIScrollView and Autolayout

Scrollview content size set to screen width max

In my view I have a scrollview.
In my scrollview I have a UILabel.
I use constraints to set my scrollview in my superview and also for my UILabel.
If I set a long text inside my UILabel the scrollview's content extends to the width of my UILabel.
Instead of that, I would like the content view set to maximum width of my screen and the UILabel goes to multilines.
How can I do this ?
Try setting your label's number of line to 0 and preferredMaxLayoutWidth the width you want, then call setNeedsLayout with your main view.

Auto resize UITextView in UIScrollView

I have a question, how can I resize a textview in a scrollview and scrollview also must resize to be conformable with what size the textview will be.
Thank you.
Without auto layout it is much easier. Just get the contentSize property of the UITextView:
float textViewHeight = textView.contentSize.height;
And then set scroll view content size according to the content size of the text view:
scrollview.contentSize = CGSizeMake(320,textViewHeight + 100);
This will make the height of the scroll view the height of the content size of the textview plus 100 pixels (in case you have other stuff in your scroll view).

How can I know the height of a superview after I change one of its subview like UILabel

I change the My UILabel's text and make it become multiple lines. I guess the the superview of this label will resize after that. But how to get the new frame of the superview as soon as I change the label.text?
CGSize size = labelSuperview.size;

Resources