我有NSAttributedString
嵌入图像的对象。这些在NSTextView
s 中呈现。在 iOS 中,我能够调整 的边界NSTextAttachment
,这使得图像适合。
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 上运行,但它实际上并没有调整图像的大小。下面你可以看到,图像周围有一个大小正确的框,但实际图像溢出了框。
我还遵循了以下指南:在 OS X 和 iOS 上实现带有图像的富文本。这会将代码移动到子类,但效果相同。
有什么建议么?NSTextAttachment.bounds
除了我应该调整的东西之外,还有什么?
更新
我发现修改size
组件NSImage
有效!但是,它现在显示我所有的图像,但尺寸正确。:(