swift - CTFontGetGlyphsForCharacters 传递表情符号时返回 false
问题描述
在符合NSLayoutManagerDelegate
我的类中实现此方法:
func layoutManager(_ layoutManager: NSLayoutManager,
shouldGenerateGlyphs glyphs: UnsafePointer<CGGlyph>,
properties props: UnsafePointer<NSLayoutManager.GlyphProperty>,
characterIndexes charIndexes: UnsafePointer<Int>,
font aFont: UIFont,
forGlyphRange glyphRange: NSRange) -> Int {
// First, make sure we'll be able to access the NSTextStorage.
guard let textStorage = layoutManager.textStorage
else { return 0 }
// Get the first and last characters indexes for this glyph range,
// and from that create the characters indexes range.
let firstCharIndex = charIndexes[0]
let lastCharIndex = charIndexes[glyphRange.length - 1]
let charactersRange = NSRange(location: firstCharIndex, length: lastCharIndex - firstCharIndex + 1)
var bulletPointRanges = [NSRange]()
var hiddenRanges = [NSRange]()
let finalGlyphs = UnsafeMutablePointer<CGGlyph>(mutating: glyphs)
// Generate the Middle Dot glyph using aFont.
let middleDot: [UniChar] = [0x00B7] // Middle Dot: U+0x00B7
var myGlyphs: [CGGlyph] = [0]
// Get glyphs for `middleDot` character
guard CTFontGetGlyphsForCharacters(aFont, middleDot, &myGlyphs, middleDot.count) == true
else { fatalError("Failed to get the glyphs for characters \(middleDot).") }
}
问题是CTFontGetGlyphsForCharacters
当我在文本视图中输入表情符号时返回 false。我认为这可能与 UTF-8 与 UTF-16 有关,但我在这里有点超出我的深度。一点帮助?
解决方案
您使用的字体没有该特定字符的字形。
当您尝试查看的特定字体没有字形但另一种字体可能时,系统会维护一个“字体后备”列表。
后备列表由 CTFontCopyDefaultCascadeListForLanguages 给出,但由于您正处于被要求提供特定字体的字形的位置,因此后备生成似乎应该在链中更高的位置处理。
您可能应该返回 0 以指示布局管理器应该使用它的默认行为。
推荐阅读
- python - 有没有办法在 json.loads 方法中获取双引号
- c++ - _Pairib': 不是 'std::map,std::allocator>> 的成员
- artifactory - 如何删除工件中的多个文件
- r - 使用 kable 将字符串拆分为多列
- vb.net - xaml 第 0 行“找不到类型的主窗口”
- html - 如何将虚线边框和边框阴影应用于径向渐变内声明的圆形?
- python - 使用 Python 快速 Ping 计算机列表
- c - 为什么这个 uint16_t 变量声明不起作用?
- c++ - nlohmann 在不知道密钥的情况下解析 json 文件
- java - 提取信息并将其与 CSV 文件进行比较