ios - the height of sizeToFit not correct in UILabel -
i create uilabel following code:
uilabel *textlabel = [[uilabel alloc] initwithframe:cgrectzero]; textlabel.font = [uifont systemfontofsize:30]; textlabel.backgroundcolor = [uicolor redcolor]; textlabel.text = @"0"; [textlabel sizetofit]; textlabel.center = cgpointmake(100, 100); [self.view addsubview:textlabel];
and textlabel.bounds.height seems higher text font actual height. following:
but because designer marks space of 2 uilabel based on real text size, can't assign value 2 uilabel use designer's mark space, need run app in simulator check it's correct.
i wan know how make uilabel sizetofit real height of text, no padding between text.
there room around 0 in example because label has take account entire set of metrics font, cocoanetics has nice illustration of in blog post "understanding uifont". example, running code on machine , putting "g" next "0", can see "g" touches bottom of label:
if implementing label can contain arbitrary text, designer should account , not ask "real text size" of placeholder text—which may end being inaccurate can see here when placeholder "0". ideally provide baseline , font, that's need have accurate results.
if absolutely must measure string precisely, nsstring
has -sizewith…
method(s), nsattributedstring
has -boundingrectwithsize:…
, -size
, , core text has ctframesettersuggestframesizewithconstraints()
. of these have own quirks , may not produce results you're expecting, though.
Comments
Post a Comment