【问题标题】:CTFontGetGlyphsForCharacters Returns false when passing emojiCTFontGetGlyphsForCharacters 传递表情符号时返回 false
【发布时间】:2021-09-22 23:48:25
【问题描述】:

在一个符合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 有关,但我在这里有点超出我的深度。一点帮助?

【问题讨论】:

    标签: swift unicode utf-8 textview textkit


    【解决方案1】:

    您使用的字体没有该特定字符的字形。

    当您尝试查看的特定字体没有字形但其他字体可能时,系统会维护一个“字体后备”列表。

    后备列表由 CTFontCopyDefaultCascadeListForLanguages 提供,但由于您正处于被要求提供特定字体的字形的位置,因此似乎应该在链中更高的位置处理后备生成。

    您可能应该返回 0 以指示布局管理器应该使用它的默认行为。

    【讨论】:

      猜你喜欢
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-10
      • 2016-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多