我的目的:画出每个字形的轮廓
例子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。
幸好谷歌的Noto对emoji字体提供了很好的支持
你可以在那里找到它:google's Noto
我使用字体 Noto Emoji
显示:
只有 Noto Emoji 和 Noto Color Emoji 支持 Emoji(我猜)
希望对来到这里的人有所帮助!
最佳答案
我相信您需要检查对应于 CTFont 的 unicode 字形是否存在。如果它不存在,则回退到任何具有 unicode glpyh 的默认 CTFont
您可以使用以下代码进行检查。
bool supports = CTFontGetGlyphWithName(myFont, "😀")
这里,myFont 是一个CTFontRef
对象。
如果这不是您要找的,请告诉我
关于ios - 如何在 iOS 中获取表情符号路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42231963/