Create Rich Text codes in IOS +

Source: Internet
Author: User

# Import <coretext/coretext. h>

# Import <quartzcore/quartzcore. h>

Nsattributedstring * getattributedstring (){

Nsmutableattributedstring * attristring = [[nsmutableattributedstring
Alloc] initwithstring: @ "this is test! "]

Autorelease];

// Change the font color of this to red

[Attristring addattribute nsstring *) kctforegroundcolorattributename

Value ID) [uicolor
Redcolor]. cgcolor

Range: nsmakerange (0,
4)];

// Turns is yellow

[Attristring addattribute 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. value can be selected in the specified enumeration.

[Attristring addattribute nsstring *) kctunderlinestyleattributename

Value ID) [nsnumber
Numberwithint: kctunderlinestyledouble]

Range: nsmakerange (0,
4)];

Return attristring;

}

@ Interface tview: uiview

@ End

@ Implementation tview

-(ID) initwithframe :( cgrect) Frame

{

Self = [Super
Initwithframe: frame];

If (Self ){

Self. backgroundcolor = [uicolor
Clearcolor];

}

Return self;

}

-(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 ));

// Cgcontexttranslatectm (CTX, 0, rect. Size. Height );

// Cgcontextscalectm (CTX, 1,-1 );

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 );

}

@ End

@ Interface bidviewcontroller ()

@ End

@ Implementation bidviewcontroller

-(Void) viewdidload

{

[Super
Viewdidload];

Tview * view = [[tview
Alloc] initwithframe: cgrectmake (0,
(0,200,200)];

[Self. View
Addsubview: View];

[View release];

Catextlayer * textlayer = [catextlayer
Layer];

Textlayer. String = getattributedstring ();

Textlayer. Frame = cgrectmake (0,
Cgrectgetmaxy (view. Frame ),
200,200 );

[Self. View. Layer
Addsublayer: textlayer];

}

-(Void) viewdidunload

{

[Super
Viewdidunload];

// Release any retained subviews of the main view.

}

-(Bool) shouldautorotatetointerfaceorientation :( uiinterfaceorientation) interfaceorientation

{

Return (interfaceorientation! =
Uiinterfaceorientationportraitupsidedown );

}

@ End

Converted from created by zhangao on 5/30/12.

Related Article

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.