How to add trailing image to UILabel in iOS

Issue #470

Use NSTextAttachment inside NSAttributedString

1
2
3
4
5
6
7
8
9
10
11
12
extension UILabel {
func addTrailing(image: UIImage) {
let attachment = NSTextAttachment()
attachment.image = image

let attachmentString = NSAttributedString(attachment: attachment)
let string = NSMutableAttributedString(string: self.text!, attributes: [:])

string.append(attachmentString)
self.attributedText = string
}
}

Comments