用于NSTextAttachments的iOS UILabel自动换行

时间:2019-06-14 11:45:20

标签: ios swift uilabel nstextattachment

任何人都经历过NSTextAttachments的自动换行,我基本上是在尝试使用NSAttributedString和NSTextAttachments重新创建包括星星(图像)的Label。我设法在同一标签中添加了星星,但是问题是字符包装。基本上,我希望所有星星都包装成一个单词。目前,每个星星都被视为单独的单词。

let imageAttachment =  NSTextAttachment()
    imageAttachment.image = "movies_rating_star_icon".image

    let attachmentString = NSAttributedString(attachment: imageAttachment)
    let completeText = NSMutableAttributedString(string: title + "  ")

    var count = rating
    while count > 0 {
        completeText.append(attachmentString)
        count = count - 1
    }

    titleLabel.attributedText = completeText

0 个答案:

没有答案