I want to make an image like this programmatically:
I have the upper image and text with me. Should I write text on the image?
I want to make it a complete .png image(image + label) and set it as the background of the button.
I want to make an image like this programmatically:
I have the upper image and text with me. Should I write text on the image?
I want to make it a complete .png image(image + label) and set it as the background of the button.
Considering performance, you should avoid having frequent calls to -drawRect:. Every UIView is backed with a CALayer and images as layer contents remain in memory as long as the CALayer stays in the hierarchy.This means that most of the operations you see in an application, including movement, rotation, and scaling of the view/layer, do not require a redraw.This means you can use add an CATextLayer on UIImageView , If you don't need a image with watermark. https://developer.apple.com/library/ios/qa/qa1708/_index.html
- (void)addText:(NSString *)text inImageView:(UIImageView *)imageView { CATextLayer *textLayer = [CATextLayer layer]; UIFont *font = [UIFont systemFontOfSize:14.0f]; CGSize textSize = [text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0f]}]; textLayer.frame = CGRectMake((imageView.size.width - textSize.width)/2, (imageView.size.height - textSize.height)/2, textSize.width, textSize.height);; textLayer.string = text; textLayer.fontSize = font.pointSize; [imageView.layer addSublayer:textLayer]; }
I have created fully customized extension for UIImage to draw watermark in Swift:
As you see, I have added some default values for attributes so you can ignore if you don't need to change. Here some examples for how to use it:
Swift 4.0 version:
I did something like this! After browsing and combining some examples.
Puts the text in the midle of the image, resizing the font if needed.
My contribution to the first answer with iOS 7 support :
Hope this helps
EDIT: modified the
UIGraphicsBeginImageContextWithOptions
to handle screen scale. Thk @SoftDesignerIn Swift-3 of @Jano Answer :-
iOS7 only.
Watermark in lower right corner.
Usage: