iphone - UILabel's sizeToFit/sizeThatFits ignore the numberoflines property -
problem: determine size (number of lines) uilabel needs, assuming width 300 px. string longer, set linebreakmode uilinebreakmodewordwrap , invoked sizethatfits try determine size. gives width of 457 px in single line, rather expected 300px in 2 lines.
please see:
cgsize available = cgsizemake(300, infinity); uilabel *label = [[[uilabel alloc] initwithframe:cgrectmake(0, 0, 300, 400)] autorelease]; label.text = title; label.linebreakmode = uilinebreakmodewordwrap; label.font = [uifont fontwithname:kboldfont size:ktitlefontsize]; label.numberoflines = 3; cgsize sizedtofit = [label sizethatfits:available];
but find sizedtofit variable has width of 457 pixels , height of 22 px, , ui displays single line clipped text. expect width of 300 pixels, , height of 44 px 2 lines.
the uilabel doc numberoflines says:
when receiver resized using sizetofit method, resizing takes account value stored in property. example, if property set 3, sizetofit method resizes receiver big enough display 3 lines of text.
i tried various combinations of:
- passing cgrectzero init function, passing 300x400 or 300 x infinity.
- setting frame after creation rather passing init function.
- invoking [sizetofit] , hoping calculates height assuming present width, doesn't.
- calling sizetofit , calling sizethatfits.
- invoking layoutifneeded.
none of them works. doing wrong, or bad bug documentation , framework implementation don't agree? thanks.
i had same problem, size fits ignores size... /: ended using:
cgrect textsize = [uilabel textrectforbounds:cgrectmake(0, 0, 300, cgfloat_max) limitedtonumberoflines:3];
works charm... :)
the documentation says shouldn't call directly, i've been using while, approved submitted apps, , awesome... :)
Comments
Post a Comment