[英]Drawing Unicode characters on iPhone
为什么很难弄清楚如何在iPhone上绘制Unicode字符,并一路推导出简单的字体指标,例如每个图像字形在所选字体中的宽度?
使用NSLayoutManager
看起来很容易,但是该API显然在手机上不可用。 人们执行此操作的方式似乎是使用私有API CGFontGetGlyphsForUnichars
,它不会让您越过Apple网守进入App Store。
谁能指出我该怎么做的文档? 我正在迅速脱发。
霍华德
我假设排除了CGFontGetGlyphsForUnichars
只是一个疏忽而不是故意的举动,但是我不是
把农场押在上面。 所以我改用
[NSString drawAtPoint:withFont:];
(在UIStringDrawing.h中)
和
[NSString sizeWithFont];
这也具有执行体面替代的优势
在字体中缺少的字符上,
CGContextShowGlyphs
不这样做。
如果您要绘制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);
我已经为私有功能做了相当合适的替代。 在此处阅读有关内容: http : //thoughts.codemelody.com/2009/07/a-replacement-for-cgfontgetglyphsforunichars/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.