swiftphoto

how to define text alignment, alpha of text background and add image before text while adding text to photo


for (index, textData) in textDataCurrentImg.enumerated() {
                let textColor = textData.fontColor
                let textFont = UIFont(name: textData.font.fontName, size: textData.fontSize)!
                let padding: CGFloat = 10.0
                let breakdText = textData.addBreaks(in: textData.text, with: textFont, forWidth: newImage.size.width, padding: Int(textData.xPosition))
                
                let scale = UIScreen.main.scale
                UIGraphicsBeginImageContextWithOptions(newImage.size, false, scale)
                
                // Draw image
                newImage.draw(in: CGRect(origin: CGPoint.zero, size: newImage.size))
                
                let textFontAttributes: [NSAttributedString.Key : Any] = [
                        NSAttributedString.Key.font: textFont,
                        NSAttributedString.Key.foregroundColor: textColor,
                        ]
                
                if let backColor = textData.backColor {
                    // Draw background rectangle
                    let textSize = breakdText.size(withAttributes: textFontAttributes)
                    let backgroundRect = CGRect(
                        origin: CGPoint(x: textData.xPosition - padding, y: textData.yPosition - padding),
                        size: CGSize(width: textSize.width + 2 * padding, height: textSize.height + 2 * padding)
                    )
                    backColor.setFill()
                    UIRectFill(backgroundRect)
                }
                
                // Draw text on top of the background rectangle
                breakdText.draw(in: CGRect(x: textData.xPosition, y: textData.yPosition, width: newImage.size.width, height: newImage.size.height), withAttributes: textFontAttributes)
                
                newImage = UIGraphicsGetImageFromCurrentImageContext() ?? image
                UIGraphicsEndImageContext()
            }

this is my code and i am adding texts to photo here

text alignment - for text alignment i have tried this but not working as expected.

let textFontAttributes: [NSAttributedString.Key : Any] = [
        NSAttributedString.Key.font: textFont,
        NSAttributedString.Key.foregroundColor: textColor,
        NSAttributedString.Key.paragraphStyle: {
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.alignment = textData.textAlignment
            return paragraphStyle
        }()
    ]

alpha of text background let alpha: CGFloat = 0.5 let alphaColor = backColor.withAlphaComponent(alpha)

add image before text - if textData.isLocation i want to add one image before text as below example image


Solution

  • You can put all the contents in a custom view like this:

    enter image description here

    Then you're able to hide any sub-views or the entire view you want before taking a snapshot

    class CollectionPhoto: UIView {
      @IBOutlet private var pinIcon: UIImageView!
      @IBOutlet private var contentLabel: UILabel!
      @IBOutlet private var contentImage: UIImageView!
      
      func takeSnapShot() -> UIImage? {
        pinIcon.isHidden = true
        contentLabel.isHidden = true
        
        ...
        let image = UIGraphicsGetImageFromCurrentImageContext()
         
        pinIcon.isHidden = false
        contentLabel.isHidden = false
    
        return image
      }
    }