弃用代码的替换:CGContextShowText

时间:2016-10-28 22:06:04

标签: ios objective-c core-graphics

我正在尝试将我的代码升级到iOS 10,而我却无法找到一些已弃用代码的替代品。通常Xcode 8显示替换,但在下面的例子中没有给出。我的旧代码是:

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

当我运行此代码时,它可以运行o.k.然后我尝试了以下内容:

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

Xcode iPhone模拟器运行时没有错误消息,但文本没有出现。 任何建议都将不胜感激。

查尔斯

1 个答案:

答案 0 :(得分:1)

你是对的,它是字体颜色。以下是替代代码:

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