Search code examples
swiftnstextattachment

How can I center an image on a textView who is attached to a NSTextAttachment type?


I have a textView (programmatically) and I inserted an image appended to a NSTextAttachment and want it to be aligned to the center of the text view... but I haven't found any solutions yet...I was wondering if that task is possible to be done via code only...

    let image = ResizeImage(image: UIImage(named: "logo")!, targetSize: size)

    let image1Attachment = NSTextAttachment()

    image1Attachment.image = image

    let image1Attachments = NSAttributedString(attachment: image1Attachment)

My best approach to the solution on trying to achieve this was this code :

    let image = ResizeImage(image: UIImage(named: "telepaint1")!, 
    targetSize: size)

//let textAttachment = NSTextAttachment()
    let textAttachment = NSTextAttachment()
//let imageStyle = NSMutableParagraphStyle()
    let imageStyle = NSMutableParagraphStyle()
    //imageStyle.alignment = .center
    imageStyle.alignment = .center
    //textAttachment.image = image
    textAttachment.image = image
    let imageText = NSAttributedString(attachment: 
    textAttachment).mutableCopy() as! NSMutableAttributedString
    //attempt to center image...
    let attributedStringToAppend: NSMutableAttributedString = 
    NSMutableAttributedString(attributedString: 
    NSAttributedString(string: "\n\n"))

    //attributedStringToAppend.addAttributes(attr, range: range)

    let combination2: NSMutableAttributedString = 
    NSMutableAttributedString(attributedString: 
    NSAttributedString(string: "\n\n"))


    combination2.append(lineBreak)
    // combination2.addAttribute(attr, range: NSRange(location: 0, 
    length: length))

    combination2.append(imageText)
    //this text will display the "Walktrough" text on image 

    combination2.append(attributedText)

    tv.attributedText = combination2

Solution

  • enter image description hereAnswer(worked for me!):

        let paragraphStyle = NSMutableParagraphStyle()
    
        paragraphStyle.alignment = .center
    
        attributedText.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: length))
    
        let size = CGSize(width: 100, height: 100)
    
    
        //Here is an attempt to resize image and center it...
    
    
    
        let image = ResizeImage(image: UIImage(named: "logo")!.withRenderingMode(.alwaysTemplate), targetSize: size)
    
    
        let textAttachment = NSTextAttachment()
    
    
        let imageStyle = NSMutableParagraphStyle()
    
    
        imageStyle.alignment = .center
    
    
        textAttachment.image = image
    
    
        let imageText = NSAttributedString(attachment: textAttachment).mutableCopy() as! NSMutableAttributedString
    
        let length2 = imageText.length
    
        imageText.addAttribute(NSAttributedString.Key.paragraphStyle, value: imageStyle, range: NSRange(location: 0, length: length2))