Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an image from UILabel?

I'm currently developing a simple photoshop like application on iphone. When I want to flatten my layers, the labels are at the good position but with a bad font size. Here's my code to flatten :

UIGraphicsBeginImageContext(CGSizeMake(widthDocument,widthDocument));  for (UILabel *label in arrayLabel) {     [label drawTextInRect:label.frame]; }  UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 

Anybody can help me ?

like image 616
Jonathan Avatar asked Aug 08 '12 14:08

Jonathan


People also ask

How do I upload a picture to UILabel?

From there you can drag a UIImageView and UILabel into the view you just created. Set the image of the UIImageView in the properties inspector as the custom bullet image (which you will have to add to your project by dragging it into the navigation pane) and you can write some text in the label.

What is the difference between a UIImage and a UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .

What is UILabel?

A view that displays one or more lines of informational text.

How do I change my UILabel?

To change the font or the size of a UILabel in a Storyboard or . XIB file, open it in the interface builder. Select the label and then open up the Attribute Inspector (CMD + Option + 5). Select the button on the font box and then you can change your text size or font.


2 Answers

From: pulling an UIImage from a UITextView or UILabel gives white image.

// iOS

- (UIImage *)grabImage {     // Create a "canvas" (image context) to draw in.     UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0.0);  // high res     // Make the CALayer to draw in our "canvas".     [[self layer] renderInContext: UIGraphicsGetCurrentContext()];      // Fetch an UIImage of our "canvas".     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();      // Stop the "canvas" from accepting any input.     UIGraphicsEndImageContext();      // Return the image.     return image; } 

// Swift extension w/ usage. credit @Heberti Almeida in below comments

extension UIImage {     class func imageWithLabel(label: UILabel) -> UIImage {         UIGraphicsBeginImageContextWithOptions(label.bounds.size, false, 0.0)         label.layer.renderInContext(UIGraphicsGetCurrentContext()!)         let img = UIGraphicsGetImageFromCurrentImageContext()         UIGraphicsEndImageContext()         return img     } } 

The usage:

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 30, height: 22)) label.text = bulletString let image = UIImage.imageWithLabel(label) 
like image 181
DJPlayer Avatar answered Sep 20 '22 21:09

DJPlayer


I have created a Swift extension to render the UILabel into a UIImage:

extension UIImage {     class func imageWithLabel(label: UILabel) -> UIImage {         UIGraphicsBeginImageContextWithOptions(label.bounds.size, false, 0.0)         label.layer.renderInContext(UIGraphicsGetCurrentContext()!)         let img = UIGraphicsGetImageFromCurrentImageContext()         UIGraphicsEndImageContext()         return img     } } 

EDIT: Improved Swift 4 version

extension UIImage {     class func imageWithLabel(_ label: UILabel) -> UIImage {         UIGraphicsBeginImageContextWithOptions(label.bounds.size, false, 0)         defer { UIGraphicsEndImageContext() }         label.layer.render(in: UIGraphicsGetCurrentContext()!)         return UIGraphicsGetImageFromCurrentImageContext() ?? UIImage()     } } 

The usage is simple:

let label = UILabel(frame: CGRect(x: 0, y: 0, width: 30, height: 22)) label.text = bulletString let image = UIImage.imageWithLabel(label) 
like image 29
Heberti Almeida Avatar answered Sep 18 '22 21:09

Heberti Almeida