ios - Swift iOS - 如何将 NSTextAttachment 内容模式设置为 Aspect Fit?

标签 ios swift uiimage nsattributedstring nstextattachment

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

问题是当我在 textAttachment 上设置 PDF 图像的边界时,World 图像会变形。它被拉得又长又宽。

如何在 textAttachment 对象上设置 contentMode 以使用 .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

最佳答案

我遵循了@Maciej Swic的回答

Resize NSTextAttachment Image

由于某种原因,我无法扩展 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)
    }
}

关于ios - Swift iOS - 如何将 NSTextAttachment 内容模式设置为 Aspect Fit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47128448/

相关文章:

气泡效果中的 iOS 图像

ios - Realm 0.102.0 保存 RLMArray

iphone - 将 UIImage/CGImage 渲染到 CGPDFContext 会导致...空白!

ios - 快速销毁特定 View 上的导航栏和标签栏

ios - 创建符合另一个协议(protocol)的协议(protocol)变量

ios - 是否可以让客户从您的 iOS 应用程序中购买 iTunes 的流媒体内容?

ios - 使用核心数据完全不相关的错误

ios - 通过电子邮件发送UIImage

ios - 安装 pod 出现错误 : Unable to find a specification for `GVRSDK`

objective-c - 这行代码是如何工作的? AFNetworking/CFURL复制密码