) Diversified display of iPhone strings (nsattributedstring)

Source: Internet
Author: User

Nsattributedstring allows us to diversify the display of a string, but so far as iOS 5 does not seem to support it well, because it is not easy to display (at least not convenient on OS X ).

First, import coretext. Framework and import it to the file to be used:

# Import <coretext/coretext. h>

Create an nsmutableattributedstring:

NSMutableAttributedString *attriString = [[[NSMutableAttributedString alloc] initWithString:@"this is test!"]                                                 autorelease];  

The general creation method is as follows:

// Change the font color of this to red [attristring addattribute :( nsstring *) kctforegroundcolorattributename value :( ID) [uicolor redcolor]. cgcolor range: nsmakerange (0, 4)]; // changes is to yellow [attristring addattriename :( nsstring *) kctforegroundcolorattributename value :( ID) [uicolor yellowcolor]. cgcolor range: nsmakerange (5, 2)]; // change the font of this. The 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. You can select [attristring addattribute :( nsstring *) as the value in the specified enumeration *) kctunderlinestyleattributename value :( ID) [nsnumber numberwithint: kctunderlinestyledouble] range: nsmakerange (0, 4)];

Even if the configuration is complete, we can find that nsattributedstring inherits from nsobject and does not support any draw method, so we can only do it ourselves. Write a subclass of uiview (named tview), set the background color to transparent (self. backgroundcolor = [uicolor clearcolor]) in initwithframe, And Then override the drawrect method:

-(void)drawRect:(CGRect)rect{      [super drawRect:rect];            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 adjusted the CTM (current transformation matrix), because the coordinate system of quartz 2D is different.

Ctframesetter is the creation factory of ctframe. nsattributedstring needs to be drawn to the interface through ctframe. After ctframesetter is obtained, a path is created, and a ctframe is obtained. Finally, the ctframedraw method is used to draw the interface.

If you want to calculate the size required by nsattributedstring, you need to use this API:

Ctframesettersuggestframesizewithconstraints. If you use sizewithfont of nsstring to calculate multiple rows, you cannot control the row spacing in coretext.

Setting the line spacing and line feed mode both sets an attribute: kctparagraphstyleattributename, which is divided into many sub-attributes.

Properties, including

 

  • Kctlinebreakbycharwrapping
  • Kctparagraphstylespecifierlinespacingadjustment

 

// Section // line break ctparagraphstylesetting linebreakmode; ctlinebreakmode linebreak = kctlinebreakbycharwrapping; // line feed mode linebreakmode. spec = kctparagraphstylespecifierlinebreakmode; linebreakmode. value = & linebreak; linebreakmode. valuesize = sizeof (ctlinebreakmode); // line spacing ctparagraphstylesetting linespacing; cgfloat spacing = 4.0; // specify the 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 settings [attributedstring addattribute :( nsstring *) kctparagraphstyleattributename value :( ID) paragraphstyle range: nsmakerange (0, attributedstring. length)];

 

----------------------------------------- The boundary -----------------------------------------

This is not the only method, but there is another 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!

 

<A href = "http://download.csdn.net/detail/zhangao0086/4340376"> Source Code address </a>

 

Original article: http://blog.csdn.net/zhangao0086/article/details/7616385

By yytong

 

 

 

 

 

 

 

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.