Search code examples
iosswiftuiimagensattributedstringnstextattachment

Swift iOS -How to Set NSTextAttachment Content Mode to Aspect Fit?


I have a PDF image that I appended to a String. I used NSTextAttachment and NSAttributedString to get it done. I add them to a textView and the result is Hello with an image of the World underneath of it.

The problem is when I set the bounds for the PDF image on the textAttachment the World image is distorted. It's stretched long and wide.

How can I set a contentMode on the textAttachment object to redraw the image correctly using .aspectRatio?

number #4 is where I set the bounds

// #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

Solution

  • I followed @Maciej Swic's answer

    Resize NSTextAttachment Image

    For some reason I couldn't extend the NSTextAttachment class so I added it to the bottom of the class I was using it in. I removed the bounds property that I used in my question and used his function instead. It's on #4, the second line:

    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)
        }
    }