将文本和图像保存在AttributedString中

问题描述 投票:0回答:1

我在Swift中有一个属性字符串,它在用户名旁边显示一个图标。这很好用,我的实现看起来像这样:

attributedUsername = NSMutableAttributedString(string: "username")
let iconAttachment = NSTextAttachment()
let iconImage = UIImage(named: "userIcon")
iconAttachment.image = iconImage
iconAttachment.bounds = CGRect(x: 0, y: -3, width: 14, height: 14)
let iconString = NSAttributedString(attachment: verifiedAttachment)
attributedUsername.append(iconString)

usernameLabel.attributedText = attributedUsername

但是,有时用户名太大而无法放在一行上,将用户名包装在第二行(numberOfLines = 0)。这没关系,但如果用户名足够长以适应屏幕,那么图像将换行到下一行。我想知道是否有任何方法可以将图标包装到用户名的末尾。我想要实现的目标,*是图标,是:

username *

longer username *

a very long
username *

代替:

username *

longer username *

a very long username
*

所以基本上我希望图标与用户名的最后一部分(如果可能)粘在一起。如果用户名不包含空格且太长,那么它应该只包含在下一行,因为这将是标准实现。有什么建议?

ios swift nsattributedstring nsmutablestring
1个回答
0
投票

好吧,我不确定你是否可以通过在NSAttributedString中设置一些选项来做到这一点,但你可以通过一个简单的算法轻松实现。

首先,将创建属性字符串的代码移动到函数中,因为我们将使用它来计算宽度。确保还设置了font属性,因此可以从属性字符串中获取正确的大小:

func attributedString(for text: String) -> NSAttributedString {
    let attributedText = NSMutableAttributedString(string: text)
    let iconAttachment = NSTextAttachment()
    let iconImage = UIImage(named: "star")
    iconAttachment.image = iconImage
    iconAttachment.bounds = CGRect(x: 0, y: -3, width: 14, height: 14)
    let iconString = NSAttributedString(attachment: iconAttachment)
    attributedText.append(iconString)
    attributedText.setAttributes([.font: UIFont(name: "Avenir-Book", size: 15)!],
                                 range: NSRange((text.startIndex..<text.endIndex), in: text))
    return attributedText
}

然后:

let text = "some really really really really long usernameeeeeeeee"
let attributedText = attributedString(for: text)
let maxWidth = ... 

if attributedText.size().width > maxWidth { // A line break is required
    let lastWord = text.components(separatedBy: " ").last!
    let attributedLastWord = attributedString(for: lastWord)
    if attributedLastWord.size().width < maxWidth { // Forcing image to stick to last word
        var fixedText = text
        fixedText.insert("\n", at: text.index(text.endIndex, offsetBy: -lastWord.count))
        label.attributedText = attributedString(for: fixedText)
    } else {
        label.attributedText = attributedText
    }
} else {
    label.attributedText = attributedText
}

当然,你会想要消除力量展开和其他不那么好的做法。不过,这些只是为了简洁起见。我希望你有这个主意。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.