In the process of development, we often encounter the problem of dynamic calculation of row height.
-(cgrectcgsize) size options: (nsstringdrawingoptions Options attributes: (nullable nsdictionary <nsstring *, id > *) Attributes Context: (nullable Nsstringdrawingcontext *) Context ns_available ( Span class= "S3" >10_11, 7_0);
Is the Apple recommended calculation method, obviously encounter paragraph formatting problems, such as line spacing, indentation and other formatting requirements, attributes passed in the dictionary, including our set of fonts and formats, where nsparagraphstyleattributename is set paragraph style, Nsfontattributename is the setting font.
OK, take a look at the function of Nsparagraphstyleattributename.
[OBJC]View PlainCopy
- Nsparagraphstyleattributename paragraph style (set first line, line spacing, alignment, etc.) see what you need and write
- Nsmutableparagraphstyle *paragraphstyle = [[Nsmutableparagraphstyle alloc] init];
- Paragraphstyle. linespacing = 10; Line spacing for fonts
- Paragraphstyle. firstlineheadindent = 20. 0f; First line indent
- Paragraphstyle. Alignment = nstextalignmentjustified; (justified) Text alignment: (left, middle, right, justified, natural)
- Paragraphstyle. Linebreakmode = Nslinebreakbytruncatingtail; The end of the content with ... Way omitted ("... wxyz", "ABCD ...", "AB ... YZ ")
- Paragraphstyle. headindent = 20; Overall indent (except for the first row)
- Paragraphstyle. tailindent = 20; //
- Paragraphstyle. minimumlineheight = 10; Minimum row height
- Paragraphstyle. maximumlineheight = 20; Maximum row height
- Paragraphstyle. paragraphspacing = 15; spacing between segments and segments
- Paragraphstyle. Paragraphspacingbefore = 22. 0f; The Blank Space/* Distance between the bottom of the previous paragraph (or the end of its paragraphspacing, if any) and the top of this paragraph. */
- Paragraphstyle. basewritingdirection = nswritingdirectionlefttoright; The direction of writing from left to right (altogether?? Three types)
- Paragraphstyle. lineheightmultiple = 15; /* Natural line height are multiplied by this factor (if positive) before being constrained by minimum and maximum line Hei Ght. */
- Paragraphstyle. hyphenationfactor = 1; Hyphenation properties in iOS, the only supported values are 0 and 1, respectively
OK, now it's easy to calculate a paragraph height, for example:
[OBJC]View PlainCopy
- _descatt = [[nsmutableattributedstring alloc] initwithstring:_model. Desc];
- Uifont *descfont = [Uifont pingfangsc_regular_withsize:12];
- Nsmutableparagraphstyle *descstyle = [[Nsmutableparagraphstyle alloc]init];
- [Descstyle setlinespacing:1]; Line spacing
- Cgsize descsize = [_model. Desc Boundingrectwithsize:cgsizemake (w, maxfloat)
- Options:nsstringdrawinguseslinefragmentorigin
- Attributes:@{nsfontattributename:descfont,
- Nsparagraphstyleattributename:d Escstyle}
- context: nil]. Size;
In addition, several properties of rich Text processing are introduced:
[OBJC]View PlainCopy
- Nsfontattributename setting font properties, default: Font: Helvetica (Neue) font Size: 12
- Nsforegroundcolorattributenam set font color, value to Uicolor object, default value is black
- Nsbackgroundcolorattributename set the region background color of the font, the value is Uicolor object, the default value is nil, transparent color
- Nsligatureattributename sets the conjoined property, with a value of NSNumber object (integer), 0 for no conjoined character, and 1 for the default conjoined character
- Nskernattributename set character spacing, value is NSNumber object (integer), positive spacing is widened, and negative spacing narrows
- Nsstrikethroughstyleattributename set Strikethrough, value is NSNumber object (integer)
- Nsstrikethroughcolorattributename set strikethrough color, value is Uicolor object, default value is black
- Nsunderlinestyleattributename sets an underscore to a value of NSNumber object (integer), enumeration of values in constant Nsunderlinestyle, similar to strikethrough
- Nsunderlinecolorattributename sets the underline color, the value is the Uicolor object, the default value is black
- Nsstrokewidthattributename Sets the stroke width, takes a value of NSNumber object (integer), negative fill effect, positive hollow effect
- Nsstrokecolorattributename fills part of the color, not the font color, and takes the value of the Uicolor object
- Nsshadowattributename Set the Shadow property to a value of Nsshadow object
- Nstexteffectattributename sets the text special effect, the value is the NSString object, currently only the plate printing effect is available:
- Nsbaselineoffsetattributename set the baseline offset value to NSNumber (float), positive offset, negative bias
- Nsobliquenessattributename set the Glyph tilt, value NSNumber (float), positive right, negative left
- Nsexpansionattributename set text stretch properties, value nsnumber (float), positive values stretch text horizontally, negative values compress text horizontally
- Nswritingdirectionattributename setting the direction of writing, writing from left to right or from right to left
- Nsverticalglyphformattributename sets the text layout direction, takes a value of NSNumber object (integer), 0 for horizontal text, and 1 for vertical text
- Nslinkattributename set the link properties, click on the browser to open the specified URL address
- Nsattachmentattributename Set the text attachment, the value is the Nstextattachment object, commonly used in the text picture mixed row
- Nsparagraphstyleattributename set the text paragraph layout format, the value is Nsparagraphstyle object
ios-text paragraph style nsmutableparagraphstyle and Nsparagraphstyle use and some rich Text processing properties