Wrap content of UILabel inside a row cell in swift - ios

Being an android developer, when I want the width and the height of something to wrap its content I simply change the width and height value in the inspector to "wrap content" and it's done. it does not seem as simple as that in Xcode. What I'm trying to achieve is the design of a messaging app, i.e.:
As you see each row has it own height and width of the UILabel according to the text size.
I tried to look for solutions using auto layout and stuff but with no success!
I was able to have dynamic height but the width of the UILabel is always stretched when used and I'm kinda lost. any straightforward solution in order to achieve this or a hint on how to use the auto layout to get the desired results ? Thank you.

labelName.sizeToFit()will change the frame of the label to fit the text

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.

Dynamic UILabel height depending content

I'm populating an UILabel with JSON data and I want it to adjust its height to the content it loads. Also the rest of the labels in the view should move. How can I do this using Swift?
Use autolayout.
You can give priority to the label you load from the json and shrink other labels.
This tutorial is a good place to start with auto layout - http://www.raywenderlich.com/115440/auto-layout-tutorial-in-ios-9-part-1-getting-started-2.
Visit http://nscookbook.com/2015/06/ios-programming-recipe-36-a-fixed-width-dynamic-height-scrollview-in-autolayout/
You can do that either in the storyboard or in the code
I would consider putting the labels in a UIStackView if your app requires iOS 9+. If for some reason the labels aren't resizing properly when you change the text, you can force it by calling [label sizeToFit];.

How to work with dynamic cell height using auto layout in iOS

I want to change the height according to content of cell. I don't want to calculate height every time for each text. This should also work for any with of text means label should update on its orientation change also.
Is there any feasible way to do this?
Please refer link below. This is the best way to do exactly what you want.
http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout

Working with AutoLayout using UITableViewCell

I am working with autoLayout, I want to place a UIlabel on UITableViewCell, which should be always at the right of the cell and the center of right.
So here is what I want to achieve
So here you can see label named RC which I am talking about.
So here my cell height changes dynamically, so whatever be the text height I always want to be the center right.
2) Now as the cell height depends on the text of the label sentence. I want to make sure that if the cell has the default height and it will not decrease below it. It can increase to any height.
Select your label go to editor then select vertical center in container, also fix it using the trailing space from super view and set fit size to content and it should work. For dynamic tableViewCell height created using autolayout find the TUTORIAL. This will help you a lot.

UILabel that has unnecessary padding in a cell

I'm trying to make a Facebook clone for practicing iOS and I can't see why a label I have on my news feed gets unnecessary padding.
It only occurs on some labels, others on my news feed turn out fine. However for a select few there's a block of white pace above and below. At first I thought it was an alignment issue so I changed the labels background to green to show that the constraints hold out.
Anyone know as to why it's placing the padding, only for a select few?
By default UILabel will center its content vertically. Therefore, if label.bounds.size.height is greater than size of the text, the label's instinct is to center the content vertically, which will results in the vertical padding that you see in the attached image. Ensure that the label's height is being set according to the height of the text it contains and the problem should go away.
As dbart pointed out your label's frame is likely higher than the text needed. You can fix this by calling -sizeToFit. You can also check the amount of space the label is actually using for text by using +textRectWithBounds:maximumNumberOfLines:
If your cell is using autolayout to determine the height of the label you should set the preferredmaxlayoutwidth to an appropriate value (for example table width) before laying out the cell.

Resources