IOS dynamically gets the width and height of the UILabel element

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.