2016-08-13 3 views
2

에 맞게 조정합니다. NSAttributedString 개의 이미지가 포함 된 개체가 있습니다. 이것들은 NSTextView에 있습니다. iOS에서는 NSTextAttachment의 경계를 조정할 수 있었고 이로 인해 이미지가 잘 맞습니다.NSTextView의 이미지 크기를

extension NSTextAttachment { 
    func setImageWidth(width: CGFloat, range: NSRange) { 
     var thisImage = image 
     if thisImage == nil { 
      thisImage = imageForBounds(bounds, textContainer: nil, characterIndex: range.location) 
     } 
     if thisImage != nil { 
      let ratio = thisImage!.size.height/thisImage!.size.width 
      bounds = CGRectMake(bounds.origin.x, bounds.origin.y, width, ratio * width) 
      print("New Bounds: \(bounds)") 
     } 
    } 
} 

이 코드는 OSX에서도 실행되지만 실제로 이미지의 크기는 조정되지 않습니다. 아래에서 볼 수 있듯이 이미지 주위에 올바른 크기의 상자가 있지만 실제 이미지가 상자를 오버플로합니다.

enter image description here

은 또한 다음과 같은 가이드를 따랐다 : Implementing Rich Text with Images on OS X and iOS. 이렇게하면 코드가 하위 클래스로 이동하지만 동일한 효과가 있습니다.

제안 사항? 내가 조정해야하는 NSTextAttachment.bounds 외에 뭔가가 있습니까?

UPDATE

내가 발견 NSImage 작품의 size 구성 요소를 수정! 그러나 이제는 모든 이미지가 거꾸로 표시되지만 올바른 크기로 표시됩니다. :(

+0

사용자가 이미지 크기를 조정할 수 있도록 허용하려면 https://github.com/josephessin/ResizableTextAttachment와 같은 라이브러리를 만들어야합니다. –

답변

0

가 해결!

extension NSImage { 
    func resizeToFit(containerWidth: CGFloat) { 
     var scaleFactor : CGFloat = 1.0 
     let currentWidth = self.size.width 
     let currentHeight = self.size.height 
     if currentWidth > containerWidth { 
      scaleFactor = (containerWidth * 0.9)/currentWidth 
     } 
     let newWidth = currentWidth * scaleFactor 
     let newHeight = currentHeight * scaleFactor 

     self.size = NSSize(width: newWidth, height: newHeight) 
     print("Size: \(size)") 
    } 
} 

I 업데이트에서 언급 한 바와 같이, 당신은 NSImage.size을 변경해야합니다. 플립 내가 문제의 링크에서 거기에 남아 있던 서브 클래스 중 하나에서오고 있었다.

관련 문제