2011-09-07 34 views
0

x和y是整數。iOS使用CGcontextshowtextatpoint爲某些字體的文本繪製矩形

int theSize = [caption.text length]; 
CGContextShowTextAtPoint(context, x, y, [caption.text UTF8String], theSize); 

但是當我使用某些字體,字母顯示爲矩形:當我使用CGBitmapCreateContext發送自定義創建的上下文,然後用普通的字體使用它像ArialMT

下面的代碼工作正常。於是,我就做什麼其他堆棧溢出回答表明,這是使用CoreText:

// supposedly a solution to glyphs problem 
CGContextSaveGState(context); 
CGContextSetRGBFillColor(context, 0, 0, 0, .5); 
[caption.text drawAtPoint:CGPointMake(x, y) withFont:caption.font]; 
CGContextRestoreGState(context); 

但是,這只是給了我一個錯誤:9月7日十三點33分31秒...:CGContextShowGlyphsWithAdvances:無效的情況下爲0x0

回答

0

矩形意味着字形在該字體中不可用。使用-drawAtPoint:withFont:將使用字體回退來使用其他字體繪製字形,但請注意,此不是核心文本,它只是UIStringDrawing。在任何情況下,這都會失敗,因爲-drawAtPoint:withFont:調用依賴於當前的UI圖形上下文堆棧,但是您使用的是不在堆棧上的自定義上下文。使用UIGraphicsBeginImageContext[WithOptions]()UIGraphicsEndImageContext()創建並銷燬您的CGContext,或者使用UIGraphicsPushContext()UIGraphicsPopContext()將您的上下文添加到堆棧並在之後再次刪除。

另外,你可以真正開始使用CoreText,它使用顯式上下文(就像CoreGraphics),但這將是更多的代碼行。