为什么很难弄清楚如何在iPhone上绘制Unicode字符,沿途获得简单的字体指标,比如每个成像字形在选择字体中的宽度如何?在iPhone上绘制Unicode字符
它看起来很容易NSLayoutManager
,但该API显然在手机上不可用。看起来人们这样做的方式是使用私有API,CGFontGetGlyphsForUnichars
,它不会让你通过Apple守门人进入App Store。
任何人都可以指向我的文档,说明如何做到这一点?我快速脱发。
霍华德
为什么很难弄清楚如何在iPhone上绘制Unicode字符,沿途获得简单的字体指标,比如每个成像字形在选择字体中的宽度如何?在iPhone上绘制Unicode字符
它看起来很容易NSLayoutManager
,但该API显然在手机上不可用。看起来人们这样做的方式是使用私有API,CGFontGetGlyphsForUnichars
,它不会让你通过Apple守门人进入App Store。
任何人都可以指向我的文档,说明如何做到这一点?我快速脱发。
霍华德
我假设的CGFontGetGlyphsForUnichars
排除是监督,而不是刻意,但我不
投注在它的农场。所以不是我用
[NSString drawAtPoint:withFont:];
(在UIStringDrawing.h)
和
[NSString sizeWithFont];
这也对人物从您的字体缺少执行体面替代
的优势,一些
CGContextShowGlyphs
不这样做。
我已经做了一个非常合适的替代私人功能。在这里阅读: http://thoughts.codemelody.com/2009/07/a-replacement-for-cgfontgetglyphsforunichars/
CoreText是答案,如果你想绘制unicode而不是CGContextShowGlyphsAtPositions
。如果您需要自定义绘图,则它比[NSString drawAtPoint:withFont:]
要好。 这里是一个完整的例子:
CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)attributedString);
CFArrayRef runArray = CTLineGetGlyphRuns(line);
//in more complicated cases make loop on runArray
//here I assumed this array has only 1 CTRunRef within
const CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runArray, 0);
//do not use CTFontCreateWithName, otherwise you won't see e.g. chinese characters
const CTFontRef font = CFDictionaryGetValue(CTRunGetAttributes(run), kCTFontAttributeName);
CFIndex glyphCount = CTRunGetGlyphCount(run);
CGGlyph glyphs[glyphCount];
CGPoint glyphPositions[glyphCount];
CTRunGetGlyphs(run, CFRangeMake(0, 0), glyphs);
//you can modify positions further
CTRunGetPositions(run, CFRangeMake(0, 0), glyphPositions);
CTFontDrawGlyphs(font, glyphs, glyphPositions, glyphCount, context);
CFRelease(line);
你知道:就是这样。这需要处理我需要对角色进行图像处理的情况,而不是为其获取简单的字体指标。发现你可以跳入和跳出当前上下文(`UIGraphicsGetCurrentContext`)并在绘制前后使用`CGContextGetTextPosition`来获得边界框的宽度。这主要是我想要得到的,所以谢谢! – hkatz 2009-05-25 17:27:55