iOS nsattributedstring specific explanations
Nsattributedstring allows us to diversify a string display, but now to iOS 5, it seems to support it not very well, because it is not easy to display (at least not on OS X).
First, import the Coretext.framework and import it in the file you need to use:
#import<CoreText/CoreText.h>
Create a nsmutableattributedstring:
- nsmutableattributedstring *attristring = [[[Nsmutableattributedstring alloc] initwithstring:@"This is test! " ]
- Autorelease];
It's a normal way to create it, and we'll configure it with properties:
- //Change the font color of this to red
- [Attristring AddAttribute: (NSString *) kctforegroundcolorattributename
- Value: (ID) [Uicolor redcolor]. Cgcolor
- Range:nsmakerange (0, 4)];
- //Turn the IS into yellow
- [Attristring AddAttribute: (NSString *) kctforegroundcolorattributename
- Value: (ID) [Uicolor yellowcolor]. Cgcolor
- Range:nsmakerange (5, 2)];
- //Change the font of this, value must be a ctfontref
- [Attristring AddAttribute: (NSString *) kctfontattributename
- Value: (ID) ctfontcreatewithname ((cfstringref) [Uifont Boldsystemfontofsize:14].fontname,
- 14,
- NULL)
- Range:nsmakerange (0, 4)];
- //underline this, value can be selected in the specified enumeration
- [Attristring AddAttribute: (NSString *) kctunderlinestyleattributename
- Value: (ID) [NSNumber numberwithint:kctunderlinestyledouble]
- Range:nsmakerange (0, 4)];
- return attristring;
This is a good configuration, but we can find nsattributedstring inherited from NSObject, and do not support whatever draw method, then we can only draw the own. Write a UIView subclass (if named Tview), set the background color to transparent in initwithframe (Self.backgroundcolor = [Uicolor Clearcolor]), and then override the DrawRect method:
- -(void) DrawRect: (cgrect) rect{
- [Super Drawrect:rect];
- Nsattributedstring *attristring = getattributedstring ();
- Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
- CGCONTEXTCONCATCTM (CTX, Cgaffinetransformscale (cgaffinetransformmaketranslation (0, Rect.size.height), 1.F, -1.f));
- Ctframesetterref Framesetter = ctframesettercreatewithattributedstring ((cfattributedstringref) attriString);
- Cgmutablepathref path = cgpathcreatemutable ();
- Cgpathaddrect (Path, NULL, rect);
- Ctframeref frame = Ctframesettercreateframe (Framesetter, cfrangemake (0, 0), path, NULL);
- Cfrelease (path);
- Cfrelease (Framesetter);
- Ctframedraw (frame, CTX);
- Cfrelease (frame);
- }
In the code we have adjusted the CTM (current transformation matrix) because the coordinate system of the quartz 2D is different, for example (10, 10) to (20, 20) of the line coordinates:
Coordinates are similar to the coordinates in mathematics, can not adjust the CTM first, see what it looks like, the following two adjustment methods are identical:
- CGCONTEXTCONCATCTM (CTX, Cgaffinetransformscale (cgaffinetransformmaketranslation (0, Rect.size.height), 1.F, -1.f));
==
- CGCONTEXTTRANSLATECTM (CTX, 0, rect.size.height);
- CGCONTEXTSCALECTM (CTX, 1,-1);
Ctframesetter is ctframe to create a factory, nsattributedstring need to draw to the interface through Ctframe, get ctframesetter, create path (draw path), and then get Ctframe, Finally, the Ctframedraw method is used to draw to the interface .
If you want to calculate the size of the nsattributedstring, you need to use this API:
Ctframesettersuggestframesizewithconstraints, with nsstring of Sizewithfont count when the number of rows will be counted, because in Coretext, line spacing is also you to control.
Setting the line spacing and wrapping mode is all set to a property: Kctparagraphstyleattributename, this property is divided into very many sub-
property, which contains
- Kctlinebreakbycharwrapping
- Kctparagraphstylespecifierlinespacingadjustment
settings such as the following:
- //Paragraph
- //line Break
- Ctparagraphstylesetting Linebreakmode;
- Ctlinebreakmode lineBreak = kctlinebreakbycharwrapping; //Line break mode
- Linebreakmode.spec = Kctparagraphstylespecifierlinebreakmode;
- Linebreakmode.value = &lineBreak;
- linebreakmode.valuesize = sizeof(Ctlinebreakmode);
- //Line spacing
- Ctparagraphstylesetting linespacing;
- cgfloat spacing = 4.0; //Specify spacing
- Linespacing.spec = kctparagraphstylespecifierlinespacingadjustment;
- Linespacing.value = &spacing;
- linespacing.valuesize = sizeof(cgfloat);
- Ctparagraphstylesetting settings[] = {linebreakmode,linespacing};
- Ctparagraphstyleref Paragraphstyle = ctparagraphstylecreate (settings, 2); //The second parameter is the length of the settings
- [Attributedstring AddAttribute: (NSString *) kctparagraphstyleattributename
- Value: (ID) paragraphstyle
- Range:nsmakerange (0, attributedstring.length)];
-----------------------------------------the wretched dividing line-----------------------------------------
This is not the only way, there is an alternative:
- Catextlayer *textlayer = [Catextlayer layer];
- textlayer.string = Getattributedstring ();
- Textlayer.frame = CGRectMake (0, Cgrectgetmaxy (view.frame), 200, 200);
- [Self.view.layer Addsublayer:textlayer];
Catextlayer can directly support nsattributedstring!
-----------------------------------------the wretched dividing line-----------------------------------------
:
Source code Address-----------------------------------------The wretched dividing line-----------------------------------------Now found that there is a problem, as if the Chinese all will be bold, set the bold font is useless, should be coretext bug, has been reported to Apple.
-----------------------------------------for The questions raised by Brother CYTHB -----------------------------------------
First, thank you for your attention
There are indeed inappropriate descriptions in the original text, for example: theupper-left corner of the context is (0, 0). In fact the quartz2d coordinate system is indeed in the lower left corner, and there are just a few techniques that use a different default coordinate system than quartz when setting up their graphics context. These coordinate systems are modified coordinate systems (modified coordinate system) relative to quartz.
UPDATEDAfter iOS6, creating a attributedstring becomes an easy thing,<CoreText/CoreText.h>import is not required. Let's say I want to set the color of the font, just like this:
- [textattr addattribute: Nsforegroundcolorattributename
- value : [Uicolor redcolor]
- Range : Nsmakerange (0, text. Length)];
iOS nsattributedstring specific explanations