Fitting text in UILabel inside a Table View Cell - ios

I am using a TableViewController with "Right Detail" style cells. To fit the text into the cell.textLabel I use cell.textLabel?.sizeToFit(). But the problem is, that the cell.detailTextLabel gets pushed out of the view. Whats the best way to solve this problem?
This is the result I am getting right now

I haven't used .sizeToFit(), so i can't say what it is actually for but you don't need it. Lower the compression resistance of your label.
Content Compression Resistance explained:
Sets the priority with which a view resists being made smaller than its
intrinsic size. Setting a higher value means that we don’t want the view
to shrink smaller than the intrinsic content size.
Read more about it here: https://medium.com/#abhimuralidharan/ios-content-hugging-and-content-compression-resistance-priorities-476fb5828ef

I would suggest make your own custom cell.
1.you can crop left label if it is too wide.
2.expand left label to multiple lines and make cell height self constraint.

Related

Strange behavior of custom UITableViewCell label

I don't know why, but my label behaves strangely. You can check screenshots bellow. Image should fill stackView.
Screenshots
Since you really ought to share a bit more, I can only try to answer based on what's available.
The problem is most likely due to the fact that the StackView that surrounds the label and image does not have a width or height constraint of any sort and/or the imageView has a very low content compression resistance property.
Because StackViews calculate their size based on their child views, it will try to find the best fit. If the label has a normal compression resistance but the imageView has its resistance set to low, the stackView will simply calculate its frame based on the label alone.
So to fix it check the compression resistance of those objects or define contraints for the surrounding stackView to match its parents width.
Firstly.. What is it that you are trying to achieve? Image and Text one beside the other, or one below the other? You might want to check for the stackview direction property
Secondly if you want that your image size remains constant, you will have to give your image view some fixed width constraint, and stackview will respect the size of the imageview. Or if you want that the image takes the entire width of the screen, you can basically pin the stackview to the edges of the screen, after you have put in the imageview inside of the stackview.
Hope that makes sense.
Comment back if you need more help :)

Determine width of UITextView in heightForRowAtIndexPath with auto layout

A rather convoluted problem, looking for an elegant solution:
A UITableViewCell subclass has a UILabel on the left and a UITextView on the right. Both label and text view have dynamic content. I am using the text view rather than another label because I need the address link recognition.
The label has a higher compression resistance than the text view, so that its right edge will expand towards the right as needed (up to a certain limit). The text view's width will in turn be compressed. The text view is set to word wrap to expand downward.
Because a text view is a itself a UIScrollView, the normal automatic dimension mechanism of a dynamically sized table view cells view will not work (i.e. using UITableViewAutomaticDimension). Thus, I tried to implement heightForRowAtIndexPath. To determine the necessary height, I thought just need:
The text in the label, plus its attributes
The text in the text view, plus its attributes
The width of the table view (as it is "plain" rather than "grouped")
With this information, I reckoned, I can use sizeThatFits on helper objects to determine the necessary height.
However, I am reluctant to hard-code the horizontal margins between the label and the text view into my size calculation, so theoretically I additionally need NSLayoutConstraint outlets for
The left margin of the label to its superview
The max allowed width of the label
The horizontal distance between the label and the text field
The right margin of the text field to its superview
This seems really insane! While I was writing the implementation of this, I asked myself if there is not an easier way to accomplish the same. (That's why I did not yet write the code to post here.) After all, the only hard problem here is how to get the available width for the text view, but the way to getting there seems unduly complex.
Any ideas for a concise, elegant solution?
Side notes: This has to be calculated before the cell is rendered to be displayed. Also, setting the text view's scrollingEnabled to false produces weird results when using sizeThatFits.

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.

UILabel is not resized with autolayout

I set inset constraints for 2 labels, and width is set to fixed value. number of lines is set to 0.
What I expect is the UILabels are resized to fit, and the parent view is also auto resized. But the output is the container remains the same size but the sub uilabels are either too high or too short. What's the problem? Thanks.
Here is the constraints
Try setting the content hugging priority to a higher value. This affects how resistant a view is to expanding past its intrinsic content size.
The image of UILabel looks like it is truncating the contents. So if you want to display the text in the next line if it is bigger then you need to use the below api.
label.numberOfLines=0;

Allow UILabel to grow dynamically using auto layout

I have a label which is going to contain a big description. I want the label to continue growing on new lines. In the image, its the label which starts with event_venue.....
The even_venue.. label has 3 constraints for now:
Vertical space with eventt_title
a horizantal space with the leading of the superview
a width constraints which defines that the label width is always less than the superview.width.
What I want to acheive is to make the event_venue.width less than superview.width, but if it has more text, it should display in new lines. Is this possible using autolayout?
This are possible steps which can create expandable UILabel
Set layouts for UILabel fixing its position
Set number of lines = 0
Set content vertical compression resistance to 1000 (this will allow text to push the label)
Since you want UILabel to expand you cannot give it fixed height constraint or its parent fixed height constraint. Sometimes depending upon condition giving height constraint is necessary to avoid error then you need to set its priority lower than vertical compression resistance
Yes, this totally is possible. I see answers here that are close to solution but not complete. Here is a solution which works with auto layout in Storyboard, no coding of sizeToFit or anything. Your modified steps would be:
Vertical space with eventt_title
A horizontal space with the leading of the superview
A horizontal space with the trailing of the superview
Set UILabel's Line Breaks as Word Wrap.
Set UILabel's lines property as 0.
I have solved a similar problem. I had to make a label that had a variable amount of text. Here's what I did:
In the storyboard, place your label with the origin where you want it.
In the Attributes Inspector, "Label" section, set the Line Breaks = Word Wrap
Fill the label with random placeholder text to the maximum shape you want. For example, if you wanted to fill the whole width and have room for a maximum of three lines of text, you could do:
abcdefghijklmnopqrstu
abcdefghijklmnopqrstu
abcdefghijklmnopqrstu
In the code, set the text of the label using setText:
[self.myLabel setText:#"MyLabelText"];
This did it for me. Your situation may be a little different in that I wasn't changing the width of the superview and it sounds like you might be. But if the width constraint is set on the label then I would expect this to work in your case, too.
I had a similar question about label resizing, and the answer that I found that was useful to me is here: UILabel Auto Size Label to Fit Text. This is a good starting source for code on how to resize your label programmatically.
I would recommend that you also add a horizontal trailing auto layout constraint from the label to the edge of the superview. Do that and you can then get rid of your current width constraint.
AutoLayout facilitate you for orientation purpose. I don think it will give you automatic expansion. You have to define label with width and height completely, otherwise you will see dots at the end of label. So you may use UITextView expanding it all over the screen. And set textView.backgroundcolot = clearColor.

Resources