2

I am working on project where I need to add text to the image which is coming from the textfield.

But when I see the text on the image it shows the font size smaller than the font size of textfield.

I am using following method to draw text on image

func drawText(text: String, inImage image: UIImage, atPoint point: CGPoint, fontName:String, fontSize: String,textColor: UIColor) -> UIImage? {

        UIGraphicsBeginImageContextWithOptions(image.size, true, UIScreen.mainScreen().scale)
        UIGraphicsBeginImageContext(image.size)

        image.drawInRect(CGRectMake(0, 0, image.size.width, image.size.height))
        let rect: CGRect = CGRectMake(point.x, point.y, image.size.width, image.size.height)
        // UIColor.whiteColor().set()

        // set the font to Helvetica Neue 18
        if let sizeFont = NSNumberFormatter().numberFromString(fontSize) {

            // TODO: - Need to resolve issue
            let originalSize = sizeFont.integerValue

            let finalFontSize = CGFloat(originalSize)

            let fieldFont = UIFont(name: fontName, size:finalFontSize*1.5)

            // set the line spacing to 6
            let paraStyle = NSMutableParagraphStyle()
            // paraStyle.lineSpacing = 6.0

            // set the Obliqueness to 0.1
            // let skew = 0.1

            let attributes: NSDictionary = [
                NSForegroundColorAttributeName: textColor,
               // NSParagraphStyleAttributeName: paraStyle,
                // NSObliquenessAttributeName: skew,
                NSFontAttributeName: fieldFont!
            ]

            NSString(string: text).drawInRect(rect, withAttributes: attributes as? [String : AnyObject])

            let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return newImage
        }

        return nil
    }

And font size could be 16-60px.

Please let me know what could be the solution.

Thanks

Gopal Devra
  • 564
  • 2
  • 5
  • 24

3 Answers3

3

Everything seems fine with your code.
One possible problem is that you don't see the image at full size inside your ImageView because it scales it, so you see the font smaller than what you want.
You could resize the image before drawing the text on it to fit the container it will be displayed on. Or you can calculate the fontSize multiplying it with 1/scale, with
scale = the scale at will the image will be shown
For example if the image is taller than larger and the container (the imageView) is smaller than the image scale will be image.size.height/imageView.frame.size.height.
I think that this could resolve your problem.

LorenzOliveto
  • 7,796
  • 1
  • 20
  • 47
0

As suggested by LorenzOliveto, this could be due to scaling issues. One of the easy workaround would be to add text as a subview of imageView and then convert the imageView to image using this extension.

extension UIView {
    /**
     Convert UIView to UIImage
     */
    func toImage() -> UIImage {
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.isOpaque, 0.0)
        self.drawHierarchy(in: self.bounds, afterScreenUpdates: false)
        let snapshotImageFromMyView = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return snapshotImageFromMyView!
    }
}
-1

iOS text font

we operates next dimensions:

for example:

font.size = 11 pt = 33 px for retina
image.size = 200x200 px
imageView.size = 100x100 px

By default algorithm is next:

1. draw Font onto Image 
2. fit Image into ImageView

As a result Font is smaller because of zoom out. More over Font will be different for different Images(depends on Image size)

As a variant you can calculate a font dynamically:

extension UIImage {
    func getFont(size: CGFloat) -> UIFont {
        let screenWidth = UIScreen.main.bounds.size.width
        let imageWidth = self.size.width
        let k = imageWidth / screenWidth
        let fontSize: CGFloat = size * k
        let font = UIFont(name: "Courier", size: fontSize)
        return font
    }
}

[Full example of drawing Text on Image]

yoAlex5
  • 29,217
  • 8
  • 193
  • 205