ios - Create UILable at runtime and set autolayout programatically related to each other -


i have uiview in uitableviewcell. getting array of names service , want create uilabel in for loop inside uiview in uitableviewcell according text width.

i have done @ end problem want check if label being created having width greater space remaining in superview, should created below first line word wrapping of text in uilabel.

what have done is, have created sample of code

-(void)addadaynamicviw {      [self.vwcontainer settranslatesautoresizingmaskintoconstraints:false];     uilabel *lblprevious;     int totalwidth;     (int i=0; i<8; i++) {         uilabel *lbl=[[uilabel alloc]init];         //[lbl setframe:cgrectmake(0, 0, 30, 50)];         [lbl setnumberoflines:0];         [lbl setbackgroundcolor:[uicolor greencolor]];         [lbl settranslatesautoresizingmaskintoconstraints:false];         [lbl settext:[nsstring stringwithformat:@"label num %d",i]];          [self.vwcontainer addsubview:lbl];          nslayoutconstraint *top;         top=[nslayoutconstraint constraintwithitem:lbl attribute:nslayoutattributetop relatedby:nslayoutrelationgreaterthanorequal toitem:self.vwcontainer attribute:nslayoutattributetop multiplier:1.0 constant:0];          nslayoutconstraint  *left;         if (lblprevious) {             cgsize size = lblprevious.intrinsiccontentsize;             nslog(@"intrinzik content size =%f",size.width);          left =[nslayoutconstraint constraintwithitem:lbl attribute:nslayoutattributeleft relatedby:nslayoutrelationlessthanorequal toitem:lblprevious attribute:nslayoutattributeleft multiplier:1 constant:size.width+2];         }        else        {          left=[nslayoutconstraint constraintwithitem:lbl attribute:nslayoutattributeleft relatedby:nslayoutrelationlessthanorequal toitem:self.vwcontainer attribute:nslayoutattributeleft multiplier:1 constant:0];        }          nslayoutconstraint *width=[nslayoutconstraint constraintwithitem:lbl attribute:nslayoutattributewidth relatedby:nslayoutrelationgreaterthanorequal toitem:nil attribute:nslayoutattributenotanattribute multiplier:1.0 constant:10];           nslayoutconstraint *height=[nslayoutconstraint constraintwithitem:lbl attribute:nslayoutattributeheight relatedby:nslayoutrelationequal toitem:nil attribute:nslayoutattributenotanattribute multiplier:1.0 constant:50];           nslayoutconstraint *bottom=[nslayoutconstraint constraintwithitem:lbl attribute:nslayoutattributebottom relatedby:nslayoutrelationequal toitem:self.vwcontainer attribute:nslayoutattributebottom multiplier:1.0 constant:0];          nslayoutconstraint *right=[nslayoutconstraint constraintwithitem:lbl attribute:nslayoutattributetrailing relatedby:nslayoutrelationlessthanorequal toitem:self.vwcontainer attribute:nslayoutattributetrailing multiplier:1.0 constant:0];            [self.vwcontainer addconstraint:left];         [self.vwcontainer addconstraint:bottom];         [self.vwcontainer addconstraint:width];         [self.vwcontainer addconstraint:top];         [self.vwcontainer addconstraint:height];         [self.vwcontainer addconstraint:right];         lblprevious=lbl;         cgsize size = lblprevious.intrinsiccontentsize;         totalwidth+=size.width;     }       [self.vwcontainer layoutifneeded];     [self.vwcontainer updateconstraintsifneeded];  } 

this line of code creating label programmatically after end of previous label, want if label being created not fit in superview should increase height of superview , should start 0 of superview, below first label.

in image can see labels created programmatically , after previous label, problem in last, showing seventh above 5th , 6th label, want show 5th 6th , 7th label next line

i have added constraints vwcontainer


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -