iOS nsattributedstring specific explanations

Source: Internet
Author: User


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:

    1. nsmutableattributedstring *attristring = [[[Nsmutableattributedstring alloc] initwithstring:@"This is test! " ]   
    2. Autorelease];
It's a normal way to create it, and we'll configure it with properties:
  1. //Change the font color of this to red   
  2. [Attristring AddAttribute: (NSString *) kctforegroundcolorattributename
  3. Value: (ID) [Uicolor redcolor]. Cgcolor
  4. Range:nsmakerange (0, 4)];
  5. //Turn the IS into yellow   
  6. [Attristring AddAttribute: (NSString *) kctforegroundcolorattributename
  7. Value: (ID) [Uicolor yellowcolor]. Cgcolor
  8. Range:nsmakerange (5, 2)];
  9. //Change the font of this, value must be a ctfontref   
  10. [Attristring AddAttribute: (NSString *) kctfontattributename
  11. Value: (ID) ctfontcreatewithname ((cfstringref) [Uifont Boldsystemfontofsize:14].fontname,
  12. 14,
  13. NULL)
  14. Range:nsmakerange (0, 4)];
  15. //underline this, value can be selected in the specified enumeration   
  16. [Attristring AddAttribute: (NSString *) kctunderlinestyleattributename





    1. Value: (ID) [NSNumber numberwithint:kctunderlinestyledouble]
    2. Range:nsmakerange (0, 4)];
    3. 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:

  1. -(void) DrawRect: (cgrect) rect{
  2. [Super Drawrect:rect];
  3. Nsattributedstring *attristring = getattributedstring ();
  4. Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
  5. CGCONTEXTCONCATCTM (CTX, Cgaffinetransformscale (cgaffinetransformmaketranslation (0, Rect.size.height), 1.F, -1.f));
  6. Ctframesetterref Framesetter = ctframesettercreatewithattributedstring ((cfattributedstringref) attriString);
  7. Cgmutablepathref path = cgpathcreatemutable ();
  8. Cgpathaddrect (Path, NULL, rect);
  9. Ctframeref frame = Ctframesettercreateframe (Framesetter, cfrangemake (0, 0), path, NULL);
  10. Cfrelease (path);
  11. Cfrelease (Framesetter);
  12. Ctframedraw (frame, CTX);
  13. Cfrelease (frame);
  14. }

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:

    1. CGCONTEXTCONCATCTM (CTX, Cgaffinetransformscale (cgaffinetransformmaketranslation (0, Rect.size.height), 1.F, -1.f));
==
    1. CGCONTEXTTRANSLATECTM (CTX, 0, rect.size.height);
    2. 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:

  1. //Paragraph   
  2. //line Break   
  3. Ctparagraphstylesetting Linebreakmode;
  4. Ctlinebreakmode lineBreak = kctlinebreakbycharwrapping; //Line break mode   
  5. Linebreakmode.spec = Kctparagraphstylespecifierlinebreakmode;
  6. Linebreakmode.value = &lineBreak;
  7. linebreakmode.valuesize = sizeof(Ctlinebreakmode);
  8. //Line spacing   
  9. Ctparagraphstylesetting linespacing;
  10. cgfloat spacing = 4.0; //Specify spacing   
  11. Linespacing.spec = kctparagraphstylespecifierlinespacingadjustment;
  12. Linespacing.value = &spacing;
  13. linespacing.valuesize = sizeof(cgfloat);
  14. Ctparagraphstylesetting settings[] = {linebreakmode,linespacing};
  15. Ctparagraphstyleref Paragraphstyle = ctparagraphstylecreate (settings, 2); //The second parameter is the length of the settings   
  16. [Attributedstring AddAttribute: (NSString *) kctparagraphstyleattributename
  17. Value: (ID) paragraphstyle
  18. Range:nsmakerange (0, attributedstring.length)];

-----------------------------------------the wretched dividing line-----------------------------------------

This is not the only way, there is an alternative:

    1. Catextlayer *textlayer = [Catextlayer layer];
    2. textlayer.string = Getattributedstring ();
    3. Textlayer.frame = CGRectMake (0, Cgrectgetmaxy (view.frame), 200, 200);
    4. [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:

    1. [textattr addattribute: Nsforegroundcolorattributename
    2. value : [Uicolor redcolor]
    3. Range : Nsmakerange (0, text. Length)];


iOS nsattributedstring specific explanations

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.