Swift 中的 drawGlyphsForGlyphRange(显示不可见字符)。 replaceGlyphAtIndex 已弃用

drawGlyphsForGlyphRange in Swift (Displaying Invisible characters). replaceGlyphAtIndex deprecated

我正在尝试使用下面的 class & func 来显示隐形字符,并且它工作正常。

但 replaceGlyphAtIndex 在 OS X 10.11 中已弃用,我们需要使用 setGlyhs()

setGlyphs(<#T##glyphs: UnsafePointer<CGGlyph>##UnsafePointer<CGGlyph>#>, properties: <#T##UnsafePointer<NSGlyphProperty>#>, characterIndexes: <#T##UnsafePointer<Int>#>, font: <#T##NSFont#>, forGlyphRange: <#T##NSRange#>)

但是我在如何将 replaceGlyphAtIndex 转换为 setGlyphs 方面遇到困难。 谁能建议我如何将下面的 replaceGlyphAtIndex 转换为 setGlyphs()?

我试过如下但它崩溃了。

let data = String(g).dataUsingEncoding(NSUTF8StringEncoding)
let cgG = UnsafePointer<CGGlyph>(data!.bytes)
self.setGlyphs(cgG, properties: nil, characterIndexes: UnsafePointer<Int>(bitPattern: characterIndex), font: font as! NSFont, forGlyphRange: NSMakeRange(glyphIndex, 1))

任何人都可以让我知道上面的代码行出了什么问题吗?

class MyLayoutManager: NSLayoutManager {

override func drawGlyphsForGlyphRange(glyphsToShow: NSRange, atPoint origin: NSPoint) {
    if let storage = self.textStorage {
        let s = storage.string
        let startIndex = s.startIndex

        for glyphIndex in glyphsToShow.location ..< glyphsToShow.location + glyphsToShow.length {
            let characterIndex = self.characterIndexForGlyphAtIndex(glyphIndex)
            let ch = s[startIndex.advancedBy(characterIndex)]
            switch ch {
            case " ": //Blank Space
                let attrs = storage.attributesAtIndex(characterIndex, effectiveRange: nil)
                if let font = attrs[NSFontAttributeName] {
                    let g = font.glyphWithName("period")//("periodcentered")
                    self.replaceGlyphAtIndex(glyphIndex, withGlyph: g)


                    //                        let data = String(g).dataUsingEncoding(NSUTF8StringEncoding)
                    //                        let cgG = UnsafePointer<CGGlyph>(data!.bytes)
                    //
                    //                        self.setGlyphs(cgG, properties: nil, characterIndexes: UnsafePointer<Int>(bitPattern: characterIndex), font: font as! NSFont, forGlyphRange: NSMakeRange(glyphIndex, 1))
                }
            case "\n": //New Line
                let attrs = storage.attributesAtIndex(characterIndex, effectiveRange: nil)
                if let font = attrs[NSFontAttributeName] {
                    let g = font.glyphWithName("logicalnot")
                    self.replaceGlyphAtIndex(glyphIndex, withGlyph: g)
                }
            case newLineUniCodeStr:
                let attrs = storage.attributesAtIndex(characterIndex, effectiveRange: nil)
                if let font = attrs[NSFontAttributeName] {
                    let g = font.glyphWithName("logicalnot")
                    self.replaceGlyphAtIndex(glyphIndex, withGlyph: g)
                }
            default:
                break
            }
        }
    }
    super.drawGlyphsForGlyphRange(glyphsToShow, atPoint: origin)
}

}

我找到了另一种显示字形的方法。 (在下面发布我的代码,因为它可能对其他人有用)

override func drawGlyphsForGlyphRange(glyphsToShow: NSRange, atPoint origin: NSPoint) {
    if let storage = self.textStorage {
        let s = storage.string
        let startIndex = s.startIndex

        var padding:CGFloat = 0
        for glyphIndex in glyphsToShow.location ..< glyphsToShow.location + glyphsToShow.length {
            let characterIndex = self.characterIndexForGlyphAtIndex(glyphIndex)
            if characterIndex < s.characters.count
            {
                var glyphStr = ""
                let ch = s[startIndex.advancedBy(characterIndex)]
                switch ch {
                case " ": //Blank Space
                    glyphStr = periodCenteredUniCodeStr
                case "\n": //New Line
                    glyphStr = lineBreakUniCodeStr
                case newLineUniCodeStr:
                    glyphStr = lineBreakUniCodeStr
                    padding += 5
                default:
                    break
                }
                var glyphPoint = self.locationForGlyphAtIndex(glyphIndex)
                let glyphRect = self.lineFragmentRectForGlyphAtIndex(glyphIndex, effectiveRange: nil)
                if glyphStr.characters.count > 0{
                    glyphPoint.x = glyphPoint.x + glyphRect.origin.x
                    glyphPoint.y = glyphRect.origin.y
                    NSString(string: glyphStr).drawInRect(NSMakeRect(glyphPoint.x, glyphPoint.y, 10, 10), withAttributes: nil)
                }
            }else{
                print("Wrong count here")
            }
        }

    }
    super.drawGlyphsForGlyphRange(glyphsToShow, atPoint: origin)
}