ios - AutoLayout - Resizing UILabel to its Superview and sizing it -
i have uicollectionview
contains cells.
each cell holds 1 uilabel
within it. within label 1 letter, acts tile (as such). when more cells added uicollectionview
uicollectionviewcells
change size 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 trying re-size uilabel
within cell each time layout changed. how can fit labels size cells size using autolayout
? how can update font size based on uilabel
size?
you can set following layout constraints:
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 can automatically shrink if set correct properties (see uilabel class reference). if set font size size you'd largest cell, label shrinks, text shrink it.
Comments
Post a Comment