Adaptive labels iOS - ios

Please help me. How to make adaptive labels for iPhone 5 and iPhone 6?
Labels on the left side should change its width.
Label on the right side should be right-aligned.

You should use Self Sizing Cells. Place the following lines of code in the viewDidLoad: method of your View Controller:
tableView.estimatedRowHeight = 36.0
tableView.rowHeight = UITableViewAutomaticDimension
The estimatedRowHeight is your custom value.

Using Autolayout first fix Left label from left side by assigning Leading space to SuperView.Same way fix Right label from Trailing.
As the right label has fix width it will be fix from right and as per the content size of Left label will vary. and if content increases the width and 'no of lines' property is 1 then it will truncate.
if you want label Left's height increase and decrese according to content then you can go for estimatedRowHeight property of UITableView.

Related

Two stacked dynamic height labels in UITableViewCell

I am trying to have two labels stacked in a UITableViewCell. I have AutomaticDimension and estimatedRowHeight set on my tableView.
I want the cells to show up stacked, with their dynamic heights if they both have content.
If only one has content, I would like it to be centered in the cell, or rather have the cell collapse to only show that label.
I have tried every constraint combo I can think of, but usually the labels either do not expand, or I end up with filler space where the label should be.
If the label has no text, I am setting label.text = "".
I am calling sizeToFit() on the labels each time I set the text.
What is the correct constraint combo? Here is what I currently have to start with.
you have to make two labels with leading, trailing, top and bottom constrains and don't add height constrains
make the number of lines for labels = 0
check the title label and from size inspector menu click on the vertical content hugging priority to be 250 not 251 as shown in the screen
image
in the viewController connect your tableview and add these lines
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 44
This could be done using StackViews in iOS,
in the Storyboard Select your both Labels and embed them inside a UIStackView
one of the benefits if you declared a good constraints it should automatically handle the requested behavior.
I recommend this tutorial to get started with UIStackView here
Better solution is UIStackView.
If still you want to go with UILabel you can try one more thing:
Setting label.text = "" will decrease the height of label but the constraint for distance between the labels will not make the label in centre.
For that add a referencing outlet of distance constraint and set it :
distanceConstraint.constant = 0
programatically while setting the label.text = "".
This will set the label with data in centre.
Hope this helps.

Label cut off on Table View Cell

The top and bottom text label for my tableView cell appears to be cutting off. It is not clipped in the Xcode preview or the storyboard view. How do I make the labels extend to fill the entire row?
I am using a stack view that contains the file name and file size labels.
There are a couple of "potential solutions":
set your constraints (especially the height constraint of the label) correctly
set your cell height correctly. (You probably want to use automatic height)
The layout would be broken if you didn't match these 2 conditions.
This thread will be helpful: https://stackoverflow.com/a/18746930/938380
Maybe you just forgot to set tableView.estimatedRowHeight = ...
Increase the height of label then it will look what you are expected size of the Label.

Simple row layout using Autolayout constraints

I've a simple layout that I know how to do it with a horizontal stackview. But for learning purpouses I want to know if it is posible to code using AutoLayout constraints.
The layout is simple, it's a rectangular row that contains an image and a label.
The problem is that the label can have multiple lines and be bigger than the icon.
The desired layout is that image and label had to be centered in Y axis and the bigger height one have a margin of 8 points at top/bottom.
You should give the image and the label a margin constraint for both top and bottom. Use greater or equal as relation for these margin constraints.
Give a top and bottom margin constraint for image and label. Set high priority with relation Greater Than or Equal.
First Write this in viewDidLoad
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 44.0;
Set numberOfLine property to 0 in StoryBoard
Set UIImageView Constrains like:
Fixed Height and Width and Vertical Center in Container.
Set UILable Constrains to HorozontalSpacing from UIImageView Trailing Top Bottom from SuperView i.e Cell.
Hope it will work...

UITableViewCell not dynamically resizing using Swift

I am trying to dynamically resize my UITableView Cells in swift:
Above is my cell setup for the smallest possible screen size (iPhone 4). The green view (an image) will enlarge if the screen width increases and retain its aspect ratio. It's pinned to the top, leading edge trading edge and aspect ratio enabled. The label below is centered horizontally. The top pinned to the bottom of the image and the height and width fixed.
I have this code in viewDidLoad:
tableView.estimatedRowHeight = 262
tableView.rowHeight = UITableViewAutomaticDimension
If I run the app I get the warning:
Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead. The cell is also the wrong size only about 25pts in height. What am I doing wrong here?
First if all, check out this link. You may be missing an overload:
custom UItableView not displaying correctly on ios8
Second, look at your constraints again. Just for the vertical dimension, you've set the distance from the image to the top, from the image to the label, and from the label to the bottom.
The size of the label will be given by the text.
But the size of the image is uncertain. You have a leading constraint and an aspect ratio, but how does that specify a size?
Basically if you were to render this yourself, would you have enough information to do it?
Try add constraint: label's bottom to cell's bottom
Remove label's fix height.
Hope this can help.

Setting constraints for dynamic cell

I'm new to using constraints in my iOS projects, and trouble setting the right constraints for my dynamic UITableViewCell. I've tried every combination I can think of but it either won't dynamically change height, or it gives me warnings about ambiguous layouts.
My first label1 is not supposed to change in height, but the other two are. My current constraints achieve the desired effect, but are giving me the warnings seen under.
The warnings go away if I constrain the height of the labels (obviously), but that doesn't solve my problem.
Any suggestions on how to solve this would be appreciated!
First of all you need to set
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44.0
in viewDidLoad of your UITableViewController
Then you need to set constraints from top to bottom of the cell
top space to superview
vertical spacing
bottom space to superview
height greater or equal
and of course set lines to 0 for every UILabel
Example project is available here https://github.com/MihaelIsaev/SwiftAutoResizableCells

Resources