How do I add text to an image in iOS Swift?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
To add text to an image in Swift, the standard approach is to draw the original image and the text into a new graphics context, then return the rendered result as a UIImage. That gives you a new image with the text permanently baked into the pixels. If you only need a caption on screen, an overlay UILabel is often a better design than modifying the image itself.
Draw Into a New Image
UIGraphicsImageRenderer is the clean UIKit API for this job.
This creates a brand-new UIImage. The original image remains unchanged.
Measure Before Positioning
Hardcoded coordinates are fine for a quick demo, but real apps usually need placement that adapts to image size and text length.
This pattern works well for captions and watermarks in the lower-right corner.
Use Attributed Text for Better Styling
If you need richer typography, use NSAttributedString.
That gives you control over color, font, stroke, paragraph style, and other formatting details.
Overlaying Text in the UI Is a Different Problem
Sometimes developers say they want to add text to an image, but they really want text displayed on top of an image view. In that case, do not redraw the image at all.
This keeps the text editable and avoids generating a new bitmap every time the caption changes.
A Reusable UIImage Extension
If the operation appears often in your codebase, a UIImage extension keeps the call site clean.
Then use it like this:
Think About Scale and Contrast
Images with very high resolution can make a fixed font size look tiny. Busy photos can also make white text unreadable. A semitransparent background, shadow, or stroke is often necessary if the text must remain legible across different images.
Common Pitfalls
- Baking text into the image when a simple UI overlay would have been more flexible.
- Hardcoding positions without measuring the rendered text first.
- Ignoring image size and scale, which makes the text look too small or too large.
- Choosing colors that disappear against the photo background.
- Reaching for older graphics APIs when
UIGraphicsImageRendereris the clearer modern UIKit option.
Summary
- To permanently add text, draw the image and text into a new graphics context.
- '
UIGraphicsImageRendereris the standard UIKit tool for this work.' - Measure the text before positioning it if placement must adapt to different images.
- Use attributed text when you need richer styling.
- If the text is only for display, overlay a label instead of modifying the image pixels.

