i want create uilabel programmatically height, width , want add constraints programmatically positioning uilabel.
update:
i want create ui this:-

how create ui programatically
code create 1 label label1 created 2 more label label2 , label3
uilabel *label1 = [[uilabel alloc]init]; label1.font = titlefont; label1.numberoflines=0; label1.text= @"descriptions"; label1.linebreakmode=nslinebreakbywordwrapping; [label1 sizetofit]; label1.backgroundcolor=[uicolor bluecolor]; label1.textcolor=[uicolor blackcolor]; label1.translatesautoresizingmaskintoconstraints = no; [self.view addsubview:label1]; and able add horizontal constraints them code
[self.view addconstraints:[nslayoutconstraint constraintswithvisualformat:@"h:|-[label1]-|" options:nslayoutformatdirectionleadingtotrailing metrics:nil views:nsdictionaryofvariablebindings(label1)]]; i able set vertical constraint view unable set constraint 1 label another.
to create label height , width constraints here constraints...and don't forget add label in view addsubview method
uilabel *label = [[uilabel alloc] init]; [label settranslatesautoresizingmaskintoconstraints:no]; [self.view addsubview:label]; // width constraint [label addconstraint:[nslayoutconstraint constraintwithitem:label attribute:nslayoutattributewidth relatedby:nslayoutrelationequal toitem:nil attribute: nslayoutattributenotanattribute multiplier:1 constant:200]]; // height constraint [label addconstraint:[nslayoutconstraint constraintwithitem:label attribute:nslayoutattributeheight relatedby:nslayoutrelationequal toitem:nil attribute: nslayoutattributenotanattribute multiplier:1 constant:21]]; and in swift
label.settranslatesautoresizingmaskintoconstraints(false) self.view.addsubview(label) label.addconstraint(nslayoutconstraint(item: label, attribute: .height, relatedby: .equal, toitem: nil, attribute: .notanattribute, multiplier: 1, constant: 21)) label.addconstraint(nslayoutconstraint(item: label, attribute: .width, relatedby: .equal, toitem: nil, attribute: .notanattribute, multiplier: 1, constant: 200)) check link more detail
update
update question, here updated answer...
uilabel *label1 = [[uilabel alloc] init]; [label1 settranslatesautoresizingmaskintoconstraints:no]; uilabel *label2 = [[uilabel alloc] init]; [label2 settranslatesautoresizingmaskintoconstraints:no]; label1.text = @"label1"; label1.backgroundcolor = [uicolor bluecolor]; label2.text = @"label2"; label2.backgroundcolor = [uicolor redcolor]; [self.view addsubview:label1]; [self.view addsubview:label2]; // width constraint [label1 addconstraint:[nslayoutconstraint constraintwithitem:label1 attribute:nslayoutattributewidth relatedby:nslayoutrelationequal toitem:nil attribute: nslayoutattributenotanattribute multiplier:1 constant:280]]; // height constraint [label1 addconstraint:[nslayoutconstraint constraintwithitem:label1 attribute:nslayoutattributeheight relatedby:nslayoutrelationequal toitem:nil attribute: nslayoutattributenotanattribute multiplier:1 constant:21]]; // centerx constraint [self.view addconstraint:[nslayoutconstraint constraintwithitem:self.view attribute:nslayoutattributecenterx relatedby:nslayoutrelationequal toitem:label1 attribute: nslayoutattributecenterx multiplier:1 constant:0]]; // top constraint [self.view addconstraint:[nslayoutconstraint constraintwithitem:label1 attribute:nslayoutattributetop relatedby:nslayoutrelationequal toitem:self.toplayoutguide attribute: nslayoutattributebottom multiplier:1 constant:40]]; // label2 [self.view addconstraint:[nslayoutconstraint constraintwithitem:label1 attribute:nslayoutattributeleading relatedby:nslayoutrelationequal toitem:label2 attribute: nslayoutattributeleading multiplier:1 constant:0]]; // label2.height = label1.height [self.view addconstraint:[nslayoutconstraint constraintwithitem:label1 attribute:nslayoutattributeheight relatedby:nslayoutrelationequal toitem:label2 attribute: nslayoutattributeheight multiplier:1 constant:0]]; // label2.width = label1.width [self.view addconstraint:[nslayoutconstraint constraintwithitem:label1 attribute:nslayoutattributewidth relatedby:nslayoutrelationequal toitem:label2 attribute: nslayoutattributewidth multiplier:1 constant:0]]; // label2.top [self.view addconstraint:[nslayoutconstraint constraintwithitem:label2 attribute:nslayoutattributetop relatedby:nslayoutrelationequal toitem:label1 attribute: nslayoutattributebottom multiplier:1 constant:34]]; result screen

Comments
Post a Comment