Search code examples
iosios6uilabeluicollectionviewautolayout

AutoLayout - Resizing UILabel to its Superview and sizing it


I have a UICollectionView which contains some cells.

Each cell holds one UILabel within it. Within the label is one letter, it acts as a tile (as such). When more cells are added to the UICollectionView the UICollectionViewCells change size with the following:

-(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);
    }

}

What I am trying to do now is re-size the UILabel within the cell each time the layout is changed. How can I fit the labels size to the cells size using AutoLayout? Also how can I update the font size based on the UILabel size?


Solution

  • Re-creating this in IB worked. I was able to set the constraints for the horizontal/vertical to 0/required for the sides of the label in the cell itself and this worked.