2016-09-14 9 views
0

아래의 클래스 & func을 사용하여 보이지 않는 문자를 표시하려고하는데 잘 동작합니다.Swift의 drawGlyphsForGlyphRange (보이지 않는 문자 표시). replaceGlyphAtIndex가 더 이상 사용되지 않습니다

그러나 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#>) 

하지만 setGlyphs에 replaceGlyphAtIndex을 변환하는 방법에 어려움을 겪고 있어요. 아래에서 GlyphAtIndex를 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) 
} 

}

답변

0

나는 문양을 표시하는 또 다른 방법을 찾아 냈다.

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

불행하게도,이 방법은 너무 느립니다 (그것은 다른 사람에게 도움이 될 수 있으므로 아래에있는 내 코드를 게시) 및 @VitaliyVashchenko가 네, 최근에 그 눈치 않았다 멀티 페이지 레이아웃 –

+0

작동하지 않습니다. 더 좋은 아이디어는 알고 있습니까? 그래서 그는 저와 다른 사람들에게 도움이 될 것입니다. –

+0

네, 매우 효율적입니다. 솔루션을 찾았을 때 내 질문 주제에 코드를 게시했습니다. http://stackoverflow.com/questions/39545718/nslayoutmanager-hides-new-line-characters-no-matter-what-i-do –