CBrauer
CBrauer

Reputation: 1079

Replacement for deprecated code: CGContextShowText

I'm trying to upgrade my code to iOS 10 and I'm having trouble finding a replacement for some deprecated code. Usually Xcode 8 shows a replacement, but in the following case none was given. My old code is:

CGContextSelectFont(context, "Arial-BoldMT", fontSize, kCGEncodingMacRoman);
CGContextSetTextPosition(context, x, y);
CGContextShowText(context, [text UTF8String], text.length);

When I run this code it works o.k. I then tried the following:

  // CGContextSetTextPosition(context, x, Y);
  // CGContextShowText(context, [text UTF8String], text.length);

  context = UIGraphicsGetCurrentContext();
  UIGraphicsPushContext(context);
  [text drawAtPoint:CGPointMake(x, Y)
      withAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Arial-BoldMT" size:12]}];
  UIGraphicsPopContext();

The Xcode iPhone simulator runs without error messages, but the text does not appear. Any suggestions would be greatly appreciated.

Charles

Upvotes: 1

Views: 885

Answers (1)

CBrauer
CBrauer

Reputation: 1079

You are right, it was the font color. Here is the replacement code that worked:

// CGContextSetTextPosition(context, x, Y);
// CGContextShowText(context, [text UTF8String], text.length);

context = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context);
[text drawAtPoint:CGPointMake(x, Y)
    withAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Arial-BoldMT" size:12], 
                                          NSForegroundColorAttributeName: [UIColor whiteColor]}];
UIGraphicsPopContext();
NSLog(@"text: %@, x: %.2f, Y: %.2f", text, x, Y);

Upvotes: 1

Related Questions