1

I have the following code which shows an icon on the left side of a uilabel, my question is how can I put the icon on the right side of the uilabel:

let attachment = NSTextAttachment()
attachment.image = UIImage(named: "ic_person")
attachment.bounds = CGRect(x: 0, y: -3, width: 15, height: 15)
let attachmentStr = NSAttributedString(attachment: attachment)
let myString = NSMutableAttributedString(string: "")
myString.append(attachmentStr)
let myString1 = NSMutableAttributedString(string: item_empresa[indexPath.item].nombre_emp!)
myString.append(myString1)
cell.lbl_nombreEmpresa.attributedText = myString

Thanks in advance

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Dimoreno
  • 227
  • 3
  • 15

1 Answers1

2

You can put the image after the text by appending attachment after appending the text itself

let attachment = NSTextAttachment()
attachment.image = UIImage(named: "ic_person")
attachment.bounds = CGRect(x: 0, y: -3, width: 15, height: 15)

let attributedString = NSMutableAttributedString(string: item_empresa[indexPath.item].nombre_emp ?? "")

attributedString.append(NSAttributedString(attachment: attachment))
cell.lbl_nombreEmpresa.attributedText = attributedString
zombie
  • 5,069
  • 3
  • 25
  • 54