[SWIFT] Display text on top of the image

I wanted to display the characters on the image, but the URL was very reliable ↓ https://qiita.com/taku/items/2c246d92bec494a1df5e

I copied the above URL as it is, but since I am a Swift beginner, I made a note of it because it took a lot of time with a little change due to the Swift version upgrade. .. With this source, I was able to display the text on the image even with Swift 12.

  //Display text on top of the image
    func drawText(image :UIImage) ->UIImage
        let text = "Sample Text"

        let tmpImage = UIImage(named: "1")
        let font = UIFont.boldSystemFont(ofSize: 32)
        let imageRect = CGRect(x: 0, y: 0, width: tmpImage.size.width, height: tmpImage.size.height)
        tmpImage.draw(in: imageRect)
        let textRect = CGRect(x: 5, y: 5, width: tmpImage.size.width - 5 , height: tmpImage.size.height - 5)
        let textStyle = NSMutableParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
        let textFontAttributes = [
            NSAttributedString.Key.font: font,
            NSAttributedString.Key.foregroundColor: UIColor.black,
            NSAttributedString.Key.paragraphStyle: textStyle
        ] as [NSAttributedString.Key : Any]
        text.draw(in: textRect, withAttributes: textFontAttributes)

        let newImage = UIGraphicsGetImageFromCurrentImageContext();
        return newImage!

I really want to dynamically place text in an Image retrieved from JSON via Alamofire, but that's about to happen ...

Recommended Posts

Display text on top of the image
Display an image on the base64 screen
[Ubuntu 20.04] Display the day of the week on the date / clock
(Ruby on Rails6) Display of the database that got the id of the database
Qualify only part of the text
Note on the path of request.getRequestDispatcher
[Ruby] Display the contents of variables
How to display 0 on the left side of the standard input value
About the version of Docker's Node.js image
Samshin on the value of the hidden field
part of the syntax of ruby ​​on rails
About the initial display of Spring Framework
Looking back on the basics of Java
Rails6: Extract the image in Action Text
Pre-processing to display on the browser (compiler)
Display the background image in the production environment
[Rails] Button to return to the top of the page
Add empty data to the top of the list
[Ruby on Rails] Until the introduction of RSpec
Display the System Monitor applet on the Ubuntu 20.04 taskbar
Display Mat type image on GUI with Swing
[Ruby] Code to display the day of the week
Install the latest version of Jenkins on Ubuntu 16
JavaFX8 image display
[Java / Kotlin] Resize considering the orientation of the image
How to display the result of form input
I touched on the new features of Java 15
Change the location folder of Docker image & container
A quick note on using jshell with the official Docker image of the JDK
[Ruby on Rails] Individual display of error messages
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
The mark on the right side of the version-controlled Xcode file
Docker the development environment of Ruby on Rails project
Diffusion simulation of ink dripping on the surface of the water
[Android] Display images and characters on the ViewPager tab
Image processing: The basic structure of the image read by the program
I want to display the name of the poster of the comment
I summarized the display format of the JSON response of Rails
Mandels to create an image of the Mandelbrot set
[SwiftUI] How to specify the abbreviated position of Text
Display the list in setDetails on the screen with spring-security
Display the average value of the evaluation as a star
[Introduction] Display Android Studio Hello World on the emulator
How to judge the click of any area of the image
[Rails] How to display an image in the view
Try using the query attribute of Ruby on Rails
The world of clara-rules (2)
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
Judgment of the calendar
The world of clara-rules (4)
The world of clara-rules (1)
The world of clara-rules (3)
The world of clara-rules (5)
The idea of quicksort
Display a balloon message in BarButtonItem of NavigationBar with a size according to the amount of text.
The idea of jQuery
[Rails] Put the same restrictions on multiple pieces of information
How to display the select field of time_select every 30 minutes
About truncation by the number of bytes of String on Android
Transition using the header image of CardView as it is
20190803_Java & k8s on Azure The story of going to the festival