Objective c 强制NSLayoutManager使用其他字体绘制图示符

Objective c 强制NSLayoutManager使用其他字体绘制图示符,objective-c,swift,cocoa,cocoa-touch,nslayoutmanager,Objective C,Swift,Cocoa,Cocoa Touch,Nslayoutmanager,我试图强制NSLayoutManager在某个范围内绘制具有不同属性的图示符,但它仍然使用NSTextStorage对象中设置的属性 我试图从不同的字体创建NSGlyph,并用NSTypesetter的glyph存储中的字体替换它。但它是useles-layout manager仍然使用文本存储属性字符串中指定的字体和颜色绘制该字形 public override func drawGlyphs(forGlyphRange glyphsToShow: NSRange, at origin: NS

我试图强制NSLayoutManager在某个范围内绘制具有不同属性的图示符,但它仍然使用NSTextStorage对象中设置的属性

我试图从不同的字体创建NSGlyph,并用NSTypesetter的glyph存储中的字体替换它。但它是useles-layout manager仍然使用文本存储属性字符串中指定的字体和颜色绘制该字形

public override func drawGlyphs(forGlyphRange glyphsToShow: NSRange, at origin: NSPoint) {
    // now set glyphs for invisible characters
    if PreferencesManager.shared.shouldShowInvisibles == true {
        var spaceRanges = [NSRange]()
        let charRange = self.characterRange(forGlyphRange: glyphsToShow, actualGlyphRange: nil)
        var substring = (self.currentTextStorage.string as NSString).substring(with: charRange)

        let spacesExpression = try? NSRegularExpression(pattern: "[ ]", options: NSRegularExpression.Options.useUnicodeWordBoundaries)
        let substringRange = NSRange(location: 0, length: substring.characters.count)
        if let matches = spacesExpression?.matches(in: substring, options: .withoutAnchoringBounds, range: substringRange) {
            for match in matches {
                spaceRanges.append(NSRange(location: charRange.location + match.range.location, length: 1))
            }
        }

        for spaceRange in spaceRanges {
            let invisibleFont = Font(name: "Gill Sans", size: 11) ?? Font.systemFont(ofSize: 11)

            // get any glyph to test the approach
            var glyph = invisibleFont.glyph(withName: "paragraph")

            // replce the glyphs
            self.typesetter.substituteGlyphs(in: spaceRange, withGlyphs: &glyph)

        }
    }
    // BUT LAYOUT MANAGER IGNORES THE FONT OF THE GLYPH I CREATED
    super.drawGlyphs(forGlyphRange: glyphsToShow, at: origin)
}

如何强制布局管理器忽略某些范围内的属性,并使用所需的字体和颜色绘制创建的图示符?我需要这个,因为我正在研究绘制不可见字符的最有效方法。

您在这里直接修改排字机,但当您调用
super
时,它将重新完成所有工作。您可能会替代
getGlyphs(in:glyphs:properties:characterIndexes:bidiLevels:)
,调用
super
,然后交换所需的任何glyphs。或者,在调用
super
之前,您可以在此处调用
setGlyphs(…)


有关使用不推荐的方法尝试执行的操作的示例,请参见。我相当肯定
replaceGlyphatinex
会被
setGlyphs

所取代,我熟悉这种方法,我知道它是有效的。但正如我所说,我需要布局管理器忽略隐藏字符所在区域的属性。若我只是在类型设置器布局管理器中设置了一些标志符号,那个么它仍然会使用文本存储中设置的属性来绘制。但是我需要用我想要的字体和颜色来画看不见的字符。修改文本存储不是一个选项,因为它将更改绘制文本的几何图形。