ios - AutoLayout link two UILabels to have the same font size -


i have 2 uilabels next each other in row left , right adjustments looks below.

 |-some text left adjusted----------some other text right adjusted-| 

both labels have adjustsfontsizetofitwidth = yes , linked each other following constraint

[nslayoutconstraint constraintwithitem:_rightlabel                     attribute:nslayoutattributeleft                     relatedby:nslayoutrelationgreaterthanorequal                     toitem:_leftlabel                     attribute:nslayoutattributeright                     multiplier:1                     constant:10] 

so take space can , if there not enough space original font size lowered adjustsfontsizetofitwidth no text truncated.

my problem when 1 needs lower font size due long text want other label lower font size both same size instead of 1 being perhaps twice size of other. constraint font size match alas not know how this, ideas?

from uilabel documentation on adjustsfontsizetowidth:

normally, label text drawn font specify in font property. if property set yes, however, , text in text property exceeds label’s bounding rectangle, receiver starts reducing font size until string fits or minimum font size reached.

i infer updated font calculated @ drawing time, , font property read, not written to. therefore, believe suggestion andrew use kvo on font property not work.

consequently, achieve result want, you'll need calculate adjusted font size.

as jackson notes in comments, this convenient nsstring method actual font has been deprecated in ios 7. technically, still use until it's removed.

another alternative loop through font scales until find 1 fit both labels. able working fine; here's sample project shows how did it.

also, here's code in case link ever stops working:

- (void)viewdidload {     [super viewdidload];      nslayoutconstraint *constraint = [nslayoutconstraint constraintwithitem:_rightlabel                                                                   attribute:nslayoutattributeleft                                                                   relatedby:nslayoutrelationgreaterthanorequal                                                                      toitem:_leftlabel                                                                   attribute:nslayoutattributeright                                                                  multiplier:1                                                                    constant:10];      [self.view addconstraint:constraint]; }  - (ibaction)makerightlabellongerpressed:(id)sender {     self.rightlabel.text = @"some longer right label text"; }  - (ibaction)adjustlabelsizes:(id)sender {     nslog(@"attempting adjust label sizes…");      cgfloat minimumscalefactor = fmaxf(self.rightlabel.minimumscalefactor, self.leftlabel.minimumscalefactor);;     uifont * startingfont = self.rightlabel.font;      (double currentscalefactor = 1.0; currentscalefactor > minimumscalefactor; currentscalefactor -= 0.05) {         uifont *font = [startingfont fontwithsize:startingfont.pointsize * currentscalefactor];         nslog(@"  attempting font scale %f (size = %f)…", currentscalefactor, font.pointsize);          bool leftlabelworks = [self wouldthisfont:font workforthislabel:self.leftlabel];         bool rightlabelworks = [self wouldthisfont:font workforthislabel:self.rightlabel];         if (leftlabelworks && rightlabelworks) {             nslog(@"    fits!");             self.leftlabel.font = font;             self.rightlabel.font = font;             return;         } else {             nslog(@"    didn't fit. :-(");         }      }      nslog(@"  won't fit without violating minimum scale (%f), set both minimum.  text may truncated.", minimumscalefactor);      uifont *minimumfont = [self.rightlabel.font fontwithsize:self.rightlabel.font.pointsize * self.rightlabel.minimumscalefactor];     self.rightlabel.font = minimumfont;     self.leftlabel.font = minimumfont; }  - (bool) wouldthisfont:(uifont *)testfont workforthislabel:(uilabel *)testlabel {     nsdictionary *attributes = [nsdictionary dictionarywithobjectsandkeys:testfont, nsfontattributename, nil];     nsattributedstring *as = [[nsattributedstring alloc] initwithstring:testlabel.text attributes:attributes];     cgrect bounds = [as boundingrectwithsize:cgsizemake(cgrectgetwidth(testlabel.frame), cgfloat_max) options:(nsstringdrawinguseslinefragmentorigin) context:nil];     bool itworks = [self doesthissize:bounds.size fitinthissize:testlabel.bounds.size];     return itworks; }  - (bool)doesthissize:(cgsize)aa fitinthissize:(cgsize)bb {     if ( aa.width > bb.width ) return no;     if ( aa.height > bb.height ) return no;     return yes; } 

this approach trivially refactored category method replaces deprecated method linked jackson.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -