0

I was written to code using NSMutableAttributedString() with UIImage() and String.

extension UILabel {    
    func attachImageLabel(string: String, image: UIImage?) {
        let attachment = NSTextAttachment()
        attachment.image = image

        let attachmentString = NSAttributedString(attachment: attachment)
        let attributedString = NSAttributedString(string: string)
        
        let contentString = NSMutableAttributedString(string: "")
        contentString.append(attachmentString)
        contentString.append(attributedString)
        self.attributedText = contentString
        self.backgroundColor = .yellow
    }
}

But I want to write like below picture. Text must be move to top in UILabel.

enter image description here

Tae Hun Yu
  • 39
  • 7
  • 1
    I prefer to use Vertical UIStackView with an UIImageView and an UILabel – Quang Hà Feb 09 '22 at 08:12
  • 1
    You can't do it that way, since thee baseline of the first line (image and "I was written") needs to be the same. You could use though excludingPath if you use a `UITextView`. Or use two views, or reduce the size of the image. – Larme Feb 09 '22 at 10:29
  • Thank you. I read comments and resolve that storyboard using UIImageView and two UILables. – Tae Hun Yu Feb 10 '22 at 01:49

0 Answers0