1

I have two of the StoryBoards containing TextFields and two Buttons.

The second StoryBoard has an ImageView received from the first StoryBoard.

I want to make the text in the TextField on the ImageView in the second StoryBoard and then convert it to a PDF file.

I found this code on the site and tried to use it, but I really did not know how to use it and I could not do that. Can someone help me?

import UIKit
import PDFKit

class ViewController: UIViewController {

    @IBOutlet weak var textview: UITextField!
    @IBOutlet weak var imageview1: UIImageView!
    let PdfView = pdfViewController()
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    // Button To send Data To Another view
    @IBAction func aa(_ sender: Any)
    {
        let label = UILabel(frame: CGRect(x: 20, y: 20, width: 80, height: 30))
        label.text = "Hello ...!"
        label.font = UIFont.systemFont(ofSize: 20)
        label.textColor = .black
        view.addSubview(label)

        sample1(label: label)
        //sample2(label: label)
        //sample3(label: label)
        //sample4(label: label)

        PdfView.imageview.clipsToBounds = true
        view.addSubview(PdfView.imageview)
    }

    func sample1(label: UILabel) {
        PdfView.imageview.contentMode = .scaleAspectFit
        PdfView.imageview.image = UIImage(named: "AR1-1")?.with(view: label) { (parentSize, viewToAdd) in
            print("parentSize: \(parentSize)")
            viewToAdd.font = UIFont.systemFont(ofSize: 40)
            viewToAdd.textColor = .yellow
            viewToAdd.bounds = CGRect(x: 40, y: 40, width: 200, height: 40)
        }
    }

    // Button To Move To Another View
    @IBAction func ww(_ sender: Any)
    {

    }

}


extension UIView {

    func copyObject<T: UIView> () -> T? {
        let archivedData = NSKeyedArchiver.archivedData(withRootObject: self)
        return NSKeyedUnarchiver.unarchiveObject(with: archivedData) as? T
    }
}
extension UIImage {

    typealias EditSubviewClosure<T: UIView> = (_ parentSize: CGSize, _ viewToAdd: T)->()

    func with<T: UIView>(view: T, editSubviewClosure: EditSubviewClosure<T>) -> UIImage {

        if let copiedView = view.copyObject() as? T {
            UIGraphicsBeginImageContext(size)

            let basicSize = CGRect(origin: .zero, size: size)
            draw(in: basicSize)
            editSubviewClosure(size, copiedView)
            copiedView.draw(basicSize)

            let newImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return newImage!
        }
        return self

    }
}

extension UIImageView {

    enum ImageAddingMode {
        case changeOriginalImage
        case addSubview
        case addCopiedSubview
    }

    func drawOnCurrentImage<T: UIView>(view: T, mode: ImageAddingMode, editSubviewClosure: @escaping UIImage.EditSubviewClosure<T>) {

        guard let image = image else {
            return
        }

        let addSubView: (T) -> () = { view in
            editSubviewClosure(self.frame.size, view)
            self.addSubview(view)
        }

        switch mode {
        case .changeOriginalImage:
            self.image = image.with(view: view, editSubviewClosure: editSubviewClosure)

        case .addSubview:
            addSubView(view)

        case .addCopiedSubview:
            if let copiedView = view.copyObject() as? T {
                addSubView(copiedView)
            }
        }
    }
}

/

import UIKit
import PDFKit


class pdfViewController: UIViewController {

    @IBOutlet weak var imageview: UIImageView!
    let me = ViewController()
    override func viewDidLoad() {
        super.viewDidLoad()


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
    @IBAction func sendbutton(_ sender: Any)
    {

    }

}
Zee
  • 1,865
  • 21
  • 42
  • Hi, I currently don't get what your problem is, you want to render text from a textfield, into an UIImage, and put that image into an UIImageView? – Dennis Weidmann Mar 24 '18 at 17:01
  • @DennisWeidmannWhat I want is: I have in the first `StoryBored` the `Textfield` and in the second `StoryBored` I have the `UIImage` I want to move the text inside the `Txtfield` on the picture in the second `StoryBored ` – Some Programmer Mar 25 '18 at 02:46
  • I think you should separate this issue in two steps. Maybe you should first take care of how to manage to send the text from the UITextField to a UILabel in the second view controller, as explained in this thread: https://stackoverflow.com/questions/32234811/how-do-i-access-text-in-uitextfield-from-another-viewcontroller-swift or this one https://stackoverflow.com/questions/47241519/how-to-pass-two-textfield-entries-to-another-viewcontroller-with-using-segue). Once achieved that, you can go to the step of converting the received image in a pdf file. – celiux Mar 25 '18 at 09:10

0 Answers0