ios - boundingRectWithSize adds extra bottom padding to UILabel -


uilabel created this:

label = [[uilabel alloc] initwithframe:cgrectmake(32, 10, 268, 44)]; label.textalignment =  nstextalignmentleft; label.backgroundcolor = [uicolor yellowcolor]; label.numberoflines = 2; label.linebreakmode = nslinebreakbytruncatingtail; 

then update label this:

label.text = sometext; cgrect frame = label.frame; nsdictionary *attributes = @{nsfontattributename: label.font}; cgrect rect = [sometext boundingrectwithsize:cgsizemake(label.frame.size.width, maxfloat)                                           options:nsstringdrawinguseslinefragmentorigin                                        attributes:attributes                                           context:nil]; frame.size.height = rect.size.height; label.frame = frame; 

this works fine label has 1 line of text, have maximum of 2 lines. when 2nd line gets truncated, bottom padding gets added label, makes impossible position label below (the red label in images):

enter image description here

enter image description here

why happen? if it's bug, there workaround?

try this:

nsattributedstring *attrstring = [[nsattributedstring alloc] initwithstring:text attributes:attributes]; uilabel *anotherlabel = [[uilabel alloc] initwithframe:cgrectmake(0, 0, mylabel.frame.size.width, maxfloat)]; [anotherlabel setattributedtext:attrstring]; [anotherlabel setnumberoflines:2]; [anotherlabel settext:nstextalignmentleft]; [anotherlabel setlinebreakmode:nslinebreakbytruncatingtail]; [anotherlabel sizetofit]; [mylabel setattributedtext:attrstring]; [mylabel setframe:cgrectmake(frame.origin.x, frame.origin.y, frame.size.width, [anotherlabel frame].size.height)]; 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -