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 ?
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.
UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .
A view that displays one or more lines of informational text.
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.
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With