IOS dynamically calculates line height, width, etc.
Source: Internet
Author: User
<span id="Label3"></p>Uilabel There are two ways to calculate the size of a text: 1. for rich text calculation nsattributedstring<p><p>-(cgrect) boundingrectwithsize: (cgsize) size options: (nsstringdrawingoptions) options Attributes: (nullable Nsdictionary*) attributes context: (nullable nsstringdrawingcontext *) context ns_available (10_11, 7_0);</p></p><span style="font-size: 1em"><span style="font-size: 1em">2. for nsstring and normal text calculation</span></span><p><p><code>- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options context:(nullable NSStringDrawingContext *)context NS_AVAILABLE(10_11, 6_0);</code></p></p><p><p><code> </code></p></p><p><p>There are the same parameters for both methods:</p></p><p><p></p></p>Parameter one: size indicates the maximum width of the computed text (which is the maximum height and width of the limit), in general we set the maximum width, the height is not limited<code><code>CGSizeMake(getScreenWidth(), CGFLOAT_MAX)</code></code>, note: the width of the limit is different and the calculated height results are different. Parameter two: options indicates the type of calculation<p><p><strong>nsstringdrawinguseslinefragmentorigin</strong>: Draw text Using line <em>fragement Origin</em> instead of <em>baseline Origin</em>. This is generally used. The entire text calculates the size of the entire text in a rectangle of each row (the specified origin is the lines fragment origin, not the base line Origin)</p></p><p><p><strong>nsstringdrawingusesfontleading</strong>: Calculates the height based on the font, <span class="comment">using the line spacing of the font to calculate the range of text occupied, that is, the distance from the bottom of each line to the bottom of the next line (</span> <span class="comment"> Uses the Font leading for calculating line heights)</span></p></p><p><p><strong>nsstringdrawingusesdevicemetrics</strong>: Use hieroglyphs to calculate the height, and to calculate the text as an image symbol, rather than as a character. The text range is computed in terms of the space occupied by each font (Uses image glyph bounds instead of typographic Bounds)</p></p><p><p><strong>nsstringdrawingtruncateslastvisibleline</strong>: If <em>nsstringdrawinguseslinefragmentorigin</em> is set, this option is not used, When the text does not fit within the specified bounds, the ellipsis is automatically added to the last Line. If Nsstringdrawinguseslinefragmentorigin is not set, this option does not take effect (truncates and adds the ellipsis character to the last visible line I f the text doesn ' t fit into the bounds Specified. Ignored if Nsstringdrawinguseslinefragmentorigin is not also set. )</p></p>Parameter Three: attributes represents the Rich Text property<em><em>NSAttributedString.h</em></em>such as font, text style<em><em>Nsfontattributename</em></em>、<em><em>Nsparagraphstyleattributename</em></em>Parameter Four: nsstringdrawingcontext<br><em>when <em>stringdrawingcontext=nil, It's equivalent of passing the default instance initialized with [[nsstringdrawingcont Ext alloc] init] Context</em></em><br>context, including some information, such as how to adjust word spacing and scaling. This parameter is generally Nil. Question: Sometimes the size is different from the actual size when calculating the text?<p><p>Workaround 1: Check that the font and limit width are set correctly ( <em>landlord is here/(ㄒoㄒ)/~~</em>)</p></p><p><p>Workaround 2: You can set <em>nsstringdrawingoptions options = Nsstringdrawinguseslinefragmentorigin | nsstringdrawingusesfontleading;</em><br>(note: It is not supported to write such a combination in swift, Click here to view the workaround www.jianshu.com/p/545f7f1d8741)</p></p><p><p>Workaround 3: When you are taking the height of the view to the height of the layout control, you need to get the <em>size = [string boundingrectwithsize:cgsizemake (getscreenwidth (), cgfloat_ MAX) options:options context:nil].size</em> converted to <em>Ceilf (size.height)</em>.</p></p><p><p>Workaround 4: Because this method computes the size of the string by taking a string of sizes to calculate, if you calculate a string containing a character such as \n\r, it will only be counted as a character. But when the display is \ n is the escape character, then the calculated height of the display is different, so you can use: calculated height = boundingrectwithsize calculated height + \n\r Escape character * The height of the number of occurrences of the single line of Text.</p></p><p><p><span style="font-size: 18pt; background-color: #ff6600">Extended:</span></p></p><span class="link_title"><span class="link_title">Textkit Learning (iv) by boundingRectWithSize:options:attributes:context: calculating text size: http://blog.csdn.net/jymn_chen/ article/details/10949279</span></span>Text Kit Learning (getting started and Advanced) http://www.cocoachina.com/industry/20131028/7250.html<p><p>IOS dynamically calculates line height, width, etc.</p></p></span>
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