AutoLayout - resize UILabel before viewing it and determine the size

I have a UICollectionView that contains some cells.

Each cell contains one UILabel inside it. There is one letter inside the label, it acts like a tile (as such). When more cells are added to the size of the UICollectionView , the size of the UICollectionViewCells changes as follows:

 -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { // NSLog(@"%s",__PRETTY_FUNCTION__); NSLog(@"the word array count is: %i",self.wordArray.count); if (self.wordArray.count <= 5) { return CGSizeMake(50,50); } else if (self.wordArray.count <= 6 ) { return CGSizeMake(30, 30); } else if (self.wordArray.count <= 8 ) { return CGSizeMake(10, 10); } else { return CGSizeMake(100,100); } } 

Now I'm trying to resize the UILabel inside the cell every time I change the layout. How can I match label size to cell size using AutoLayout ? Also how can I update font size based on UILabel size?

+4
source share
2 answers

A re-creation of this in IB was created. I was able to set limits for the horizontal / vertical value 0 / needed for the label sides in the cell itself, and it worked.

0
source

You can set something like the following layout restrictions:

 NSDictionary *viewDictionary = NSDictionaryOfVariableBindings(label); NSDictionary *insetMetrics = @{ @"left" : @(10.0f), @"right" : @(10.0f), @"top" : @(10.0f), @"bottom" : @(10.0f) }; [cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(left)-[label]-(right)-|" options:kNilOptions metrics:insetMetrics views:viewDictionary]]; [cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(top)-[label]-(bottom)-|" options:kNilOptions metrics:insetMetrics views:viewDictionary]]; 

Text in UILabel may automatically decrease if you set the correct properties (see UILabel Class Reference ). Therefore, if you initially set the font size to the size that you want to use for the largest cell, as the label is compressed, the text will be compressed with it.

0
source

Source: https://habr.com/ru/post/1497273/


All Articles