在swift 3中更改图标位置

时间:2017-11-29 21:28:39

标签: ios swift nsattributedstring

我有以下代码,在uilabel的左侧显示一个图标,我的问题是如何将图标放在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

提前致谢

1 个答案:

答案 0 :(得分:1)

您可以在附加文本后附加附件

,将图像放在文本之后
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