图像附件作为NSAttributed字符串中的第一项(左对齐)

时间:2016-12-26 19:32:35

标签: ios swift uilabel nsattributedstring

我正在尝试使用NSAttributedString NSTextAttachment

将图像添加到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
}

enter image description here

当我切换

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

...这就是我想要的订单,图片根本没有出现: - (

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

enter image description here

1 个答案:

答案 0 :(得分:1)

使用交换的两个append语句,您的代码可以正常工作:

enter image description here

标签的大小/定位方式或者如何将属性字符串分配给标签可能存在问题。但是你没有展示你的代码中的任何一部分,所以谁知道你做错了什么?