Auto sizing UILabel when using Autolayout - ios

I'm creating a xib using autolayout (NSLayoutConsraint). Is there a way to use sizeToFit or something similar to make a UILabel fit its contents?
I've tried setting a constraint of "greater than or equal to x" and tried setting the height to be below this. However, it keeps overwriting the height property. sitToFit doesn't seem to work when you have NSLayoutConstraints.
How can I get the label to size appropriately?

Make an IBOutlet for your width and height constraints, and use sizeToFit to change their constant property, and call needsUpdateConstraints

Using a "greater than constraint" for the height of the UILabel and
calling sizeToFit within viewDidLayoutSubviews did the trick for me
I did not need to call needsUpdateConstraints.
You also need to set the number of lines to 0 to allow an unlimited amount of lines or > 1 if you want a limit.
Also the property preferredMaxLayoutWidth needs to be set, which is automatically set to the views width by the interface builder.

You should try to lower your height constraint priority.

Related

Constraints making UITextView smaller

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.

How to set default height of the UILabel in objective-C?

I want to set the (no text) height like the UIlabel have a line text 's height
With AutoLayout:
add a constraint of height to the label!
With no AutoLayout:
set the frame will work!
Use autolayout constraints. simply put a constraint on height but make sure to put other constraints too else constraint bugs will occur. to check thatits occuring...set the background color to grey and you will see the results.

Automatically refactor objects from Autolayout

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 autolayout to manage height of UILabel within a subview

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.

Set height constraint for UITextView

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.

Resources