How do I add text to an image in iOS Swift?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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.
Related reading
- How do I adjust the anchor point of a CALayer, when Auto Layout is being used?
- How do I align views at the bottom of the screen?
- How do I allow text selection on a Text label in SwiftUI?
- How do I apply a perspective transform to a UIView?
- How do I associate file types with an iPhone application?
- How do I avoid capturing self in blocks when implementing an API?
- How do I call Objective-C code from Swift?
- How do I call Objective-C code from Swift?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.