How to set labels text align to top UITableViewCell in iOS - ios

I have tableViewCell which contain two labels with title and subtitle .Both have numberOfLine 0 with sizeToFit in cell class.But if text of one them is less then label text with less one, comes at center as
Cell height is base on Label text size and if both Label text have same number of line then it appear good . So i want text of both label align to top.Cell height should be base of text of Label with more number of line.

This can be achieved in auto layout by setting the bottom labels constraint to >= rather than just = to the bottom of the cell in the xib. That way it should automatically fill the least amount of space so that text vertical alignment is no longer an issue.

Related

Sliding Text In a Label

In my Xcode Project I have a tableview that is populated with a label. Sometimes the text in the label does not fit all the way causing the label to cut off the rest of the text. So I will like to add in a sliding effect in the label where it will show the beginning text and then start to slide all the way to the end of the string and back only when the user is not scrolling in the tableview. How will I implement this?
Just assign the leading, trailing, top, bottom constrains of this label = 0 with the cell itself and let the tableView to calculate each cell's size dynamically

Is it possible to get part of tableview cell to disappear by reducing it height at tableview delegates

I have a table view cell which has three elements vertically
a title label
a text box
a validation label
I want to show validation label only when it fails.
If I just hide the validation label then the space occupied by the label is stil there and it looks awkward having more distance between the next row
so what would be the best approach to handle the scenario
Change the table-row height whenever validation label's hidden status changes
I think this approach probably will not work because label is still there in the tableview cell so the tableview cell will readjust itself to have the label height there irrespetive of whether it appears or not
Change the label height constraint constant and tablerow height whenever there is change in label's hidden status
so basically if I want to hide it then I will be setting the label's heigh to zero and readjust the tablerow height ... it'll work but there is a problem..
If I set label number of lines zero then depending on the content label will change its content height .... but as I am using constraint for height... I will be hardcoding the height and it can lead to problem again.
Basicallyy my need is I have one label and text box whose height is fixed and I have a one more label ... this could be hidden sometimes and this could be one line or 4 lines
I want the row height to adjust according to that(0 lines(hidden),1/2/3 line label visible)
sometime even the first label that I was talking it could go to the next line in smaller devices like iphone 4s
so somebody help me out on the issue

Dynamic table view height

The idea is to resize height of the cell automatically based on few controls heights. As you can see on image above I have Top Label and Bottom Label. These two labels can have different height based on text length.
Few words about setup in storyboard.
I set number of lines to 0 for 2 described labels to allow grow their height dynamically based on given text.
For the Top Label I have next constraints:
For the Bottom Label I have next constraints:
So I we say about vertical spacing between 1000 green label and bottom label it's every time the same:
But without this spacing cell won't stretch height. How can I reduce this vertical spacing? Because there is to much spacing between "1000 green label" and bottom label in case if top label have big height because of text.
In -viewDidLoad method I set:
[self.theTableView setEstimatedRowHeight:145];
[self.theTableView setRowHeight:UITableViewAutomaticDimension];
Seems it works pretty cool sometimes with a bug described here but I don't know how to restrict that vertical spacing:
http://www.appcoda.com/self-sizing-cells/
Set the bottom label's top constraint to be >= 11.5 (or whatever its minimum spacing should be).
This will let the cell adjust that vertical spacing, depending on the other content in the cell.
Update:
In iOS 9, this would much more simply be handled by UIStackView.
A horizontal stack view would constrain (and determine the cell height based on the) two inner vertical stack views. The left vertical stack would handle the image, banner, and label layout, and the right vertical stack would handle the top label, 10000, and bottom label layout. You'd only need with 4 constraints (for the horizontal stackView to constrain it to the contentView).

Align UILabel Text to bottom left corner

I have a UILabel that is positioned on top of a UIImageView. The text can be various lengths, so it is necessary that I make the label as large as possible (The same size frame as the imageView). The problem is, I don't want the text from the label to cover the image by being directly in the center of the image. It's easy to set left alignment in a UILabel. Any clue how to set vertical alignment as well? Basically I want the text to hug the bottom left corner no matter what length it is without being cut off.
You can accomplish this with Autolayout. If you set up your constraints so that the label is fixed to the bottom left, but don't set a height constraint, Autolayout will use the label's Intrinsic Content Size. This just means that as the label gets more text / changes font / etc, the label will grow in height and will only ever be tall enough to contain all the text in the label.

Constraints to specify when view size is dependent on multiple subviews

I am using custom cell in UITableView. There are 4 views in cell. 3 UILabels and 1 UIImageView (grey colored one) as shown below. Text of labels are dynamic so width and height is dynamic.
Here width of UIImageView depends on 2 labels. So issue is to specify constraints between labels and UIImageView that will decide the width of UIImageView.
See below what happens if first label's text is long.
How to specify constraints when width depends on width of multiple labels?
Update : I tried setting number of lines for labels to 0 and intrinsic size to placeholder. It's not working, too. Below is how it looks. I tried setting intrinsic width and height to none, but it gives error when I do that for both label, not giving error if set that for only one label.
Update : I changed properties and below is displayed what are they now and how the cell is displayed.
Cell :
Constraints for label in first row (pink colored) :
Constraints for label in second row (cyan colored) :
I solved this after experimenting with lot of things. The only thing I had to do is to set horizontal and vertical content compression resistance priority to required.i.e. 1000.
I did this for all labels because I don't want any of the labels to trim their content.
One more thing which is too much important is Getting Right Height Of Cell. If there is even 1pt of error in calculating custom cell's height it will not be displayed as expected.
Hint :
If height of any view is greater than expected then possibly calculated height of cell is greater than what is actually required.
If any of views is shrinking vertically or not displaying whole content then possibly calculated height of cell is lesser than what is actually required.
Yoy can test if height is wrong by adding/removing constant value to height (variable) you calculate for cell.

Resources