在iOS中如何获取表情符号路径

4

我的目的:绘制每个字形的轮廓。

示例1:

输入:text = "666棒"

显示:
enter image description here

附加说明:上图中,1 是显示视图,2 是输入视图。

示例2:

输入:text = "666棒"

显示:
enter image description here

附加说明:上图中,1 是显示视图,2 是输入视图,3 没有渲染。

主要思路:

  1. 使用 CoreText 获取每个 CGglyph。
  2. 获取每个字形的 CGPath。
  3. 使用 CAShapeLayer 在屏幕上显示字形。

主要方法:

    let letters     = CGMutablePath()
    let font        = CTFontCreateWithName(fontName as CFString?, fontSize, nil)
    let attrString  = NSAttributedString(string: text, attributes: [kCTFontAttributeName as String : font])
    let line        = CTLineCreateWithAttributedString(attrString)
    let runArray    = CTLineGetGlyphRuns(line)

    for runIndex in 0..<CFArrayGetCount(runArray) {

        let run     : CTRun =  unsafeBitCast(CFArrayGetValueAtIndex(runArray, runIndex), to: CTRun.self)
        let dictRef : CFDictionary = unsafeBitCast(CTRunGetAttributes(run), to: CFDictionary.self)
        let dict    : NSDictionary = dictRef as NSDictionary
        let runFont = dict[kCTFontAttributeName as String] as! CTFont

        for runGlyphIndex in 0..<CTRunGetGlyphCount(run) {
            let thisGlyphRange  = CFRangeMake(runGlyphIndex, 1)
            var glyph           = CGGlyph()
            var position        = CGPoint.zero
            CTRunGetGlyphs(run, thisGlyphRange, &glyph)
            CTRunGetPositions(run, thisGlyphRange, &position)

            let letter          = CTFontCreatePathForGlyph(runFont, glyph, nil)
            let t               = CGAffineTransform(translationX: position.x, y: position.y)
            if let letter = letter  {
                letters.addPath(letter, transform: t)
            }
        }
    }
    let path = UIBezierPath()
    path.move(to: CGPoint.zero)
    path.append(UIBezierPath(cgPath: letters))

    let pathLayer               = CAShapeLayer()
    pathLayer.path              = path.cgPath
    self.layer.addSubLayer(pathLayer)
   ...

问题:

如何获取emoji路径,在这种情况下,我可以绘制emoji轮廓而不是绘制整个emoji?另一个好处是如果需要,我可以绘制动画的emoji路径。

非常感谢任何帮助!

************************ 更新 2017.2.15 ***********************

感谢@KrishnaCA的建议。

我使用bool supports = CTFontGetGlyphWithName(myFont, "")发现没有字体支持emoji。

幸运的是Google的Noto为emoji字体提供了良好的支持。

您可以在此处找到它:Google的Noto

我使用的字体是Noto Emoji

显示:

输入图像描述

只有Noto EmojiNoto Color Emoji支持Emoji(我猜)

希望能帮助来到这里的人们!

3个回答

1
我认为您需要检查对应于CTFont的Unicode字符是否存在字形。如果不存在,则回退到具有Unicode字符字形的任何默认CTFont。
您可以使用以下代码进行检查。
bool supports = CTFontGetGlyphWithName(myFont, "")

在这里,myFont是一个CTFontRef对象。

如果这不是您正在寻找的,请告诉我。


因为时差的原因向大家道歉。我已经更新了我的问题,谢谢。 - Oo_oO

0

我知道CATextLayers可以显示文本,但是CATextLayer会显示整个表情符号而不仅仅是表情符号的轮廓。 - Oo_oO

0

我知道有点晚了,但不幸的是你不能这样做 - 表情符号实际上是绘制在表示常规字符的形状相同的上下文中的位图。你最好的选择可能是按需要的比例单独绘制表情符号字符到上下文中。这样做不会让你访问实际的矢量数据。

如果你真的需要它以矢量形式:

  • 我会找到苹果表情符号字体的矢量重绘版(我记得在互联网上看到过,不确定是否包含所有最新的表情符号)
  • 将你找到的各个矢量图像的名称映射到字符,然后绘制矢量图像

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接