2016-10-28 54 views
1

我試圖將我的代碼升級到iOS 10,並且無法找到一些棄用代碼的替代品。通常Xcode 8顯示了一個替換,但在下面的情況下沒有給出。我的舊代碼是:替換棄用的代碼:CGContextShowText

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模擬器運行時沒有錯誤消息,但文本未顯示。 任何建議將不勝感激。

Charles

+0

1. x和y值是否合適? 2.文本是否爲「非零」? 3.你確定'UIFont'是'非零'嗎? 4.在繪製文本時,嘗試向屬性添加顏色。 – rmaddy

+0

是的,參數是有效的。我添加了:NSLog(@「text:%@,x:%.2f,Y:%.2f」,text,x,Y);並得到:text:NYSE TRIN 1.01 15:22:36,x:83.00,Y:277.75。此外,「Arial-BoldMT」的UIFont與舊代碼一起工作。顏色是「白色」,並在舊代碼中顯示爲藍色背景。 – CBrauer

+0

顏色不是白色,因爲您沒有在字體屬性中設置顏色。 – rmaddy

回答

0

你說得對,它是字體顏色。這裏是工作的替代代碼:

// 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); 
相關問題