2016-09-08 1 views
2

나는 NSTextView를 사용하고 이미지 NSTextAttachment를 삽입합니다. 내가 선택하고 그것을 복사 한 다음 붙여 넣을 때 textview에 붙여 넣은 이미지가 표시되지만 NSTextView.attributedString에서 내용 문자열을 가져올 수 없습니다. 왜 ?NSTextView 복사 붙여 넣기 NSTextAttachment Mac OS에서

+0

안녕하세요, 아직 해결책을 찾았습니까? – Daniel

+1

사용자 지정 형식을 사용하여이 문제를 해결하려면 다음을 수행하십시오. NSPasteboard.general(). addTypes ([CustomParsteFormat.Emotion], 소유자 : nil) (영문 일 수 있음) NSPasteboard.general() .setString (newContent, forType : CustomParsteFormat.Emotion) – user1461382

답변

1

비슷한 문제가있었습니다. 정확히 원하는지 확실하지 않지만 NSTextAttachment의 문자열 표현을 복사하여 붙여 넣기를 원합니다. 나는 또한 문자열 표현의 기억 사용자 정의 NSTextAttachmentCell 클래스를 사용하고

override func writeSelection(to pboard: NSPasteboard, type: String) -> Bool { 
    let selectedAttributedString = attributedString().attributedSubstring(from: selectedRange()) 
    let selectedAttributedStringCopy = NSMutableAttributedString(attributedString: selectedAttributedString) 

    selectedAttributedStringCopy.enumerateAttribute(NSAttachmentAttributeName, in: NSMakeRange(0,(selectedAttributedString.string.characters.count)), options: .reverse, using: {(_ value: Any?, _ range: NSRange, _ stop: UnsafeMutablePointer<ObjCBool>) -> Void in 

     if let textAttachment = value as? NSTextAttachment, let textAttachmentCell = textAttachment.attachmentCell as? YouCustomTextAttachmentCellClass { 
      var range2: NSRange = NSRange(location: 0,length: 0) 
      let attributes = selectedAttributedStringCopy.attributes(at: range.location, effectiveRange: &range2) 

      selectedAttributedStringCopy.replaceCharacters(in: range, with: NSMutableAttributedString(string: textAttachmentCell.yourStringRepresentation)) 
      selectedAttributedStringCopy.addAttributes(attributes, range: range) 

      // selectedAttributedStringCopy.insert(attachmentAttributedString, at: range.location) 

     } 
    }) 

    pboard.clearContents() 
    pboard.writeObjects([selectedAttributedStringCopy]) 

    return true 
} 

참고 :

나는 내 사용자 정의를 NSTextView 클래스에서 func writeSelection(to pboard: NSPasteboard, type: String) -> Bool을 무시 끝났다.