【问题标题】:Drawing Unicode characters on iPhone在 iPhone 上绘制 Unicode 字符
【发布时间】:2010-10-28 18:02:00
【问题描述】:

为什么很难弄清楚如何在 iPhone 上绘制 Unicode 字符,沿途导出简单的字体指标,例如每个图像字形在所选字体中的宽度?

使用NSLayoutManager 似乎很容易,但该 API 显然在手机上不可用。人们这样做的方式似乎是使用私有 API,CGFontGetGlyphsForUnichars,这不会让您通过 Apple 看门人进入 App Store。

任何人都可以向我指出显示如何执行此操作的文档吗?我掉头发很快。

霍华德

【问题讨论】:

    标签: iphone unicode text fonts drawing


    【解决方案1】:

    我假设排除CGFontGetGlyphsForUnichars
    是疏忽而不是故意的举动,但是我不是
    把农场赌在上面。所以我改为使用

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

    [NSString sizeWithFont];

    这还具有执行体面替换的优势
    关于字体中缺少的字符,
    CGContextShowGlyphs 不会这样做。

    【讨论】:

    • 你知道:差不多了。这满足了我对字符进行成像的需要,但不是为它获取简单的字体度量。事实证明,您可以跳入和跳出当前上下文 (UIGraphicsGetCurrentContext) 并在绘制前后使用CGContextGetTextPosition 基本上获得边界框的宽度。这主要是我感兴趣的推导,所以谢谢!
    • 等等,[@"a" sizeWithFont:<font>] 怎么了?为什么需要单个字符的指标?
    • 它给了我指标,因为我为每个字符做了一个 sizeWithFont。 Andrew:也许他想将角色缓存到纹理中,或者沿着路径绘制它们。
    【解决方案2】:

    如果您想绘制 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);
    

    【讨论】:

    • 我需要将用draw(rect:)绘制的字符导出成字符串,以后可以用作字符串字母吗?它会帮助我吗?如果不是,我应该如何使用手绘字母作为字符串?
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2016-09-05
      • 1970-01-01
      • 2011-05-04
      • 2014-08-04
      • 1970-01-01
      • 1970-01-01
      • 2012-01-10
      • 1970-01-01
      • 2011-10-16
      相关资源
      最近更新 更多