How to make UITableViewCell resizable with EasyPeasy? - ios

I know there was a lot of questions about it. But, I have problem with this anyway. I have UIImageView that needs to have specific size and UILabel that needs to have dynamic height. I want to show label on the right side of image. I try not to use storyboard, so here is how I gave constraints using EasyPeasy library:
myImage.easy.layout([Top(8), Left(20), Width(95), Height(95), Bottom()])
myLabel.easy.layout(Top().to(myPicture, .top), Left().to(myPicture, .right), Right(), Bottom())
Below is the result:
As you see, when the text is big, it doesn't fit in cell. I set numberOfLines as zero for label. As I've understood, my image defines the size of the cell and the label can only cover the area that it actually can have. So, what can be the problem? How to solve it? How to make label's height resizable, despite image height? Is order of constraints important?

I don't know exactly how EasyPeasy works but I think you should use something like this:
myImage.easy.layout([Top(8), Left(20), Width(95), Height(95), Bottom(>=0)])
myLabel.easy.layout([Top(20), Left().to(myPicture, .right), Right(), Bottom(20), Height(>=90)])

Related

Fitting text in UILabel inside a Table View Cell

I am using a TableViewController with "Right Detail" style cells. To fit the text into the cell.textLabel I use cell.textLabel?.sizeToFit(). But the problem is, that the cell.detailTextLabel gets pushed out of the view. Whats the best way to solve this problem?
This is the result I am getting right now
I haven't used .sizeToFit(), so i can't say what it is actually for but you don't need it. Lower the compression resistance of your label.
Content Compression Resistance explained:
Sets the priority with which a view resists being made smaller than its
intrinsic size. Setting a higher value means that we don’t want the view
to shrink smaller than the intrinsic content size.
Read more about it here: https://medium.com/#abhimuralidharan/ios-content-hugging-and-content-compression-resistance-priorities-476fb5828ef
I would suggest make your own custom cell.
1.you can crop left label if it is too wide.
2.expand left label to multiple lines and make cell height self constraint.

Issues setting constraints on UICollectionViewCell

I created an UICollectionViewCell (.xib) and I just wanted to add a label and some simple constraints there, as such:
However, doing this messes up everything. No matter the size I chose on UICollectionViewDelegateFlowLayout, the cell will always use the size of the label! What am I doing wrong here? Am I supposed to not use constraints on CollectionViewItems and instead translatesMaskIntoConstraints = true?
Thanks!
edit: Here's what I expected:
And here's what I actually have (as soon as I add the constraints):
edit2: Fixed! By changing the label's content hugging priority to 10 (for example, or lower). But...why exactly if there are no other constraints in the view? If somebody can answer it, I would be very grateful, cause I could just answer this question and give this solution but I can't explain why exactly it fixed it, so it's not a proper answer anyway.
You have 2 ways: you can delegate to a cell to calculate own height or you can set fixed size for height.
If you want the first case, please, read other related answers
If you want fixed size, you should make a fixed height on the label. Label is calculating own size based on contentSize, so your constraints are failed in the matter of fixed size of your cell

Strange behavior of custom UITableViewCell label

I don't know why, but my label behaves strangely. You can check screenshots bellow. Image should fill stackView.
Screenshots
Since you really ought to share a bit more, I can only try to answer based on what's available.
The problem is most likely due to the fact that the StackView that surrounds the label and image does not have a width or height constraint of any sort and/or the imageView has a very low content compression resistance property.
Because StackViews calculate their size based on their child views, it will try to find the best fit. If the label has a normal compression resistance but the imageView has its resistance set to low, the stackView will simply calculate its frame based on the label alone.
So to fix it check the compression resistance of those objects or define contraints for the surrounding stackView to match its parents width.
Firstly.. What is it that you are trying to achieve? Image and Text one beside the other, or one below the other? You might want to check for the stackview direction property
Secondly if you want that your image size remains constant, you will have to give your image view some fixed width constraint, and stackview will respect the size of the imageview. Or if you want that the image takes the entire width of the screen, you can basically pin the stackview to the edges of the screen, after you have put in the imageview inside of the stackview.
Hope that makes sense.
Comment back if you need more help :)

Autoshrink in UILabel only for width?

I have some ASCII syntax diagrams which must not have line breaks in the middle.
These don't have to be editable so I thought the best way is to use an UILabel with auto shrink option. But this option shrinks the text also if the content doesn't fit the height of the labels frame rectangle.
I just want to shrink only if the content doesn't fit the width. It would be absolutely fine to scroll vertically through the text.
What is the best way to do this with UILabel or any other UI element?
Use UITextView with 'editable' property set to false.
So let me rephrase your question. I guess what you want is a UILabel which can show multiple lines, but the longest line need to fit into the width of UILabel. If this is what you want, well the imagination is weird to me...
But anyway, I feel there's a conflict in your settings. First, allowing multiple lines implies you set "Lines" attribute (number of lines) as 0, which allows unlimited lines. But then Autoshrink will play no effect. I'm afraid it is not possible to be done by just setting the storyboard and instead, you need to write some code.
I guess people have raised related questions earlier, by which they want to dynamically change the font size when the text become too long. I guess you want to take a look about this:
Autoshrink on a UILabel with multiple lines
The last issue is you also want the scrolling effect (this is why I feel the outlooking will be weird.) But in short, to achieve this you need 1) dynamically change the UILabel height, most likely using the same technique as explained in the reference thread, and 2) wrap the UILabel in a scroll view. Maybe this can achieve what you want.

Is there a way to fill a collection cell with two labels (fixed-scale-width) in xib?

I'm a newer to iOS, and start to use auto layout in xib.
These days I ran into an issue: Is there a way to fill a collection cell with two labels (fixed-scale-width, say, the scale is 3:1) in xib? If xib cannot do this, then how to do to achieve this?
And, I haven't turn to Swift yet, so the solution must not only for Swift.
Hope your answer, many thanks!
PS: Aspect Ratio can be the solution? But is it just a ratio for one label's width and height? as follows:
But, what I wanted is just let the ratio of widths of item1 and item2 be 3:1, no matter with the height!
Another problem is: if the cell height changed, but the width not, the ratio can keep to be 3:1?
Yes.
Create a xib
Add a collection view cell
Add two labels to it
Control-drag from the label to itself. Set the aspect ratio to 3:1
Do the same for the other label
Add the other necessary constraints to each label.
I had no problems doing this? Did you come across a specific issue?

Resources