Ios 在AttributedString中将文本和图像放在一起

Ios 在AttributedString中将文本和图像放在一起,ios,swift,nsattributedstring,nsmutablestring,Ios,Swift,Nsattributedstring,Nsmutablestring,我在Swift中有一个属性字符串,它在用户名旁边显示一个图标。这非常有效,我的实现如下所示: attributedUsername = NSMutableAttributedString(string: "username") let iconAttachment = NSTextAttachment() let iconImage = UIImage(named: "userIcon") iconAttachment.image = iconImage iconAttachment.bound

我在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
*

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

嗯,我不确定您是否可以通过在
NSAttributedString
中设置一些选项来实现这一点,但您可以通过简单的算法轻松实现

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

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
}
func attributedString(用于文本:String)->NSAttributedString{
让attributedText=NSMutableAttributedString(字符串:文本)
设iconAttachment=NSTextAttachment()
让iconImage=UIImage(名为“星”)
iconAttachment.image=iconImage
iconAttachment.bounds=CGRect(x:0,y:-3,宽度:14,高度:14)
让iconString=NSAttributedString(附件:iconAttachment)
AttributeText.append(iconString)
AttributeText.setAttributes([.font:UIFont(名称:“Avenir Book”,大小:15)!],
范围:NSRange((text.startIndex..maxWidth{//需要换行符
让lastWord=text.components(以:“”分隔)。last!
让attributedLastWord=attributedString(for:lastWord)
如果attributedLastWord.size().width

当然,您会希望删除强制展开和其他不太好的做法。不过,这些只是为了简洁。我希望您能理解。

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

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

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
}
func attributedString(用于文本:String)->NSAttributedString{
让attributedText=NSMutableAttributedString(字符串:文本)
设iconAttachment=NSTextAttachment()
让iconImage=UIImage(名为“星”)
iconAttachment.image=iconImage
iconAttachment.bounds=CGRect(x:0,y:-3,宽度:14,高度:14)
让iconString=NSAttributedString(附件:iconAttachment)
AttributeText.append(iconString)
AttributeText.setAttributes([.font:UIFont(名称:“Avenir Book”,大小:15)!],
范围:NSRange((text.startIndex..maxWidth{//需要换行符
让lastWord=text.components(以:“”分隔)。last!
让attributedLastWord=attributedString(for:lastWord)
如果attributedLastWord.size().width

当然,您会希望删除强制展开和其他不太好的做法。不过,这些只是为了简洁。我希望您能理解。

是否最好先有图像,后有用户名?然后您可以使用
.lineBreakMode=.byTruncatingTail
这样用户名以
非常长的用户结尾…
如果太长。我只是觉得用户名换行看起来是错误的。不幸的是,这不符合我的设计。这种情况并不常见——用户名通常会截短,并且不会换行,但也有可能发生这种情况。现在的例子是关于帖子下面喜欢的反馈:
用户名、用户名和13个其他人喜欢你的帖子。
-这可能会有两个长用户名被截断,而且可能发生在图标之前,所以我想把它们放在一起。好的,我现在明白了。只是一个建议,所以你显然不必这么做,但你可以创建一个类似Twitter的UI在手机上。当你进入“通知”部分时,会看到一排个人资料照片。下面写着类似于
用户名和另外两个喜欢你的回复的人。
。也许这看起来更干净、更有条理?这是一个显示个人资料照片的选项。如果能像Twitter、Instagram和其他社交网站一样显示这些照片,那就太好了首先显示的是10张个人资料图片,最后显示+13张。然而,如果只有一两张喜欢的图片,这会有点奇怪。因此,我们现在正在研究一种方法,以如下方式显示:
用户名喜欢你的帖子。
用户名和用户名喜欢你的帖子
,如果有更喜欢它会进入
用户名,用户名和其他1个喜欢你的帖子。
-但是问题就是我在OP.中问的