I have a tableView with the bottom of low hanging characters (g, p, q, etc) in detailTextView being cut off. Changing the font size and cell height doesn't seem to fix the problem. I can't set values of the cells labels so I am out of ideas. Any help?
Here is a pic of an example.
EDIT: I found that using fontawesome is what is causing this. I have no idea why fontawesome is doing this but removing this line of code in my cellForRowAtIndexPath removes the problem
cell.detailTextLabel!.font = UIFont(name: "FontAwesome", size: 16)
Now I have no idea why changing the font is causing this issue, nor how to fix it.
You have to add these two lines of code to your UITableView set up:
tableView.rowHeight = UITableViewAutomaticDimension // 1
tableView.estimatedRowHeight = 50 // 2
// 1 This tells the UITableView that its cells should be as height as their content
// 2 This is needed by the UITableView to calculate its scroll bar height and position. This value does not have to be 100% correct, an estimation on your average cell height is enough.
Related
I have an issue with the usage of textField’s adjustsFontSizeToFitWidth. I have a textField with font size of 50 and a maximum width of 300 (This is a less than or equal to constraint). TextField is placed centre vertically and horizontally using auto layout. Now when I run the app, the text is being shrinked from the beginning itself. I know this may be because the system thinks the bounds of Texfield is not enough… but I gave the control to auto layout to figure that out and I assume it should work. Below is the snapshot… you can see the big placeholder and then suddenly the text shrinks… Any thoughts? Am I doing something wrong, I was trying to avoid manual calculation of width..
SourceCode Sample
Set leading/trailing space to text field like 15 pt from left safe area and 15 from Button so that it could automatically increase the font size. As now it shrinks the font to minimal 17 (which is set in storyboard) as the width is not enough.
Okay!! Finally I found a way, the idea was to check if the width is greater than the defined width limit... when the width is more I set adjustsFontSizeToFitWidth = true else set to false ... for some reason the solution didnt work when I clear the text... then I tried programatical approach and it started working .... dont know what was the difference... anyways I updated the source..... if there is a better solution let me know.
#IBAction func textChanged(_ txtField: UITextField) {
if txtField.frame.width >= (view.frame.width * widthMultiplier).rounded() {
txtField.adjustsFontSizeToFitWidth = true
} else {
txtField.adjustsFontSizeToFitWidth = false
}
}
I have a static UITable view with 4 cells: the first and last cells are custom, while the second and third cells use a built-in style ("Basic" and "Right Detail", respectively). I have the following constraints on the label in the first and last cells:
label.leading = superview.leading + 20
label.Center Y = superview.Center Y
I'm having trouble matching the constraints for my custom UITableViewCell labels to those of the default UITableViewCells (the ones using the built-in styles).
For certain devices (e.g iPhone XR) the X position of the labels match for all four cells (20):
But for other devices (e.g. iPhone Xs) the X position of the labels do not match (16 vs 20):
Note the X position of the first and last cell is greater (20) than that of the second and third cell (16).
At first I thought that the constraints for the default cells were being varied based on the size classes of the device, but both the iPhone XR and Xs have compact width (wC) and regular height (hR).
Any idea how I can match my custom cell's label constraints to those of the default UITableView cell's label?
textLabel and detailTextLabel are present even in a subclassed UITableViewCell. While using textLabel instead of a custom label would solve the left margin issue, there is still a problem with UISwitch on the right side.
However, you could align your layout with a leading constraint of textLabel and a trailing one of detailTextLabel.
if let leadingAnchor = cell?.textLabel?.leadingAnchor {
myLabel.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
}
if let trailingAnchor = cell?.detailTextLabel?.trailingAnchor {
mySwitch.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
}
I'm having this weird issue with UITableView that can't calculate it's content's height properly.
I have custom UITableView class that is embedded in another custom UITableView, I want it to auto-adjust it's height to fit content so I have already:
override var contentSize: CGSize {
didSet {
self.invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
self.layoutIfNeeded()
return self.contentSize
}
And now when I use:
self.estimatedRowHeight = UITableView.automaticDimension // non-zero value like 40 isn't working either
self.rowHeight = UITableView.automaticDimension
the output is the frame that is not full height, when I turn "Scrolling enabled" in this TableView it's scrollable with full content (don't want that):
Now when I change
self.estimatedRowHeight = UITableView.automaticDimension
to:
self.estimatedRowHeight = 0
the output is exactly what I would want to have except the content text is cut...
Here's my CommentCell:
Console isn't showing any errors with autolayout in any case.
Do you maybe know what's going on? I have spent literally days trying to get those comments to work and that's the last thing I need.
If you need any more info please just tell me.
Edit:
If i change estimatedRowHeight to a large number for example 500 I get loads of empty space under cells:
So it looks like TableView can't fix the cell height to content. Maybe this will help someone.
Maybe it's about the textfield inside the CellView. Did you set it's Layout to wraps?
Also I would try to set it's intrinsic size value to 'placeholder' inside the Size Inspector.
The problem is that I'm getting too much of it in relation to line length and text-wrapping.
Here is the code I added to the view controller's viewDidLoad():
tableView.estimatedRowHeight = 80
tableView.rowHeight = UITableViewAutomaticDimension
Here's the code I added to the Message Cell's configureCell() function to try to solve the problem:
self.sizeToFit()
self.layoutIfNeeded()
And finally, here's a screen-shot of the constraints I've applied.
Message Label constraints
Despite all of these attempts to solve the problem, my cells keep ballooning geometrically as I add more text. Any suggestions on a fix?
Edit: Here's what the problem looks like:
Ballooning Cells Cemos
I have a custom tableview cell that contains 2 image views and several buttons.
Most of the items in the cell have a consistent size (height & width), but my main button displays text of a variable height. (The table displays user posts, and this button displays the the first 200 characters. But, the post may be <200 characters.)
The button has word wrap, so it may be 1 to several lines.
I've created the nib as 400, which is based on the required heights of the other items and estimated maximum for 200 characters for the post text button. I then return 400 as the heightForRowAt
But, while I'm happy that 4 lines of the full 200 characters looks fine, for posts with only 1 or 2 lines, there is far too much empty space. I would like to set the height of the cell dynamically based on the size of the button.
I added:
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 400
to the viewDidLoad, and
tableView.rowHeight = UITableViewAutomaticDimension
return tableView.rowHeight
to the heightForRowAt but that just turned the tableview into a complete mess!
How do I achieve this?