IOS CoreText. framework --- basic usage

Source: Internet
Author: User
Tags alphanumeric characters

IOS CoreText. framework --- basic usage

 

The most common types of CoreText frameworks are as follows:

 

  1. CTFont
  2. CTFontCollection
  3. CTFontDescriptor
  4. CTFrame
  5. CTFramesetter
  6. CTGlyphInfo
  7. CTLine
  8. CTParagraphStyle
  9. CTRun
  10. CTTextTab
  11. CTTypesetter

     

    First, let's take a look at the overall window combination diagram of the framework:

    As a Canvas, CTFrame consists of rows, and each row can be divided into one or more small squares (CTRun ).

    Note: you do not need to create CTRun by yourself. Core Text will automatically create CTRun Based on the attributes of NSAttributedString. Each CTRun object corresponds to different attributes. Therefore, you can freely control the font, color, font spacing, and other information.

    Usually process steps:

    1. Use core text to display a string first, and then define the style of each part of the string-> attributedString-> Generate CTFramesetter-> Get CTFrame-> draw (CTFrameDraw)
    You can set the line feed, alignment, and area size in detail.
    2. The painting is only displayed, and the click event requires a judgment.
    CTFrame contains multiple ctlines and obtains the actual location and size of each line. Determine that the click is not on a line. CTLine can also determine the text range at this point (relative to the ctline coordinate. Traverse all nstextcheckingresults of the string, and judge that the click is not on this rang Based on the rang of the result to obtain the link and position of the click.

     

    Basic font knowledge:

     

    Font: A series of characters with the same Font size, style, and pound value (for example, Palatino, a 10-pound simhei ). Most of them are considered synonyms of words.

    Face: a combination of the scales and formats of all font sizes

    Font family: a set of related fonts (for example, Franklin family includes Franklin Gothic, Fran-klinHeavy, and Franklin Compressed)

    Weight: used to describe the font width. Typical pounds, ranging from the shortest to the shortest, are extremely fine, fine, book, moderate, semi-coarse, and extremely crude.

    Style: Roman type is a straight body, oblique type is italic, and utakuc type is italic and curve (more like a calligraphy body than Roman type ).

    X height: the average height of lowercase letters (based on X ). Two letters with the same pound value, the letter with the higher x height looks bigger than the letter with the lower x height

    Cap height: similar to x. The average height of uppercase letters (based on C)

    Descender: for example, in the letter q, the lower part of the baseline is the lower part.

    Ascender: the upper and lower part of x (such as letter B) is called the upper and lower part.

    Baseline: The line usually under x, v, B, and m.

    Stroke: a line or curve that constitutes a character. Bold or character shapes can be changed

    Serif: a horizontal line used to make characters more visible. For example, the upper left corner and lower part of the letter are horizontal lines.

    Sans Serif: The Sans Serif can be used by the typesetting staff not to deploy the Sans Serif.

    Block: a stroke in this font makes the character look more conspicuous than the silent line, but it is less than the degree of common lines. For example, Lubalin Graph is a square word, which looks like wood blocks.

    Calligraphic script: a font used to simulate handwriting. For example, Murray Hill or Fraktur font

    Decorative: A painting-like font

    Pi (Pisymbol): a special symbol for non-standard alphanumeric characters. For example, Wingdings and Mathematical Pi

    Ligature: A series of writing letters, such as fi, fl, ffi, or ffl. Because the characters are often in the shape of letters, the typesetting staff are used to connecting them.


     

    Character attribute name:

     

    const CFStringRef kCTCharacterShapeAttributeName;              
    // The font shape attribute must be a CFNumberRef object. The default value is 0. If the value is not 0, the corresponding character shape is defined. For example, 1 indicates the traditional character shape.
    const CFStringRef kCTFontAttributeName;  
    // The font attribute must be the CTFont object const CFStringRef kCTKernAttributeName;
    // The character interval attribute must be the CFNumberRef object const CFStringRef kCTLigatureAttributeName;
    // Set whether to use the hyphen property. If it is set to 0, the hyphen property is not used. The standard English hyphen (FI) and FL. The default value is 1. Both use the standard hyphen. That is, when f is searched, fl is considered as a text. It must be CFNumberRef. The default value is 1. The value 0, 1, and 2 const CFStringRef kCTForegroundColorAttributeName can be obtained;
    // The font color attribute must be a CGColor object. The default value is blackconst CFStringRef kCTForegroundColorFromContextAttributeName;
    // The context font color attribute must be CFBooleanRef; default value: False; const CFStringRef kCTParagraphStyleAttributeName;
    // The Section style attribute must be a CTParagraphStyle object. The default value is NILconst CFStringRef kCTStrokeWidthAttributeName;
    // The stroke line width must be a CFNumberRef object. The default value is 0.0f. The standard value is 3.0 fconst CFStringRef kCTStrokeColorAttributeName;
    // The color attribute of a stroke must be a CGColorRef object. The default value is const CFStringRef kCTSuperscriptAttributeName;
    // Set the font's upper/lower mark attribute to 0 by default for the CFNumberRef object. It can be-1 as the subscript, and 1 as the upper mark, which must be supported by the font. For example, Cn1const CFStringRef kCTUnderlineColorAttributeName;
    // The font underline color attribute must be a CGColorRef object. The default value is const CFStringRef kCTUnderlineStyleAttributeName;
    // The font underline style attribute must be a CFNumberRef object. If it is kCTUnderlineStyleNone, you can use CTUnderlineStypleModifiers to modify the underline style const CFStringRef kCTVerticalFormsAttributeName;
    // The font direction attribute of the text must be CFBooleanRef. The default value is false. false indicates the horizontal direction. true indicates the const CFStringRef kCTGlyphInfoAttributeName in the vertical direction;
    // The font information attribute must be the CTGlyphInfo object const CFStringRef kCTRunDelegateAttributeName
    // The CTRun delegate attribute must be a CTRunDelegate object.
    Example:

     

     

    [Cpp]View plaincopy
    1. NSMutableAttributedString * mabstring = [[NSMutableAttributedString alloc] initWithString: @ This is a test of characterAttribute. Chinese character];

       

       

      [Cpp]View plaincopy
      1. // Set Font attributes
      2. CTFontRef font = CTFontCreateWithName (CFSTR (Georgia), 40, NULL );
      3. [Mabstring addattriange :( id) kCTFontAttributeName value :( id) font range: NSMakeRange (0, 4)];
        [Cpp]View plaincopy
        1. // Set italics
        2. CTFontRef font = CTFontCreateWithName (CFStringRef) [UIFont italicSystemFontOfSize: 20]. fontName, 14, NULL );
        3. [Mabstring addattriange :( id) kCTFontAttributeName value :( id) font range: NSMakeRange (0, 4)];
          [Cpp]View plaincopy
          1. // Underline
          2. [Mabstring addattriename :( id) kCTUnderlineStyleAttributeName value :( id) [NSNumber numberWithInt: kCTUnderlineStyleDouble] range: NSMakeRange (0, 4)];
            [Cpp]View plaincopy
            1. // Underline color
            2. [Mabstring addattriename :( id) kCTUnderlineColorAttributeName value :( id) [UIColor redColor]. CGColor range: NSMakeRange (0, 4)];

              [Cpp]View plaincopy
              1. // Set the font to simple separated by eg: test
              2. Long number = 10;
              3. CFNumberRef num = CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt8Type, & number );
              4. [Mabstring addattriange :( id) kCTKernAttributeName value :( id) num range: NSMakeRange (10, 4)];
                [Cpp]View plaincopy
                1. // Set the hyphen
                2. Long number = 1;
                3. CFNumberRef num = CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt8Type, & number );
                4. [Mabstring addAttribute :( id) kCTLigatureAttributeName value :( id) num range: NSMakeRange (0, [str length])]; The hyphen is not used yet and no effect is displayed.
                  [Cpp]View plaincopy
                  1. // Set the font color
                  2. [Mabstring addattriename :( id) kCTForegroundColorAttributeName value :( id) [UIColor redColor]. CGColor range: NSMakeRange (0, 9)];
                    [Cpp]View plaincopy
                    1. // Set the font color to the prefix color.
                    2. CFBooleanRef flag = kCFBooleanTrue;
                    3. [Mabstring addattriename :( id) kCTForegroundColorFromContextAttributeName value :( id) flag range: NSMakeRange (5, 10)]; no obvious effect.

                       

                       

                      [Cpp]View plaincopy
                      1. // Set hollow words
                      2. Long number = 2;
                      3. CFNumberRef num = CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt8Type, & number );
                      4. [Mabstring addAttribute :( id) kCTStrokeWidthAttributeName value :( id) num range: NSMakeRange (0, [str length])];
                        [Cpp]View plaincopy
                        1. // Set hollow words
                        2. Long number = 2;
                        3. CFNumberRef num = CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt8Type, & number );
                        4. [Mabstring addAttribute :( id) kCTStrokeWidthAttributeName value :( id) num range: NSMakeRange (0, [str length])];
                        5.  
                        6. // Set the font color
                        7. [Mabstring addattriename :( id) kCTStrokeColorAttributeName value :( id) [UIColor greenColor]. CGColor range: NSMakeRange (0, [str length])];
                          When you set the font color, you must first set the font height to hollow, or set the color to no effect.

                           

                          [Cpp]View plaincopy
                          1. // Set multiple attributes for the same font family
                          2. // Red
                          3. NSMutableDictionary * attributes = [NSMutableDictionary dictionaryWithObject :( id) [UIColor redColor]. CGColor forKey :( id) kCTForegroundColorAttributeName];
                          4. // Italic
                          5. CTFontRef font = CTFontCreateWithName (CFStringRef) [UIFont italicSystemFontOfSize: 20]. fontName, 40, NULL );
                          6. [Attributes setObject :( id) font forKey :( id) kCTFontAttributeName];
                          7. // Underline
                          8. [Attributes setObject :( id) [NSNumber numberWithInt: kCTUnderlineStyleDouble] forKey :( id) kCTUnderlineStyleAttributeName];
                          9.  
                          10. [Mabstring addAttributes: attributes range: NSMakeRange (0, 4)];

                             

                            It's draw.

                             

                            [Cpp]View plaincopy
                            1. -(Void) characterAttribute
                            2. {
                            3. NSString * str = @ This is a test of characterAttribute. Chinese character;
                            4. NSMutableAttributedString * mabstring = [[NSMutableAttributedString alloc] initWithString: str];
                            5.  
                            6. [Mabstring beginEditing];
                            7. /*
                            8. Long number = 1;
                            9. CFNumberRef num = CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt8Type, & number );
                            10. [Mabstring addattriename :( id) kCTCharacterShapeAttributeName value :( id) num range: NSMakeRange (0, 4)];
                            11. */
                            12. /*
                            13. // Set Font attributes
                            14. CTFontRef font = CTFontCreateWithName (CFSTR (Georgia), 40, NULL );
                            15. [Mabstring addattriange :( id) kCTFontAttributeName value :( id) font range: NSMakeRange (0, 4)];
                            16. */
                            17. /*
                            18. // Set the font to simple separated by eg: test
                            19. Long number = 10;
                            20. CFNumberRef num = CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt8Type, & number );
                            21. [Mabstring addattriange :( id) kCTKernAttributeName value :( id) num range: NSMakeRange (10, 4)];
                            22. */
                            23.  
                            24. /*
                            25. Long number = 1;
                            26. CFNumberRef num = CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt8Type, & number );
                            27. [Mabstring addAttribute :( id) kCTLigatureAttributeName value :( id) num range: NSMakeRange (0, [str length])];
                            28. */
                            29. /*
                            30. // Set the font color
                            31. [Mabstring addattriename :( id) kCTForegroundColorAttributeName value :( id) [UIColor redColor]. CGColor range: NSMakeRange (0, 9)];
                            32. */
                            33. /*
                            34. // Set the font color to the prefix color.
                            35. CFBooleanRef flag = kCFBooleanTrue;
                            36. [Mabstring addattriename :( id) kCTForegroundColorFromContextAttributeName value :( id) flag range: NSMakeRange (5, 10)];
                            37. */
                            38.  
                            39. /*
                            40. // Set hollow words
                            41. Long number = 2;
                            42. CFNumberRef num = CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt8Type, & number );
                            43. [Mabstring addAttribute :( id) kCTStrokeWidthAttributeName value :( id) num range: NSMakeRange (0, [str length])];
                            44.  
                            45. // Set the font color
                            46. [Mabstring addattriename :( id) kCTStrokeColorAttributeName value :( id) [UIColor greenColor]. CGColor range: NSMakeRange (0, [str length])];
                            47. */
                            48.  
                            49. /*
                            50. Long number = 1;
                            51. CFNumberRef num = CFNumberCreate (kCFAllocatorDefault, kCFNumberSInt8Type, & number );
                            52. [Mabstring addAttribute :( id) kCTSuperscriptAttributeName value :( id) num range: NSMakeRange (3, 1)];
                            53. */
                            54.  
                            55. /*
                            56. // Set italics
                            57. CTFontRef font = CTFontCreateWithName (CFStringRef) [UIFont italicSystemFontOfSize: 20]. fontName, 14, NULL );
                            58. [Mabstring addattriange :( id) kCTFontAttributeName value :( id) font range: NSMakeRange (0, 4)];
                            59. */
                            60.  
                            61. /*
                            62. // Underline
                            63. [Mabstring addattriename :( id) kCTUnderlineStyleAttributeName value :( id) [NSNumber numberWithInt: kCTUnderlineStyleDouble] range: NSMakeRange (0, 4)];
                            64. // Underline color
                            65. [Mabstring addattriename :( id) kCTUnderlineColorAttributeName value :( id) [UIColor redColor]. CGColor range: NSMakeRange (0, 4)];
                            66. */
                            67.  
                            68.  
                            69.  
                            70. // Set multiple attributes for the same font family
                            71. // Red
                            72. NSMutableDictionary * attributes = [NSMutableDictionary dictionaryWithObject :( id) [UIColor redColor]. CGColor forKey :( id) kCTForegroundColorAttributeName];
                            73. // Italic
                            74. CTFontRef font = CTFontCreateWithName (CFStringRef) [UIFont italicSystemFontOfSize: 20]. fontName, 40, NULL );
                            75. [Attributes setObject :( id) font forKey :( id) kCTFontAttributeName];
                            76. // Underline
                            77. [Attributes setObject :( id) [NSNumber numberWithInt: kCTUnderlineStyleDouble] forKey :( id) kCTUnderlineStyleAttributeName];
                            78.  
                            79. [Mabstring addAttributes: attributes range: NSMakeRange (0, 4)];
                            80.  
                            81.  
                            82.  
                            83. Nsange kk = NSMakeRange (0, 4 );
                            84.  
                            85. NSDictionary * dc = [mabstring attributesAtIndex: 0 paitiverange: & kk];
                            86.  
                            87. [Mabstring endEditing];
                            88.  
                            89. NSLog (@ value = % @, dc );
                            90.  
                            91.  
                            92.  
                            93. CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString (CFAttributedStringRef) mabstring );
                            94.  
                            95. CGMutablePathRef Path = CGPathCreateMutable ();
                            96.  
                            97. CGPathAddRect (Path, NULL, CGRectMake (10, 0, self. bounds. size. width-10, self. bounds. size. height-10 ));
                            98.  
                            99. CTFrameRef frame = CTFramesetterCreateFrame (framesetter, CFRangeMake (0, 0), Path, NULL );
                            100.  
                            101. // Obtain the current (View) context to facilitate subsequent painting. This is an off-screen.
                            102. CGContextRef context = UIGraphicsGetCurrentContext ();
                            103.  
                            104. CGContextSetTextMatrix (context, CGAffineTransformIdentity );
                            105.  
                            106. // Press the stack and press it into the graphical state stack. Each graphic context maintains a graphical state stack. Not all elements of the graphics state of the current painting environment are saved. The current path is not considered in the graph status, so it is not saved.
                            107. // Save the current context image status. No matter what you draw on the context in the future, the real screen will not be affected.
                            108. CGContextSaveGState (context );
                            109.  
                            110. // X and Y-axis move
                            111. CGContextTranslateCTM (context, 0, self. bounds. size. height );
                            112.  
                            113. // Scale x and Y axis,-1.0 is 1.0 times in reverse direction, coordinate system conversion, turning 180 degrees along the x axis
                            114. CGContextScaleCTM (context, 1.0,-1.0 );
                            115.  
                            116. CTFrameDraw (frame, context );
                            117.  
                            118. CGPathRelease (Path );
                            119. CFRelease (framesetter );
                            120. }
                              [Cpp]View plaincopy
                              1. -(Void) drawRect :( CGRect) rect
                              2. {
                              3. [Self characterAttribute];
                              4. }

                                CORETEXT framework

                                For more information about Context, see: http://www.padovo.com/blog/2013/01/31/study-coretext/

                                 

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.