Is there any way to adapt line spacing in a single multi-line table cell? Not the spacing between different rows of the table but between the actual lines of text within a single cell?
Related
I have a UICollectionView that uses UICollectionViewCompositionalLayout.
I have multiple sections each section contains a specific number of items (based on the model data) each section has items with estimated width and an absolute height (which is fine)
each cell contains a textview that has initial text so it's sized based on the estimated size for the text that it contains(the initial text)
these textviews are active and i want them to be dynamic (when i start typing i want the cell that's containing the textview that is being edited now to expand horizontally).
I have a UITableViewCell that contains two labels positioned vertically one on top of another, and I am using AutoLayout.
The cell works (and looks) fine when both labels have some text.
Sometimes, though, the top label does not contain any text, and in this case I would the cell to have the same height, but the bottom label to be centered vertically. Is it possible to do it with AutoLayout without modifying the constraints at run time?
I don't believe this can be accomplished without modifying something at run time. I see a few options if you'd like to do this without changing constraints programmatically, some messier than others:
First option, you could set three labels in the cell. Two labels that are stacked vertically and one that covers the whole cell vertically, fully overlapping both other labels. At runtime you can determine if the bottom label shouldn't contain text and then set the overlapping top label with the text you would have previously set on the top vertically stacked label.
Second option, you could utilize a label that has the height of both labels you currently have. Set this new (2x height) label to allow for 2 lines (can be done in InterfaceBuilder side options). Then at runtime interpolate the label.text attribute with both label's text. Put a new line character in between the labels if the second label has text. It would look something like this
In Swift:
my2xLabel.text = "\(firstLabelString) \n \(secondLabelString)"
In Obj-C:
my2xLabel.text = [NSString stringWithFormat:#"%# \n %#", firstLabelString, secondLabelString];
All of this being said, modifying the constraints at runtime may be a less hacky way to achieve this formatting.
Make two prototype cells then. One will be the one you have right now, the other would contain only one label but centred vertically. Check if the text would be empty, return the cell with the vertically centred label. Otherwise, return the other cell.
I have a simple table view with a unique label for the cell prototype. I have been following the recommendations in order to have proper auto-sizing cells:
setting auto-layout constraints for the label regarding its leading, trailing, top and bottom spacing in its content view,
setting the rowHeight property to UITableViewAutomaticDimension,
providing the estimatedRowHeight property (set to 44),
setting the number of lines for the label to 0.
I also have enabled the default Edit Mode which allows the reordering and deletion of rows (exactly like in the Alarm tab of the Apple Clock app).
Now, on to observed problem:
I provide two labels, one with a short text and a second with a longer text, so that the second row has twice the height of the first row.
I enter Edit Mode, and swap the position of the rows. So far, everything keeps its composure and the sizing is still correct, the longer label being in the top row.
When I swap the positon again to return to the original layout (short label on top and long label on bottom), the longer text is not spread over two lines in its row as it should, but turns into a one-line display with an ellipsis at the end. Moreover, the height of the cell remains the same and does not fit the now single-lined text.
And after exitting Edit Mode (compare with the first image):
I suspect that I might be missing some kind of reload operation after the moving or deletion of rows, but I did not find the relevant information. Similar sizing problems appear when deleting rows that have been previously reordered.
How to keep the cell sizing as well as the text formatting consistent after moving/deletion of rows?
Thank you very much.
I'm displaying 2 or three lines of text in a UILabel however the text is getting truncated rather than wrapping.
Within IB Line Breaks is set to Word Wrap and the height of the label is easily plenty to accommodate the lines of text.
I'm doing exactly the same thing as this in other labels in other views and they do wrap, I can't see any reason or anything different why this one won't wrap.
You need to set numberOfLines for the UILabel to either 0 or whatever number of lines you want it to be. Setting it to 0 will automatically choose the number of lines based on the length of the text. The default is 1.
Edit: I just checked and in IB, the property is just called Lines.
I have a UITextView in a prototype UITableView cell. I'd like to dynamically indent the first line of the UITextView to make room for a button that will have a variable width depending on the cell row. The button will vary in width because it's title will contain usernames in an activity feed.
Think of Instagram or any app that has a feed with clickable usernames on the left and text directly following.
I've been able to get close by counting the number of characters in the button title and left padding the text of my UITextView with a series of #" " blank spaces. This is akin to what other SO posts have suggested.
This is inaccurate for me because the width of blank spaces don't precisely match that of the button title because different characters have different widths.
Does anyone know if it is possible to indent a UITextView by a specific float value? I've been able to convert the width of the button title to a float and I believe that this would solve my issue.