Enable scroll view scroll if text view is long - ios

I have a scrollView which contains textView. If text is long and does not fit on a screen, I would like to increase textView height (which I think I could do by adding NSLayoutConstraint outlet and modifying it, correct me if I am wrong) so that view would be scrollable (or non scrollable) depending on text length.
Pretty much, like I can set Scrolling Enabled for textView, except I wan to scroll whole view because of images and labels I have on that view.
EDIT. Basically - how to guess textView height based on string, in order to have proper size textView

To make your scrollview content scrollable you need to set the size of your UIScrollView content ( your textview ), e.g. use:
scrollView.contentSize = CGSizeMake(textview.frame.size.width, textview.frame.size.height)

Related

Only scroll UIScrollView, if the label in it is bigger than the View respectively

I have a little problem with the UIScrollView. It scrolls way to far. Is it possible to enable it only, if the label in it is bigger than the ScrollView?
Pictures (sorry, they are quite huge):
The Label, that is on the ScrollView, is in the ScrollView
You should try using a UITextView instead of a scrollview. It looks like you only need text. A textview does NOT have to be editable. You can disable the editable check box in the storyboard. Take a look at my screenshot.
If you do want the scrollview method to work, you will have to add a bottom constraint to the label in your scrollview. It will tell the content view of your scrollview the size of the content within the scrollview.
The easiest way this could be done is through the Storyboard (auto-layout enabled).
The main idea is to wrap your views (Labels) inside the UIScrollView with a UIView instead of directly inside the UIScrollView, then you need to set the constraints of the UIView to match the UIScrollView from all sides and add a height and width constraints equaling the UIScrollView. The height constraints priority should be lowered (lets say to 750 instead of 1000) so it will allow the scrollView to stretch when needed and the UILabel's button constraint should be set to greater or equal to allow the UILabel to take it's size and "push" the UIView when it needs to.
Last but not least, you need to lower the vertical content hugging priorityof the UIScrollView and the UIView to 249 and raise the vertical content compression resistance of the UILabel to 751.
Here is a full working example

Scrollview doesn't scroll with multiline label in it

I have a multiline label inside a scrollview. I set up the content size, let's say to scrollView.contentSize.height = 2000
But the view doesn't scroll. There is barely any code in the project. What is going wrong?
The only thing is that I don't have constrain for the height of the label, because it will vary depending on the length of text.
It doesn't matter about the height. But what does matter is that you need to pin it to the bottom of the scroll view also.
By pinning it on the top and bottom it will use the label to set the content size and so allow it to scroll.
I suggest to add a UITableView instead of UIScrollView, adding one UITableViewcell that contains a UILabel. By setting the appropriate values of:
Label's constraints.
tableView's rowHeight.
tableView's estimatedRowHeight.
It should works fine for your case.
For more Information about setting a dynamic cell height, you might want to check this answer.
Hope that helped.

Add view under text view

I have a fullsceen textView including much strings, so height of this view automatically changes. Its works nice, but i need to create some newView under this textView.
If I just make constraints like bottom of screen = bottom of newView, top of newView = bottom of textView, so newView will overlap textView.
try add height constraints to your textView with priority is 750.
Now you can build your app and run it again!
Goodluck
Just add your text view to the new view.
Here you are adding view with same size of textview. So it will over lapping. But if you add your text view subview of view so your main view will go back and text view will come front.
I think the problem is your views are trying to fulfil their constraints on fixed screen size.
If you want to keep it on one page (no scrolling) you can either add height constraints to your text view such that its height won't go over certain value or set the bottom view's constraint priority higher so that even when the textview expand it won't overrule bottom view's constraint.
Otherwise you might want to add ScrollView underneath your views. This way when your textview expands the bottom view will get pushed to the bottom outside the screen and you can scroll down the page

Setting UILabel to width of screen and height to fit contained text

I am trying to layout a detail view of my application. The view has a title, some information, and a description. I would like to set the width of my title label and the description label to be the width of the screen and the height of those labels to accommodate the contents of the labels. I also want the entire view to scroll if the height of the content is too large to fit on the screen.
I currently have a xib with the labels inside a view, with autolayout constraints to position the objects and set the width of the title label and description label to the size of the containing view. That view is inside a UIScrollView but does not have any constraints on it. I set the labels to accommodate multiple lines in the attributes inspector. I set the width of the view with the content and the scroll view content size and frame in my viewDidLoad method.
Currently I cannot get the labels to be the width of the screen and the height to automatically accommodate the text. Am I on the right path or do I need to try a different method?
To accommodate the height of the text inside, use 0 as the number of lines in the UILabel. That will automatically make them grow according to the text inside. You will also need to add a constraint from the last element inside your UIScrollView to the bottom of the scroll view, to avoid ambiguous content size.
To get the labels to be the width of the screen, it should be a straightforward constraint from the label to it's container. However, if the label is within the scroll view, you might have to constrain the label to have the same width as the scroll view, because constraining the label to the leading and trailing edges of the scroll view will constrain it to the scrollview's content view, which grows according to its contained views.

Scrolling in TextView

I have a UITextView with Scrolling Enabled.
My problem is that the TextView is small, width for two lines, adding successive lines the scroll does not appear to move around the TextView.
In short, I need to know how to implement the "scroll" according to the TextView grows.
Thank you!
If there are more content in text view with respect to size of text view then by
yourTextView.scrollEnabled = YES;
will scroll add the scrolling behaviour in your text view.
But if your text view's content is equal or less than to your text view's size then there is no need to scroll the text.
By default the TextView scrolls in vertical direction. So, when your content inside TextView will become more than its frame.height, it will automatically allow you to scroll vertically.

Resources