How to Add Images as Text Attachment in Swift Using Nsattributedstring

Keep text and image together in AttributedString

Well, I am not sure if you can do this by setting some option in NSAttributedString, but you can easy achieve that with a simple algorithm.

First, move the code that creates the attributed string to a function, since we'll be using it to calculate the width. Make sure to also set the font attribute, so it's possible to get the correct size out of the attributed string:

func attributedString(for text: String) -> NSAttributedString {
let attributedText = NSMutableAttributedString(string: text)
let iconAttachment = NSTextAttachment()
let iconImage = UIImage(named: "star")
iconAttachment.image = iconImage
iconAttachment.bounds = CGRect(x: 0, y: -3, width: 14, height: 14)
let iconString = NSAttributedString(attachment: iconAttachment)
attributedText.append(iconString)
attributedText.setAttributes([.font: UIFont(name: "Avenir-Book", size: 15)!],
range: NSRange((text.startIndex..<text.endIndex), in: text))
return attributedText
}

Then:

let text = "some really really really really long usernamKeep text and image together in AttributedString How to insert a PDF attachment into NSAttributedString and display in a UITextView Cant add image as NSTextAttachment te"
let attributedText = attributedString(for: text)
let maxWidth = ...

if attributedText.size().width > maxWidth { // A line break is required
let lastWord = text.components(separatedBy: " ").last!
let attributedLastWord = attributedString(for: lastWord)
if attributedLastWord.size().width < maxWidth { // Forcing image to stick to last word
var fixedText = text
fixedText.insert("\n", at: text.index(text.endIndex, offsetBy: -lastWord.count))
label.attributedText = attributedString(for: fixedText)
} else {
label.attributedText = attributedText
}
} else {
label.attributedText = attributedText
}

Of course you will want to remove the force unwrap and other not so good practices. Those are just for brevity, though. I hope you got the idea.

How to insert a PDF attachment into NSAttributedString and display in a UITextView

The best solution I have come up with so far is to subclass NSTextAttachment:

class TextAttachmentWithThumbnail: NSTextAttachment {
private var thumbnail: UIImage?

override var image: UIImage? {
get { return thumbnail }
set { thumbnail = newValue }
}
}

So that setting an image no longer resets contents and fileType

Cant add image as NSTextAttachment to UITextField

UITextField will not allow NSTextAttachment but UILabel allows the NSTextAttachment.

Sample Image

For Example:

let attachment = NSTextAttachment()
let imageTest = UIImage(named:"user.png")
attachment.image = imageTest
let myString = NSMutableAttributedString(string: "My text ")
let myStringWithImage = NSMutableAttributedString(attributedString: NSAttributedString(attachment: attachment))
myStringWithImage.append(myString)
myTextField.attributedText = myStringWithImage
myLabel.attributedText = myStringWithImage

No answers in this apple thread. As of my thought we can enter emojis in text field but not text attachment. In text field we are giving flexibility to user for entering text.

You can use the leftView of UITextField to display image.

Align the image and text in NSAttributed text

You need to set the bounds of the attachment. Here is your updated code.

        let attachment = NSTextAttachment()
let text = "Current user."
let img = UIImage(named: "icon-horizontal-line")
let font = UIFont.systemFont(ofSize: 18)
attachment.image = img
let mid = font.descender + font.capHeight
attachment.bounds = CGRect(x: 0, y: font.descender - img!.size.height / 2 + mid + 2, width: img!.size.width, height: img!.size.height)
let attachmentString = NSAttributedString(attachment: attachment)
let mutableAttributedString = NSMutableAttributedString()
mutableAttributedString.append(attachmentString)
let string = NSMutableAttributedString(string: text, attributes: [:])
mutableAttributedString.append(string)

label.attributedText = mutableAttributedString

NSTextAttachment image in attributed text with foreground colour

It looks like there is a bug with the NSTextAttachment(image:) constructor (on iOS 13, at the time of this answer), the following image attachment construction works correctly:

let attachment = NSTextAttachment()
attachment.image = image


Related Topics



Leave a reply



Submit