Highly adaptive Uifont *font = [Uifont fontwithname:@ "Arial" size:13];//set a line height upper limit cgsize size = Cgsizemake (320,1000);// Calculates the actual frame size and turns the frame of the label into the actual size cgsize labelsize = [Entity.content sizewithfont:font constrainedtosize:size Linebreakmode:uilinebreakmodewordwrap];D Ebuglog (@ "height =%f", height);
Method One: Cgsize titlesize = [titlecontent boundingrectwithsize:cgsizemake (kScreen_Width, Maxfloat) options:nsstringdrawinguseslinefragmentorigin attributes:@{nsfontattributename:[uifont systemFontOfSize:13]} context:nil].size; or Method two:cgsize labelsize = [str sizewithfont:font constrainedtosize:size linebreakmode:uilinebreakmodewordwrap];// Gets the width and height of a single-line string nsstring *content = @ "get UILabel width and height test"; Cgsize size =[content sizewithattributes:@{nsfontattributename:[uifont systemfontofsize:13 ]}];//Gets the width and height of a multiline string uilabel *titlelabel = [[uilabel alloc]init]; titlelabel.font = [uifont systemfontofsize:1]; nsstring * titlecontent = @ "Get UILabel width and height test get UILabel width and height test get UILabel Width and height test get UILabel width and height test get UILabel width and height test get UILabel width and height tests obtainedTake UILabel width and height test "; titlelabel.text = titlecontent; titlelabel.numberoflines = 0;//multi-line display, calculate height titlelabel.textcolor = [uicolor lightgraycolor]; cgsize titlesize = [ Titlecontent boundingrectwithsize:cgsizemake (kscreen_width, maxfloat) options: NSSTRINGDRAWINGUSESLINEFRAGMENTORIGIN ATTRIBUTES:@{NSFONTATTRIBUTENAME:[UIFONT SYSTEMFONTOFSIZE:13]} context:nil].size; titleLabel.size = titleSize; titlelabel.x = 0; titlelabel.y = 0; [ self.view addsubview:titlelabel]; //or//Initialize labeluilabel *label = [[ uilabel alloc] init];//set the automatic line number and character wrapping [label setnumberoflines:0]; [label.linebreakmode:uilinebreakmodewordwrap]; // Test String NSSTRING *STR&Nbsp;= @ "Get UILabel width and height test get UILabel width and height test get UILabel width and height test get uilabel width and height test get UILabel width and height test get UILabel width and height test get UILabel width and height test "; uifont *font = [uifont fontwithname:@ "Arial" size:13];//set a line height upper limit cgsize size = cgsizemake (320,1000);//Calculate the actual frame size and change the label frame to the actual size cgsize labelsize = [str sizeWithFont:font constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap]; [Label setframe:cgrectmake: (0,0, labelsize.width, labelsize.height)];
IOS dynamically gets the width and height of the UILabel element