Set the basic attributes of coretext

Source: Internet
Author: User

Use the following code to learn how to set the basic attributes of coretext and learn more about the basic attributes of coretext to set your own styles.

For more information, see:

Http://web.archiveorange.com/archive/v/nagQXJDPDGVNz9LFLmSK

Http://www.cocoanetics.com/2011/01/befriending-core-text/

First, inherit uiview and reload its drawrect function:

-(Void) drawrect :( cgrect) rect {// drawing code. // create the nsstring * longtext = @ "Yuan weilai lorem ipsum dolor sit Amet, before the iPad was released you had basically two ways how to get text on screen. either you wowould stick with uilabel or uitextview provided by uikit or if you felt hard-core you wowould draw the text yourself on the quartz level incurring all the headaches induced by having to mentally Switch between objective-C and c api functions. \ as of IOS 3.2 we gained a third alternative in core text promising full control over styles, thread safety and performance. however for most of my apps I did not want to break 3.x compatibility and so I procrastinated looking at this powerful new API. apps running only on iPads cocould have made use of core text from day 1, but to me it made more sense Supporting iPad via Hybrid apps where the iPhone part wocould still be backwards compatible. \ now as the year has turned the adoption of 4.x on all IOS platforms is ever more accelerating. creating new iPads where found under the Christmas tree and by now even the most stubborn people (read needing 3.x for jailbreaking and Sim-unlocking) have little reason to stick with 3. x. thus we have almost no incen Tive left to stick with 3.x compatibility. Yay !"; /*... * // Create extension * string = [[nsmutableattributedstring alloc] initwithstring: longtext]; // create a font and font size ctfontref Helvetica = ctfontcreatewithname (cfstr ("Helvetica"), 14.0, null); ctfontref helveticabold = ctfontcreatewithname (cfstr ("Helvetica-bold"), 14.0, null ); // Add the font target string from the subscript 0 to the end of the string [String addattribute :( ID) kctfontattributenamevalue :( ID) helveticarange: nsmakerange (0, [String Length])]; // Add the font target string starting from the subscript 0 and ending with four units of length [String addattribute :( ID) kctfontattributenamevalue :( ID) helveticaboldrange: nsmakerange (0, 4)]; // Add the font target string starting from subscript 6 and ending with five units of length [String addattribute :( ID) kctfontattributenamevalue :( ID) helveticaboldrange: nsmakerange (6, 5)]; // Add the font target string starting from subscript 109 and ending with nine units of length [String addattribute :( ID) kctfontattributenamevalue :( ID) helveticaboldrange: nsmakerange (109, 9)]; // Add the font target string starting from subscript 223 and ending with six units of length [String addattribute :( ID) kctfontattributenamevalue :( ID) helveticaboldrange: nsmakerange (223, 6)]; // Add color. The target string starts from the subscript 0 and ends with four units of length [String addattriue :( ID) kctforegroundcolorattributenamevalue :( ID) [uicolor bluecolor]. cgcolorrange: nsmakerange (0, 4)]; // The addition process is the same as that of [String addattriue :( ID) kctforegroundcolorattributenamevalue :( ID) [uicolor redcolor]. cgcolorrange: nsmakerange (18, 3)]; [String addattribute :( ID) kctforegroundcolorattributenamevalue :( ID) [uicolor greencolor]. cgcolorrange: nsmakerange (657, 6)]; [String addattribute :( ID) kctforegroundcolorattributenamevalue :( ID) [uicolor bluecolor]. cgcolorrange: nsmakerange (153, 6)]; // create text alignment = align; // align left with align right with ctparagraphstylesetting alignmentstyle; alignmentstyle. spec = kctparagraphstylespecifieralignment; // specify alignmentstyle as the alignmentstyle attribute. valuesize = sizeof (alignment); alignmentstyle. value = & alignment; // create text line spacing cgfloat linespace = 5.0f; // The spacing data ctparagraphstylesetting linespacestyle; linespacestyle. spec = kctparagraphstylespecifierlinespacing; // specify the line spacing attribute linespacestyle. valuesize = sizeof (linespace); linespacestyle. value = & linespace; // create a style Array Using settings [] = {alignmentstyle, linespacestyle}; // set the style ctparagraphstyleref paragraphstyle = ctparagraphstylecreate (settings, sizeof (settings )); // Add the style attribute to the string [String addattribute :( ID) kctparagraphstyleattributenamevalue :( ID) paragraphstylerange: nsmakerange (0, [String Length])]; // layout repeated framesetter = iterator (cfattributedstringref) string); cgmutablepathref leftcolumnpath = iterator (); cgpathaddrect (leftcolumnpath, null, cgrectmake (0, 0, self. bounds. size. width, self. bounds. size. height); ctframeref leftframe = second (framesetter, cfrangemake (0, 0), leftcolumnpath, null); // flip the coordinate systemcgcontextref context = second (); cgcontextsettextmatrix context, cgaffinetransformidentity); cgcontexttranslatectm (context, 0, self. bounds. size. height); cgcontextscalectm (context, 1.0,-1.0); // values (leftframe, context); // cleanupcgpathrelease (leftcolumnpath); cfrelease (framesetter); cfrelease (Helvetica ); cfrelease (helveticabold); [String release]; uigraphicspushcontext (context );}

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.