UILabel with 0 lines doesn't initially show full text - ios

In my UITableViewCell, when I load data into it, I have 2 UILabels which can have an unlimited number of lines.
I handle both of them like this:
label.numberOfLines = 0
label.lineBreakMode = .ByWordWrapping
When I initially load data into the UITableView, for some reason sometimes the cells on the screen don't yet show the whole String in the labels. Instead they just don't enlarge themselves.
However, when I scroll down so that they're off the screen and then scroll back up, everything looks perfect.
Why is this? How do I fix this issue?
In my cellForRowAtIndexPath I basically use dequeueReusableCellWithIdentifier(:forIndexPath:)
then, I do cell.configureViews(...) and return.
In my PostCell I do the label editing above in the override func layoutSubviews()

You might have forgotten to set the 0 lines property on the storyboard too...
Go to your Storyboard, select the label, and click Attributes Inspector.
Under Label you should see "Lines 0" like in the screenshot below:
Also, make sure you are handling the excess of characters the way you want - below is an example of the settings to wrap the text to a new line:

You need to calculate your UILabel height and use it as cell height before the cell drawn.
Try this to calculate the UILabel height UILabel height calculation

Related

Why is tableView cell not dynamic?

Update:
I have found that the problem occurs because of the date labels top constraint when removed the wrapped text becomes visible. The issue is then the date label is under the other text. Could the real problem be that the cell is not dynamically changing based on the constraints?
I am trying to make text in a UILabel to wrap (at the same time I am trying to make the cell change based on the text)
In the .xib I setup the cell, doing this:
self.notifInfoLabel.numberOfLines = 0
self.notifInfoLabel.lineBreakMode = .byWordWrapping
sizeToFit is called right before returning the cell in the delegate method.
cell.notifInfoLabel.sizeToFit()//EDIT: turns out this is not needed
return cell
}
The configure method is called on viewDidLoad and right before .reloadData()
func configureTableView() {
self.tableView.estimatedRowHeight = 82
self.tableView.rowHeight = UITableView.automaticDimension
}
Currently, the text in the label seems to wrap underneath the date label...
How can I make it work as intended?
You have confusing constraints for Auto layout engine.
You want some space from bottom of image view if text is small , And
you want text to dynamic in that case label should grow.
Solution is very simple.
Bottom constraint of Imageview change = to >= with 750 priority
Bottom constraint of Date change = to >=
Hope it is helpful

Xcode: Table View Cell not automatically adjusting its height

I'm having a problem with my table view cells as they do not adjust automatically with its content.
I have a label for a title and another label for a name. There is a text view below the two labels which is never displayed when the simulator runs.
This is
what the Table View Cell is supposed to look like, however, this is what the Table View Cell displays.
I have pinned all elements inside the table view cell to the content view using constraints. I read up that adjusting the table view cell height itself will not work, so, I have to adjust the height from the table view itself.
It is set to automatic but it is not adjusting as seen here. I have also tried to set the estimated height to automatic but to no avail. The only solution was to set a custom height but it would look extremely weird if the text view contains only a few text as there would be a large white space. I did not add any code at all to adjust the size.
These are the following constraints:
Table View
Name Label
Title Label
Text View
First You need to add height constraint for textview and add its IBOUTlet then you need to override the updateconstraint of cell and do following in update constraints method.
override func updateConstraints() {
super.updateConstraints()
self.textViewHeightConstraint.constant = self.textView.contentSize.height
}
and also for name label add bottom constraint.
By default the UITextView will not resize itself to fit its content. While you could use #Waqas Sultan approach, I would recommend to let the textView size itself. To achieve that, just use this:
textView.isScrollEnabled = false
Or, in storyboards, find the Scroll Enabled attributed and uncheck it.
This would make textView to size itself.
However, from the constraints you show it is hard to tell if there are really enough constraints to determine the proper frames for all the content - there are constraints related to Review label, but who knows how you constrained that label.
Not seeing all the relevant constraints in the tableView cell, I cannot guarantee that this will be enough to make it work as you expect (you might have forgotten about just a single one constraint, and it might be messing up your whole layout).
Hey buddy i would like you to try this way.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == desiredIndexPath { // the index where you want automatic dimension
return UITableViewAutomaticDimension
} else {
return 100 // the height of every other cell.
}
}
note: Make sure that you do not give height to the label. Otherwise the label wont expand according to content.

Self sizing cell with multiple labels does not show multiline label

I'm having three labels in my static table view cell and the middle label should be a multiline one.
I'm setting these two lines in viewDidLoad()
self.tableView.estimatedRowHeight = 130.0
self.tableView.rowHeight = UITableViewAutomaticDimension
The storyboard looks like this
Here are the constraints for each subview
Top label, middle label, bottom label, button
I've also set the number of lines for the middle label to 0. However it only shows one single line, instead of multiple lines. I guess it must have something to do with content hugging or content compression priorities or it's because my I'm using a UITableViewController with static cells.
UPDATE
If I change Vertical Compression Resistance Priority of the middle label to 751 and Vertical Content Hugging Priority to 250, the label shows multilines, but the cell does not get resized, so the top and the bottom label are outside the cell now.
UPDATE 2
I've just created a sample project and it turned out that it works with dynamic cells as expected but not with static cells. You can download the sample project here: https://dl.dropboxusercontent.com/u/67692950/ResizingCell.zip
I cloned your sample project. I think the problem is that you don't need to set UITableViewCell's height.
This is the one simple solution.
・To set Row Height "Default" in Table View Cell.(Unchecked Custom)
In this case, it works.
Download the sample project that I implemented here:
https://www.dropbox.com/s/w8q6ov9qjfxu1l3/ResizingCell.zip?dl=0
But the other way is that you calculate UITableViewCell's height from UILabel's height.
If you customize cell more complicatedly, it is better to isolate cell as a Custom cell.
Firstly, open storyboard, set your tableview row height to be 100, uncheck custom row height for the tableview cell, which value is currently 100.
Secondly, like #DBoyer said, call layoutIfNeeded. If you see "Unable to simultaneously satisfy constraints, Will attempt to recover by breaking constraint X", lower X's priority to 999. I think the warnings may have something to do with that when you get the cell, cell frame is CGRectZero.
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = super.tableView(tableView, cellForRowAtIndexPath: indexPath)
cell.layoutIfNeeded()
return cell
}
You need to either call layoutIfNeeded on the cell after setting the text of the multiline label or set the labels preferredMaxLayoutWidth
Use aspectRatio constraint for labels. Its puspose is to change width aswell as height according to need.This will help

