Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How to AirPrint Text and Images

I have the code below that prints the contents of a textview but i would like to print the contents of a UIView. How could I do this?

Thanks

        var pic:UIPrintInteractionController = UIPrintInteractionController.sharedPrintController()!
        var viewpf:UIViewPrintFormatter = textView.viewPrintFormatter()

        pic.delegate = self
        pic.showsPageRange = true
        pic.printFormatter = viewpf

You don't need a type declaration when you are assigning a variable.

var pic = UIPrintController.... is sufficient. Swift will derive the type from the assigned value. It is necessary if you are just declaring a variable and not assigning it a value.

1 Answer

UIViewPrintFormatter supports UIWebView, UITextView, and MKMapView. This is not available for any generic UIView. So if you can get your UIView to render properly inside of a UIWebView then you can use the proper values there.

Depending on the content of your view, you may be able to just duplicate it in HTML/CSS files locally then use those as the source for the webView and then you'd be able to use webView's printFormatting just as you do above of the textView.

Also UIPrintInteractionController.canPrintData() could be useful for testing this.