ios - Where is this vertical spacing coming from in UILabelView? -
i'm creating ios view displays various static text elements. xib looks this:
it uses 4 labels title, timestamp, body, , footer. every view anchored sibling view above vertically , anchored left/right of parent view. labels have fixed height except body has >= height , number of lines set 0 "word wrap" line wrapping style. parent view uiscrollview.
on iphone looks fine:
however on ipad looks this:
huh? vertical space in body label coming from? xib , view controller identical between iphone , ipad (there no custom ipad code @ moment). i've found vertical space directly related how many line-wraps label renders. if no lines wrap, no vertical space. if few lines wrap, there's little vertical space. if every line wraps, well, that's looks like.
first of ideas on why uilabel behaving way?
second of all, if can't make stop doing how can work around it?
i've tried few things. if call [bodylabel sizetofit] within -viewdidlayoutsubviews fixes label doesn't fix layout of of sibling views (e.g. footer label stuck way @ bottom of screen instead of pulled under body). attempts entire view re-layout children after calling sizetofit ignored. i've tried sizing uilabel calculating height based on font, results in same behavior -sizetofit (albeit more code).
replacing body uilabel uitextview instead doesn't give me weird vertical spacing issues need calculate height of uitextview manually (using font calculations) , resizing uitextview within parent uiscrollview makes uiscrollview refuses scroll (as if doesn't know contents big bounds).
so @ moment i'm stuck. explanation of why uilabel behaves way on ipad layout helpful.
the main problem method auto resizing text inside label failing because in ipad label doesn't have set width beginning, calculated on run time , that's source of mess. on iphone, label has set width (on ib) there no troubles.
there 2 ways solving problem:
having 2 storyboards : 1 iphone , 1 ipad
doing make label knows width since beginning , works on iphone.
having 1 storyboard both iphone , ipad
you can go around problem calculating size best fits text , result add height constraint code label. calculating desiredsize can calculate width formula:
current view's width - (leading space + trailing space)
. here code
cgsize desiredsize = [_bodylabel sizethatfits:cgsizemake(self.view.frame.size.width-40, 10)]; nsstring *visualcontraint = [nsstring stringwithformat:@"v:[_bodylabel(%.0f)]",desiredsize.height]; [_bodylabel addconstraints:[nslayoutconstraint constraintswithvisualformat:visualcontraint options:nslayoutformatdirectionleadingtotrailing metrics:nil views:nsdictionaryofvariablebindings(_bodylabel)]];
Comments
Post a Comment