2

NSLayoutManager に異なる属性を持つ範囲でグリフを描画させようとしていますが、それでも NSTextStorage オブジェクトに設定された属性を使用します。

別のフォントから NSGlyph を作成し、それを NSTypesetter のグリフ ストレージにあるものに置き換えようとしていました。しかし、それは役に立ちます。レイアウト マネージャーは、テキスト ストレージ属性文字列で指定されたフォントと色でグリフを描画します。

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)
}

ある範囲でこれらの属性を無視し、必要なフォントと色で作成したグリフを描画するようにレイアウト マネージャーに強制するにはどうすればよいですか? 目に見えない文字を描画する最も効率的な方法に取り組んでいるため、これが必要です。

4

1 に答える 1