I am making an application that automatically enters a number when a photo is selected using UIKit and Swift.
The picture above is the most perfect situation and result.
The problem arises in a situation like the picture below.
The problem arises when working with photos of different resolutions. The text is large in some photos and small in others.
The problem I was thinking of is that the font size seems to be different when giving pictures with different resolutions because the font size is given as a fixed value.
// add text in photo
func textToImage(drawText text: String, inImage image: UIImage, fontSize: CGFloat, atPoint point: CGPoint) -> UIImage {
let textStrokeWidth: Double = -4.0
let textStrokeColor: UIColor = UIColor.black
let textFillColor: UIColor = UIColor.white
let textFont = UIFont(name: "Helvetica Bold", size: fontSize)!
UIGraphicsBeginImageContextWithOptions(image.size, false, 1)
let textFontAttributes = [
NSAttributedString.Key.strokeColor: textStrokeColor,
NSAttributedString.Key.strokeWidth : textStrokeWidth,
NSAttributedString.Key.font: textFont,
NSAttributedString.Key.foregroundColor: textFillColor,
] as [NSAttributedString.Key : Any]
image.draw(in: CGRect(origin: CGPoint.zero, size: image.size))
let rect = CGRect(origin: point, size: image.size)
text.draw(in: rect, withAttributes: textFontAttributes)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
I modified the code from the above source slightly and tried it. And to solve the scale problem, I am trying ways to calculate the fontsize, but the solution is taking too long than I thought, so I leave a question.
let scaleFactor: Float = Float(imageWidth) / 1500.0
let fontSize = CGFloat(150.0 * scaleFactor)
The calculation method I tried is as follows. When there is a picture with a width of 1500, font size 150 is the best, and the scale factor is calculated based on that.
However, as you can see, it is impossible to cope with all resolutions because only horizontal is used.
How can I find the scale factor that corresponds to all resolutions and calculate the font size of the same ratio even if different photos are input?
You don’t necessarily have to create text this way. I would like to know more about how to solve this problem.
2
Answers
try to get the dynamic font size based on the image width and image height.
One approach — with a few drawbacks — but might work for you.
The general idea…
150
pointsHere are the steps:
So, using this complete code for a sample app:
We can enter an image name to load, or generate solid-blue images with the entered dimensions, and draw the string at the calculated font size:
Note: this is a starting point. You’ll notice that the calculated text rect and position is based on the font, not the specific characters.
So, we can get this:
If you need the text vertically centered based on the actual characters, you’ll want to calculate the glyph bounding box and offset the drawing.
Edit – forgot to include the
sizeOfString
font extension…