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

标签 ios swift uibezierpath core-text

我的目的:画出每个字形的轮廓

例子1:

输入: text= "666棒"

显示:
enter image description here

附上:上图中,1为displayView,2为inputView。

例子2:

输入:text= "666😁棒"

显示:
enter image description here

附:上图中1为displayView,2为inputView,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)
   ...

问题:

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

感谢任何帮助!

************************ 更新 2.15.2017 ******************* ****

感谢@KrishnaCA 的建议。

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

幸好谷歌的Noto对emoji字体提供了很好的支持

你可以在那里找到它:google's Noto

我使用字体 Noto Emoji

显示:

enter image description here

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

希望对来到这里的人有所帮助!

最佳答案

我相信您需要检查对应于 CTFont 的 unicode 字形是否存在。如果它不存在,则回退到任何具有 unicode glpyh 的默认 CTFont

您可以使用以下代码进行检查。

bool supports = CTFontGetGlyphWithName(myFont, "😀")

这里,myFont 是一个CTFontRef 对象。

如果这不是您要找的,请告诉我

关于ios - 如何在 iOS 中获取表情符号路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42231963/

相关文章:

ios - 具有从右到左文本和自动调整字体大小的多行 UILabel

ios - 详细信息 View Controller 中缺少 UIBarButton

ios - 使用 Safari View Controller 登录时的 Swift iOS Facebook SDK 循环

ios - UIBezierPath 大,渲染速度慢

ios - 如何获得 CAShapeLayer superview 位置?

将 GMSPolygon 或 GMSPolyline 添加到 map 时,iOS Google Map SDK 崩溃

c# - 在 visual studio 中构建错误

iOS9 - 无法验证任何应用程序

swift - 为什么绘制的UIBezierPath 位于右下方,而没有指定CAShapeLayer 的中心位置?

iOS:有没有办法以编程方式以特定字体设置 Storyboard的所有字体?