iOS 8 self-sizing cells - wrongly calculated height for labels and Infinite scrolling

I am trying to get my cells to resize dynamically and I wanted to take advantage of new self-sizing cells in iOS 8. Now, I set constraints to edges of superview and the cells really resizes itself. The problem is that the cell has sometimes wrong height and the text is not displayed all. If the text is longer also the mistake in height of label is greater. I was experimenting if the problem is not caused by newly added margins but it doesn't work either.
When I was trying do the resizing the old way, I discovered that cell.contentView.systemLayoutSizeFittingSize gives also wrong height. There is probably something I am missing in sizing of content view in table view cell, but I can't figure out what it is.
I prepared working example with resizing text and image, for you to test out:
https://dl.dropboxusercontent.com/u/35953801/Cell%20test.zip
EDIT:
There is additional problem, when I try to implement infinite scrolling the reloadTable always scrolls it to the top. When I try to use insertRowsAtIndexPaths instead, it starts to jump to wrong position and glitch very badly.
Here is updated code to test out:
https://dl.dropboxusercontent.com/u/35953801/Cell%20test2.zip
EDIT 2:
So as #gabbler suggested, the best way to make reliable infinite scrolling is to do it the old way. I haven't found way how to use the storyboard prototype cells to measure them in heightForRow method. If you try something like this, you get stuck in infinite loop, because dequeReusableCell actually calls heightForRow:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
var cell = tableView.dequeueReusableCellWithIdentifier("CellProgrammatical", forIndexPath: indexPath) as TableViewCell
It might be possible to set it up with Nib resource for cells, but best will be probably to save yourself a headache, follow https://github.com/smileyborg/TableViewCellWithAutoLayout and set up constraints programatically.
There is one thing that needs to be done for iOS8. It adds extra constraint UIView-Encapsulated-Layout-Height to 44 initially, which breaks the cell if it is taller than that. It's necessary to set all the vertical constraints to lower priority (999).
Here is the example once again, with working infinite scrolling:
https://dl.dropboxusercontent.com/u/35953801/Cell%20test3.zip
Thanks for help
FWIW I had this problem when I set constraints between my label and the cell margins instead of the cell itself. Margins are weird and don't appear to be correctly taken into account when calculating preferredMaxLayoutWidth.
Changing my constraints to be attached to the cell itself fixed the problem.
There is a label in your cell which has preferred width being set, remove the setting seems to fix the problem.
Since you have already make the label width to be equal with the cell width, there is no need to set the Preferred Width.
And try to remove these code.
self.tableView.estimatedRowHeight = 600;
tableView.reloadData()
which seems to be redundant here.
I use the following code to calculate height
Here 20 is my font size
UIFont *font = [UIFont systemFontOfSize:20];
NSDictionary *userAttributes = #{NSFontAttributeName: font,
NSForegroundColorAttributeName: [UIColor blackColor]};
NSString *text = self.userModel.quote;
CGSize textSize = [text sizeWithAttributes: userAttributes];
ht = 20;
if (textSize.width > [FunctionUtils getViewWidth]) {
ht = (textSize.width/([FunctionUtils getViewWidth]));
ht = (ceil(ht))*20+35;
}

TextLabel shifts up when detailTextLabel becomes multiline

i'm currently making an app where the suer selects an MKMapView annotation and then the title of the annotation(pin) is set to a detailtextLabel in a Right Detail UITableViewCell.
My Problem is that when the text is large, the detailTextLabel becomes multiple lines. When this happens the TextLabel of the cell(the one the left) shifts up. Heres What I've tried:
In the cellForRowAtIndexPath Method, I tried adjusting the frame through the following code:
CGRect frame = cell.textLabel.frame;
frame.origin.y = cell.frame.size.height/2;
cell.textLabel.frame = frame;
Where cell is a UITableViewCell that is set to right detail
Subclass the cell and tun try to adjust the frame in the -(void)layoutSubviews
How do I stop it from going up and keep it at the center of the cell?
If you want to do a custom layout of UITableViewCell, then you need to add your custom UI elements to its -[UITableViewCell contentView] instead of trying to modify geometry of standard UI elements that are created by default.
So, in your case, you need to add two UILabels and set their position so that:
Title label will not move at all
Detail text label will be also multiline
In this way you'll be able to solve this problem!
Please try to make the font size adjustable amount to the text.
I think you can use,
cell.detailTextLabel.adjustFontSizeToWidth = Yes;
And also set the numberOfLines to 0.
Hope that solves the purpose.

Resources