CGContextShowGlyphsAtPoint DEPRECATED

After spending quite some time to display the "Thai Phonetic YK" fonts in the iPhone app. I finally figured it out and worked.

Even though it works, there is still a complaint (warning) from the compiler about one line of code in the (void) drawRect method: my class doing the rendering.

CGContextShowGlyphsAtPoint(context, 20, 50, textToPrint, textLength);

      

The compiler tells me that this code is DEPRECATED. My question is, "How should I change it?" Even though I searched the net for the answer, I didn't understand anything. The documentation says something like "Use body text" which is too vague to be considered an answer.

+3


source to share


1 answer


Core Graphics:

void CGContextShowGlyphsAtPoint (
   CGContextRef context,
   CGFloat x,
   CGFloat y,
   const CGGlyph glyphs[],
   size_t count
);

      

Main text:

void CTFontDrawGlyphs (
   CTFontRef font,
   const CGGlyph glyphs[],
   const CGPoint positions[],
   size_t count,
   CGContextRef context
);

      

The Core Text version requires a CTFontRef (in the Core Graphics version, the font must be set in the context).

You can get CTFontRef from UIFont:

    CTFontRef ctFont = CTFontCreateWithName( (__bridge CFStringRef)uiFont.fontName, uiFont.pointSize, NULL);

      

The CT version also requires an array of points, one for each glyph. Assuming you are drawing a single glyph with CG code, you can create an array like this:

        CGPoint point = CGPointMake(x, y);
        const CGPoint* positions = &point;

      

This change means that you need the position of the position for each glyph. In my case, the extra work was minimal: I was promoting the typesetter one character at a time (for curved text), so I had to do this calculation anyway.

You might be able to type one text run at a time with CTRun:



  void CTRunDraw ( 
      CTRunRef run, 
      CGContextRef context, 
      CFRange range );    

      

This can save you the trouble of repeating every character. You could use it something like this ...

CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CTLineRef line = CTLineCreateWithAttributedString(
      (__bridge CFTypeRef)self.attributedString);

  CFArrayRef runs = CTLineGetGlyphRuns(line);
  CFIndex runCount = CFArrayGetCount(runs);
  for (CFIndex runIndex = 0; runIndex < runCount; ++runIndex) {
      CTRunRef run = CFArrayGetValueAtIndex(runs, runIndex);
      [self adjustContextForRun:run];
       CTRunDraw (run, context, 0)
 }

      

(this is just a sketch, the implementation will depend on your needs and I have not tested the code)

adjustContextForRun

will be responsible for setting things like font and drawing starting position for run.

Each CTRun is a subrange of an attribute string where all attributes are the same. If you don't change the attributes line by line, you can abstract this further:

  CTLineDraw(line, context);

      

I don't know if this abstraction layer will work in your case (I only use it for working with roman fonts), but it's worth knowing it there, saving a lot of lower-level problems.

You can set the drawing start position for the text as follows:

 void CGContextSetTextPosition ( 
    CGContextRef c, 
    CGFloat x, 
    CGFloat y );

      

+3


source







All Articles