In iOS, a function that calculates the height of text, based on the given content, font, width, and iOS7, uses boundingrectwithsize after using SIZEWITHFONT,IOS7. </span>
-BoundingRectWithSize:options:attributes:context:Calculates and returns the bounding rect for the receiver drawn using th e given options and display characteristics, within the specified rectangle in the current graphics context.
Discussionto correctly draw and size multi-line text, pass Nsstringdrawinguseslinefragmentorigin in the options parameter. This method returns fractional sizes (in the size component of the returned CGRect); To use a returned size to size of views, you must raise their value to the nearest higher integer using the Ceil function. This method returns the actual bounds of the glyphs in the string. Some of the glyphs (spaces, for example) is allowed to overlap the layout constraints specified by the size passed in, so In some cases the width value of the size component of the returned CGRect can exceed the width value of the size Paramet Er.
According to discussion, the size of the return value CGRect contains a decimal point, and if the view size is defined using the size of the function return value CGRect, you must use the "ceil" function to get the length width (ceil: The smallest positive number greater than the current value).
Post a paragraph of your own writing, using the code:
Incoming text content, font (use screen width here)
Return to the rectangular area, note that the cgsize length that is returned must be handled using Ceil
+ (Cgsize) Sizefornoticetitle: (nsstring*) Text font: (uifont*) font{cgrect screen = [UIScreen mainscreen].bounds; CGFloat maxWidth = screen.size.width; Cgsize maxSize = Cgsizemake (MaxWidth, Cgfloat_max); Cgsize textSize = Cgsizezero; IOS7 later uses boundingrectwithsize, before using Sizewithfont if ([Text respondstoselector: @selector (boundingrectwithsize:o Ptions:attributes:context:)] {//Multiple lines must use Nsstringdrawinguseslinefragmentorigin, Some people on the internet say it's not using nsstringdrawingusesfontleading to calculate the result nsstringdrawingoptions opts = Nsstringdrawinguseslinefragmentorigin | nsstringdrawingusesfontleading; Nsmutableparagraphstyle *style = [[Nsmutableparagraphstyle alloc] init]; [Style setlinebreakmode:nslinebreakbycharwrapping]; Nsdictionary *attributes = @{nsfontattributename:font, nsparagraphstyleattributename:style}; CGRect rect = [text Boundingrectwithsize:maxsize options:opts Attributes:attributes Context:nil]; TextSize = rect.size; } else{textSize = [text Sizewithfont:font constrainedtosize:maxsize linebreakmode:nslinebreakbycharwrapping]; } return textSize;}
This is not the pit we are talking about today.
The Pit is here! The pit is here! The pit is here! (re-say three)
Boundingrectwithsize: The method simply gets the size of the string, and if the string contains a character such as \n\r, it will only be evaluated as a character. But put in the Uitextview to parse, will turn it into a carriage return line character, then the display will be more than a row of height out.
Also, use stringWithFormat to ignore \ n, using the @ "" form.
Dwarfish point of practice, gross actual height = Boundingrectwithsize calculated height + \n\r Occurrences * The height of a single line of text
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Use Boundingrectwithsize to calculate content height pits