IOS learning: uilabel and sizewithfont Methods

Source: Internet
Author: User
1-(void) viewdidload 2 {3 [Super viewdidload]; 4 // do any additional setup after loading the view, typically from a nib. 5 6 nsstring * text = @ "TW Ia nwifei xantianwif eixan tianwi feixan"; 7 8 // set the font, including the font and size 9 uifont * font = [uifont fontwithname: @ "Arial" Size: 50366f]; 10 11 // The maximum height and width of the label can be set to 12 cgsize maxsize = cgsizemake (320.f, maxfloat ); 13 14 // the height and width required when the string is displayed in the specified area according to the specified font (useful when the string has only one line in width) 15 // General Usage: if the width of the specified area and the height is maxfloat, the return value contains the corresponding height 16 // If the width of the specified area is specified, and the height of the area to be displayed by the string exceeds the height of the specified area, returns the height 017 // core: Display multiple rows, specify the width, and obtain the height 18 cgsize labelsize = [text sizewithfont: font19 constrainedtosize: cgsizemake (320.f, maxfloat) 20 linebreakmode: bytes]; 21 22 // specify the area for display 23 cgrect rect = cgrectmake (0.0f, 0.0f, labelsize. width, labelsize. height); 24 25 // cgrectzero indicates (0, 0, 0), that is, leave it to the end and set 26 uilabel * mylabel = [[uilabel alloc] initwithframe: cgrectzero]; 27 28 // The label displays only one row by default. Setting numberofline to 0 indicates no limit on the number of rows. According to the actual display, 29 [mylabel setnumberoflines: 0]; 30 31 mylabel. frame = rect; 32 mylabel. TEXT = text; 33 mylabel. font = font; 34 mylabel. textcolor = [uicolor graycolor]; 35 36 // Add the label to the view to display 37 [self. view addsubview: mylabel]; 38 39 // The reference count is reduced by one, because in the previous addsubview line, view has added 40 [mylabel release] to the reference count of mylabel. 41 42} 43 44-(void) test45 {46 nsstring * text = @ "asfawlfaw; lojf"; 47 uifont * font = [uifont fontwithname: @ "Arial" Size: 20f]; 48 49 // the height and width required when the string is displayed in the specified area according to the specified font (the width is useful when the string has only one line) 50 // common usage: if the width of the specified area and the height is maxfloat, the return value includes the corresponding height 51 // If the width of the specified area is specified, and the height of the area to be displayed by the string exceeds the height of the specified area, then the height returns 052 // core: Display multiple rows, specify the width, and obtain the height 53 cgsize labelsize = [text sizewithfont: font54 constrainedtosize: cgsizemake (320.f, maxfloat) 55 linebreakmode: bytes]; 56 57 // when the string is displayed in a single row in a specified font, the required height and width are 58 cgsize size1 = [text sizewithfont: font]; 59 60 // the height and width required when the string is displayed in a single row in the specified area in the specified font; 61 // The general usage is that the height of the specified area is fixed and the width is maxfloat, the returned value contains the corresponding width 62 // If the width of the specified area is insufficient, the return value is 0; if the height is insufficient, the return value is 63 // core: Single Row display, the width of the specified area must be large enough to obtain the width 64 cgsize size2 = [text sizewithfont: font65 constrainedtosize: cgsizemake (maxfloat, 100366f)]; 66 67 68 // when the string is displayed in the specified font width or line feed mode, the required height and width 69 // although the line feed mode is specified, the line feed is also used in actual calculations, however, the returned result only returns the height and width of the first row 70 // if the specified width is smaller than the string width, the width returns 071 // core: 72 cgsize size3 = [text sizewithfont: font73 forwidth: 40020.f74 linebreakmode: nslinebreakbywordwrapping]; 75 76 cgfloat F = 0.0f; 77 78 // although the line feed mode is specified, the line feed is also displayed in actual calculations, but the returned result is only the height of the first line very wide 79 // specifies the font to be displayed, the smallest font, and the actual font, in actual calculation, if the width is not enough, try to reduce the font of the string until all lines can be displayed. If the character string cannot be fully displayed at the minimum, perform truncation, returns the height and width of the truncated string 80 // The actual font size, which is stored in actualfontsize. 81 cgsize size4 = [text sizewithfont: font82 minfontsize: 10.0f83 actualfontsize: & f84 forwidth: 10010000f85 linebreakmode: nslinebreakbywordwrapping]; 86 87}
Related Article

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.