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: enter image description here

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:

"0g" metrics

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

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -