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에서도 실행되지만 실제로 이미지의 크기는 조정되지 않습니다. 아래에서 볼 수 있듯이 이미지 주위에 올바른 크기의 상자가 있지만 실제 이미지가 상자를 오버플로합니다.
은 또한 다음과 같은 가이드를 따랐다 : Implementing Rich Text with Images on OS X and iOS. 이렇게하면 코드가 하위 클래스로 이동하지만 동일한 효과가 있습니다.제안 사항? 내가 조정해야하는 NSTextAttachment.bounds
외에 뭔가가 있습니까?
UPDATE
내가 발견 NSImage
작품의 size
구성 요소를 수정! 그러나 이제는 모든 이미지가 거꾸로 표시되지만 올바른 크기로 표시됩니다. :(
사용자가 이미지 크기를 조정할 수 있도록 허용하려면 https://github.com/josephessin/ResizableTextAttachment와 같은 라이브러리를 만들어야합니다. –