ios - getting adjustsFontSizeToFitWidth to work with constraints for snug content -


this little view control i've written test out issue i'm having constraints , adjustsfontsizetofitwidth. little thing add's , remove's words label see how various content sizes in label_title effect layout. desired layout snug around label_title (with label_count taking rest of room). unfortunately not quite snug yet.

here 3 view states i've screen captured illustrate lack of snugness on blue title i'm trying fix.

following full source of view controller run view.

one word - pretty snug

two words - not snug @ all

three words - pretty snug

#import "mainviewcontroller.h"  @interface mainviewcontroller ()  // stuff test our problem @property(nonatomic,strong) uibutton * addbutton; @property(nonatomic,strong) uibutton * removebutton;  // our problem @property(nonatomic,strong) uiview * view_labels; @property(nonatomic,strong) uilabel * label_title; @property(nonatomic,strong) uilabel * label_count;  @end  @implementation mainviewcontroller  // synthesize views constraint mapping @synthesize label_title; @synthesize label_count;  - (void)viewdidload {     [super viewdidload];      [self.view setbackgroundcolor:[uicolor graycolor]];      self.addbutton = [self buttonwithtitle:@"add word"                                   selector:@selector(addbuttonpressed) color:[uicolor greencolor]];     self.removebutton = [self buttonwithtitle:@"remove word"                                      selector:@selector(removebuttonpressed) color:[uicolor redcolor]];      // view containing our troublesome labels     self.view_labels = [[uiview alloc] init];     [self.view addsubview:self.view_labels];     //[self.view_labels settranslatesautoresizingmaskintoconstraints:no];     [self.view_labels setbackgroundcolor:[uicolor lightgraycolor]];      // label row 1     self.label_title = [self labelwithtext:@"word" defaultfontsize:30.0 bgcolor:[uicolor bluecolor]];     [self.view_labels addsubview:self.label_title];      // label row 2     self.label_count = [self labelwithtext:@"77" defaultfontsize:40.0 bgcolor:[uicolor purplecolor]];     [self.view_labels addsubview:self.label_count];       nsdictionary *views = nsdictionaryofvariablebindings(label_title, label_count);     nsdictionary *metrics = @{@"pad":@2};      [self.view addconstraints:[nslayoutconstraint                                constraintswithvisualformat:@"v:|-(==pad)-[label_title(<=44)]-(==pad)-[label_count]-(==pad)-|"                                options:0                                metrics:metrics                                views:views]];     [self.view addconstraints:[nslayoutconstraint                                constraintswithvisualformat:@"|-(==pad)-[label_title]-(==pad)-|"                                options:0                                metrics:metrics                                views:views]];     [self.view addconstraints:[nslayoutconstraint                                constraintswithvisualformat:@"|-(==pad)-[label_count]-(==pad)-|"                                options:0                                metrics:metrics                                views:views]]; }  #pragma mark - boiler plate stuff  -(uilabel*) labelwithtext:(nsstring*) text defaultfontsize:(float) fontsize bgcolor:(uicolor*) color {     uilabel * label = [[uilabel alloc] init];     [label settranslatesautoresizingmaskintoconstraints:no];     [label settextalignment:nstextalignmentcenter];     [label settextcolor:[uicolor whitecolor]];     [label setadjustsfontsizetofitwidth:yes];     [label setnumberoflines:0];     [label setminimumscalefactor:0.6];     [label setfont:[uifont boldsystemfontofsize:fontsize]];     [label setbackgroundcolor:color];     [label settext:text];     return label; }  -(uibutton*) buttonwithtitle:(nsstring*) title selector:(sel) selector color:(uicolor*) color {     uibutton * button = [uibutton buttonwithtype:uibuttontypecustom];     [self.view addsubview:button];     [button settitle:title forstate:uicontrolstatenormal];     [button setbackgroundcolor:color];     [button addtarget:self action:selector forcontrolevents:uicontroleventtouchupinside];     return button; }  -(void) viewwilllayoutsubviews{     [super viewwilllayoutsubviews];      float square_size = 100;      [self.addbutton setframe:cgrectmake(0, self.view.frame.size.height-44.0, self.view.frame.size.width, 44.0)];     [self.removebutton setframe:cgrectmake(0, self.view.frame.size.height-(44.0*2), self.view.frame.size.width, 44.0)];      [self.view_labels setframe:cgrectmake(square_size, square_size, square_size, square_size)]; }  -(void) addbuttonpressed {     nsstring * text = self.label_title.text;     if (text == nil){         text = @"";     }     text = [nsstring stringwithformat:@"%@%@",text,@" word"];     text = [text stringbytrimmingcharactersinset:[nscharacterset whitespaceandnewlinecharacterset]];     [self.label_title settext:text]; }  -(void) removebuttonpressed {     nsstring * text = self.label_title.text;     if (text == nil || [text isequaltostring:@""]){         return;     }     nsarray * array = [text componentsseparatedbystring:@" "];     text = @"";     (int = 0; < [array count]-1; i++){         nsstring * token = [array objectatindex:i];         text = [nsstring stringwithformat:@"%@ %@",text,token];     }     text = [text stringbytrimmingcharactersinset:[nscharacterset whitespaceandnewlinecharacterset]];     [self.label_title settext:text]; }  @end 

if trying fit text in uilabel replace method following.

-(uilabel*)labelwithtext:(nsstring*) text defaultfontsize:(float) fontsize bgcolor:(uicolor*) color { @autoreleasepool {    uilabel * label = [[uilabel alloc] initwithframe:cgrectmake(50, 50,100,100)];    [label settextalignment:nstextalignmentcenter];    [label settextcolor:[uicolor whitecolor]];    [label setadjustsfontsizetofitwidth:yes];   [label setfont:[uifont boldsystemfontofsize:fontsize]];   [label setbackgroundcolor:color];   [label settext:text];   [label setnumberoflines:0];      while (fontsize > 0.0)     {       cgsize size = [text sizewithfont:label.font constrainedtosize:cgsizemake(label.frame.size.width, label.frame.size.height) linebreakmode:nslinebreakbywordwrapping];          if (size.height <= label.frame.size.height)         break;        fontsize -= 1.0;      }   [label setfont:[uifont boldsystemfontofsize:fontsize]];   return label;  } } 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -