在iPhone上绘制Unicode字符

时间:2009-05-25 13:34:41

标签: iphone unicode text fonts drawing

为什么要弄清楚如何在iPhone上绘制Unicode字符这么难,在此过程中得出简单的字体指标,例如每个成像字形在选择字体中的宽度?

使用NSLayoutManager看起来很容易,但手机上显然没有该API。人们这样做的方式似乎是使用私有API CGFontGetGlyphsForUnichars,它不会让你通过Apple看门人进入App商店。

有人能指出我的文档,说明如何做到这一点?我快速失去了头发。

霍华德

3 个答案:

答案 0 :(得分:3)

我认为排除了CGFontGetGlyphsForUnichars 是一个疏忽而不是故意的举动,但我不是 把农场押在上面。所以我使用

[NSString drawAtPoint:withFont:];(在UIStringDrawing.h中)

[NSString sizeWithFont];

这也具有进行适当替换的优点 你字体上缺少字符的东西 CGContextShowGlyphs没有。

答案 1 :(得分:1)

如果您想绘制unicode而不是CGContextShowGlyphsAtPositions,CoreText就是答案。如果你需要自定义绘图,它也比[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);

答案 2 :(得分:0)

我已经为私人功能做了一个非常合适的替代品。在这里阅读: http://thoughts.codemelody.com/2009/07/a-replacement-for-cgfontgetglyphsforunichars/