2

我有一个附加到字符串的 PDF 图像。我使用NSTextAttachmentNSAttributedString完成它。我将它们添加到 textView 中,结果是Hello ,其下方带有World的图像。

问题是当我在 textAttachment 上设置 PDF 图像的边界时,世界图像被扭曲了。它被拉得又长又宽。

如何contentModetextAttachment对象上设置 a 以正确使用重绘图像.aspectRatio

数字 #4 是我设置界限的地方

// #1. Define dict attribute for string
let bold17 = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 17)]

// #2. Create "hello" string and add the dict attribute to it
let helloStr = NSAttributedString(string: "Hello\n\n", attributes: bold17)

// #3. Create NSTextAttachment
let textAttachment = NSTextAttachment()

// #4. Add image to the textAttachment then set it's bounds
textAttachment.image = UIImage(named: "world_PDF")
textAttachment.bounds = CGRect(x: 0, y: 0, width: 200, height: 200)

// #5. Set image as NSAttributedString
let worldImage = NSAttributedString(attachment: textAttachment)

// #6. Create NSMutableString to 
let mutableAttributedString = NSMutableAttributedString()

// #7. Append the "hello" string and the "world" image to each other using the mutableAttributedString object
mutableAttributedString.append(helloStr)
mutableAttributedString.append(worldImage)

// #8. Set the mutableAttributedString to the textView then center it
textView.attributedText = mutableAttributedString
textView.textAlignment = .center
4

1 回答 1

2

我跟着@Maciej Swic 的回答

调整 NSTextAttachment 图像的大小

出于某种原因,我无法扩展 NSTextAttachment 类,所以我将它添加到我正在使用它的类的底部。我删除了我在问题中使用的bounds属性并改用了他的函数。它在#4,第二行:

class MyController: UIViewController{

    override func viewDidLoad() {
            super.viewDidLoad()

       // #1. Define dict attribute for string
       let bold17 = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 17)]

       // #2. Create "hello" string and add the dict attribute to it
       let helloStr = NSAttributedString(string: "Hello\n\n", attributes: bold17)

       // #3. Create NSTextAttachment
       let textAttachment = NSTextAttachment()

       // #4. Add image to the textAttachment then set it's bounds
       textAttachment.image = UIImage(named: "world_PDF")
       textAttachment.setImageHeight(height: 200) // <----HIS ANSWER HERE

       // #5. Set image as NSAttributedString
       let worldImage = NSAttributedString(attachment: textAttachment)

       // #6. Create NSMutableString to 
       let mutableAttributedString = NSMutableAttributedString()

       // #7. Append the "hello" string and the "world" image to each other using the mutableAttributedString object
       mutableAttributedString.append(helloStr)
       mutableAttributedString.append(worldImage)

       // #8. Set the mutableAttributedString to the textView then center it
       textView.attributedText = mutableAttributedString
       textView.textAlignment = .center

    }
}

extension NSTextAttachment {
    func setImageHeight(height: CGFloat) {
        guard let image = image else { return }
        let ratio = image.size.width / image.size.height

        bounds = CGRect(x: bounds.origin.x, y: bounds.origin.y, width: ratio * height, height: height)
    }
}
于 2017-11-06T02:21:07.550 回答