iOS Swift insert Image and Text in UITextView example

iOS Swift insert Image and Text in UITextView example

UITextView supports the display of text using custom style information and also supports text editing. You typically use a text view to display multiple lines of text, such as when displaying the body of a large text document.

This class supports multiple text styles through use of the attributedText property. In this case we will use NSTextAttachment to attach an image to the attributedText. You can use the image instance property to link an Image representing the text attachment contents.

import UIKit

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "Main View"
        
        addUITextView()
       
    }
    
    func addUITextView(){
        
        //lauout for the View
        let myTextView = UITextView()
        myTextView.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(myTextView)
        
        let views = [
            "view" : view,
            "textView" : myTextView
            ]
        
        var allConstraints: [NSLayoutConstraint] = []
        allConstraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|-[textView]-|",
                                                         options: [], metrics: nil, views: views as [String : Any])
        allConstraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|-[textView]-|",
                                                         options: [], metrics: nil, views: views as [String : Any])
        NSLayoutConstraint.activate(allConstraints)
        
        // start with our text data
        let font = UIFont.systemFont(ofSize: 26)
        let attributes: [NSAttributedString.Key: Any] = [
            .font: font,
            .foregroundColor: UIColor.orange
            ]
        let myString = NSMutableAttributedString(string: "Text at the beginning\n",
                                                 attributes: attributes)
        
        // A text attachment object contains either an NSData object or an FileWrapper object,
        // which in turn holds the contents of the attached file.
        let imageAttachment = NSTextAttachment()
        imageAttachment.image = UIImage(named: "apple.jpg")!
        let imageSize = imageAttachment.image!.size.width;
        
        // calculate how much to resize our image
        // here we are doing it base on the space available in the view
        var frameSize = self.view.frame.size.width - 100;
        let topBarHeight = UIApplication.shared.statusBarFrame.size.height +
            (self.navigationController?.navigationBar.frame.height ?? 0.0)
        let height = self.view.frame.size.height - topBarHeight - 100;
        if(height < frameSize) {
            frameSize = height;
        }
        let scaleFactor = imageSize / frameSize;
        
        // scale the image down
        imageAttachment.image = UIImage(cgImage: imageAttachment.image!.cgImage!, scale: scaleFactor, orientation: .up)
        
        // create attributed string from image so we can append it
        let imageString = NSAttributedString(attachment: imageAttachment)
        
        // add the NSTextAttachment wrapper to our original string, then add some more text.
        myString.append(imageString)
        myString.append(NSAttributedString(string: "\nTHE END!!!", attributes: attributes))
        
        // set the text for the UITextView
        myTextView.attributedText = myString;
        
       
    }
    
    
    
}


No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.