图像附件作为 NSAttributedstring 中的第一项(左对齐)

Image attachment as first item (left aligned) in NSAttributed string

我正在尝试使用 NSAttributedStringNSTextAttachment

将图像添加到 UILabel
func attributedString(string:String)->NSAttributedString {
    let label:NSMutableAttributedString = NSMutableAttributedString()

    let string = NSAttributedString(string: string)
    let attachment = NSTextAttachment()
    attachment.image = #imageLiteral(resourceName: "WaterIcon")
    let imageString = NSAttributedString(attachment: attachment)

    label.append(string)
    label.append(imageString)

    return label
}

当我切换时

    label.append(imageString)
    label.append(string)

…这就是我想要的订单,图像根本没有显示:-(

如何将图像添加为字符串中的第一项?

你的代码对我来说工作得很好,交换了两个 append 语句:

可能您的标签 sized/positioned 或您将属性字符串分配给标签的方式有问题。但是你没有显示你的代码的任何部分,所以谁知道你做错了什么?