I'm fairly new to iOS development and am having a hard time sizing a UITextView to it's content. I've found several examples of calculating a strings render size and/or updating the frame size of a textview, however I can't seem to get those to work.
When I add a UITextView to my view it adds a Height constraint which I think is locking it to a specific height. How can I work around it, update the constraint via code, and/or get my UITextView to resize vertically to show my content?
You can create an outlet to the vertical size constraint, and then adjust the .constant property of the constraint to amend the height of your text view.
A UITextView inherits from UIScrollView. As part of it's logic, it changes the height of its contentSize property to allow the user to scroll vertically to any portion of the TextView's content.
I would recommend using the ContentSize property of your UITextView to figure out how "tall" your content really is. You can then set the frame of your UITextView accordingly.
OR: You could figure out the size of your text using the right method from the NSString+UIKitAdditions category.
If you don't want autolayout then disable it in the xib file.
If you want to change the contraint and it's value this tutorial will help you to deal with autolayout.
Related
I googled but have not found working solution, I have UIScrollView adding inside UITextField and UIButtons with NSLayoutConstraint, I don't use storyboard and setting everything programmatically
my issue is: can't calculate dynamic height of UIScrollView.contentSize because I am using AutoLayout
also tryed UIScrollView.layoutIfNeeded() but no luck
can any one guide or advice me how to to get it work?
#DonMag's comment was right solution to my problem
stackoverflow.com/a/44933358/6257435
if you have in UIScrollView objects with AutoLayout don't forget add .bottomAnchor to last item
Sum of scrollview subview’s height and constraint constants is the final height. Even after constraints layed out, it’s always summ. So just calculate it, and you height. But why do you need calculating scrollview’s content height? If it’s adjusting wrong, set your subviews height constraints, top and bottom contraints to scrollview, and content size will be calculated automatically
I just put constraints into my ViewController, and I set the UITextView so that it keeps its height and width, yet at runtime it shrinks to only cover the text inside of it. How can I avoid this?
Thank you!
In my opinion, at the run time, the size of your TextView has changed because of the content, I mean the text set to it. To prevent this kind of issue, I would suggest some options which will be regarding your UI
At first, if you intend to fix size of the TextView, so from the constraints or Interface Builder, please create width and height constraints then fix the value. For example
If you wish your text view will be dynamically in size based on contents, you can set width and height values are greater than or equal a minimum value as it will not break your UI
I hope this would be helpful.
I assume you're using Interface Builder. Check the value for Content Hugging Priority that IB set for the text view. If it's too high, it may be overriding your constraints.
I read that you should not normally use setFrame when you are using autolayout.
However, if I have a constraint from a UIButton to a UITextView, lets say the UITextView's height changes and in the IB I set the UIButton's constraint to be 10 vertical unit spacings apart from the UITextView. When I change the UITextView's height by using setFrame, is there anyway to get the UIButton to automatically recalculate it's y position based on the constraint?
Thanks!
You really shouldn't modify the view's frame when using autolayout. You need to modifiy the constraint, wether by resetting it programmatically or by adding an IBOutlet that references the constraint and modify its constant value.
You might want to take a look at this article
Using Xcode 5, interface builder and developing for iOS 7.
Within my content view I have 2 additional sub views, one on top of another. Within the upper subview I have a UILabel. I would like for that UILabel to expand in height when the content exceeds the first line, but I can't seem to get the height increase of the UILabel to increase the height of the subview, thus pushing the bottom subview down the main content view.
Additionally, I would assume the content view would need some sort of a constraint that reflects the overall height of the two subviews?
Perhaps this the question has already been answered somewhere, but I've searched everywhere and can't seem to come up with a solution.
Any help would be hugely appreciated! Thanks in advance.
There is a couple of steps that you have to do to achieve this using autolayout.
Set layout constrains for the label.
Set height constraint with low priority.
Set numberOfLines to 0 to allow multiline text.
Set preferredMaxLayoutWidth for the label.
The preferredMaxLayoutWidth is used by label to calculate its height.
This property affects the size of the label when layout constraints
are applied to it. During layout, if the text extends beyond the width
specified by this property, the additional text is flowed to one or
more new lines, thereby increasing the height of the label.
Also, have a look here.
My Goal
I'm creating a custom UITableViewCell (MessageCell), and I'd wish to have my UITextView resizing both in width and height whenever the cell changes its size, while keeping some basic constraint like margins to the edges.
Note : I also change my cell's height in the heightForRowAtIndexPath method depending on the content which will be placed into UITextView.
What I tried
Here are all the constraints currently applied & the cell :
I tried :
With a UILabel.
To fix a Vertical Space from the bottom, thinking it would change the height to fit all the constraints.
To override Height with an User Constraint Height >= 43 (my initial height), but the purple-automatic Height is re-created again whenever I do this.
To find a solution on SO first, but didn't find a case like (even if the UITextView's height's resize with autolayout seems to be a frequent issue).
Plenty of combinations of various & random constraints until my fingers bleds.
How it render now
So...
If someone has any clue or guideline to achieve my goal, I'd appreciate!
I might add, I'm totally new to Autolayout & constraints. My reference : Ray Wenderlich's awesomeness
You need to get rid of that height constraint that the text view has. Make sure you have constraints to something above and below the text view (usually the top and bottom of the cell), and then you should be able to delete that height constraint.