如何在 iOS 中获取表情符号路径
How to get emoji path in iOS
我的目的:画出每个字形的轮廓
示例 1:
输入: text= "666棒"
显示:
附上:上图中,1为displayView,2为inputView。
示例 2:
输入:text= "666棒"
显示:
附:上图中,1为displayView,2为inputView,3为无渲染。
主要思想是:
- 使用CoreText获得每一个CGglyph。
- 获取每个字形的 CGPath
- 使用 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
还好是Google的Noto对emoji字体提供了很好的支持
你可以在那里找到它:google's Noto
我使用了字体 Noto Emoji
显示:
只有 Noto Emoji 和 Noto Color Emoji 支持 Emoji(我猜的)
希望能帮到过来的人!
我认为您需要检查是否存在与CTFont 对应的unicode 字形。如果它不存在,则回退到任何具有 unicode
的 glpyh 的默认 CTFont
您可以使用以下代码进行检查。
bool supports = CTFontGetGlyphWithName(myFont, "")
这里,myFont 是一个 CTFontRef
对象。
如果这不是您要找的,请告诉我
我相信你需要 CATextLayers
来帮助你。
我知道有点晚了,但遗憾的是你不能 - 表情符号实际上是绘制到与表示常规字符的形状相同的上下文中的位图。您最好的选择可能是根据需要的比例将表情符号字符单独绘制到上下文中。这不会让您访问实际的矢量数据。
如果你真的需要它的矢量形式:
- 我会去寻找在矢量中重绘的 Apple Emoji 字体(我记得在互联网上看到它,但不确定它是否包含所有最新的表情符号)
- 将您找到的各个矢量图的名称映射到字符,然后绘制矢量图
我的目的:画出每个字形的轮廓
示例 1:
输入: text= "666棒"
显示:
附上:上图中,1为displayView,2为inputView。
示例 2:
输入:text= "666棒"
显示:
附:上图中,1为displayView,2为inputView,3为无渲染。
主要思想是:
- 使用CoreText获得每一个CGglyph。
- 获取每个字形的 CGPath
- 使用 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
还好是Google的Noto对emoji字体提供了很好的支持
你可以在那里找到它:google's Noto
我使用了字体 Noto Emoji
显示:
只有 Noto Emoji 和 Noto Color Emoji 支持 Emoji(我猜的)
希望能帮到过来的人!
我认为您需要检查是否存在与CTFont 对应的unicode 字形。如果它不存在,则回退到任何具有 unicode
的 glpyh 的默认 CTFont您可以使用以下代码进行检查。
bool supports = CTFontGetGlyphWithName(myFont, "")
这里,myFont 是一个 CTFontRef
对象。
如果这不是您要找的,请告诉我
我相信你需要 CATextLayers
来帮助你。
我知道有点晚了,但遗憾的是你不能 - 表情符号实际上是绘制到与表示常规字符的形状相同的上下文中的位图。您最好的选择可能是根据需要的比例将表情符号字符单独绘制到上下文中。这不会让您访问实际的矢量数据。
如果你真的需要它的矢量形式:
- 我会去寻找在矢量中重绘的 Apple Emoji 字体(我记得在互联网上看到它,但不确定它是否包含所有最新的表情符号)
- 将您找到的各个矢量图的名称映射到字符,然后绘制矢量图