代码之家  ›  专栏  ›  技术社区  ›  Lance Samaria

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

  •  2
  • Lance Samaria  · 技术社区  · 7 年前

    我有一个附加到字符串的PDF图像。我用过 NSTextAttachment NSAttributedString 完成它。我将它们添加到textView,结果是 你好 带有 世界 在它下面。

    问题是当我在文本附件上设置PDF图像的边界时 世界 图像失真。它伸展得又长又宽。

    我如何设置 contentMode textAttachment 对象正确重画图像 .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
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Lance Samaria    7 年前

    我听从了@Maciej Swic的回答

    Resize NSTextAttachment Image

    由于某种原因,我无法扩展NSTextAttachment类,所以我将其添加到了使用它的类的底部。我删除了 界限 我在问题中使用的属性,并使用了他的函数。在第二行#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)
        }
    }
    
    推荐文章