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。 –