加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocoa-touch – 设置属性时NSTextAttachment图像消失

发布时间:2020-12-14 04:41:59 所属栏目:百科 来源:网络整理
导读:我在将属性应用于NSMutableAttributedStrings时遇到了麻烦.如果他们有图像附件,则在添加属性时图像会消失. 获取包含文本附件的NSMutableAttributedString,如下所示: let myString = NSMutableAttributedString(string: "Hellonn")let attachment = NSText
我在将属性应用于NSMutableAttributedStrings时遇到了麻烦.如果他们有图像附件,则在添加属性时图像会消失.

获取包含文本附件的NSMutableAttributedString,如下所示:

let myString = NSMutableAttributedString(string: "Hellonn")
let attachment = NSTextAttachment()
attachment.image = image    // some UIImage
let attachmentString = NSAttributedString(attachment: attachment)
myString.appendAttributedString(attachmentString)

如果我尝试将属性应用于字符串,则会丢失附件:

let bodyFont = UIFont.preferredFontForTextStyle(UIFontTextStyleBody)
myString.setAttributes([NSFontAttributeName: bodyFont],range: NSMakeRange(0,myString.length))

字符串现在显示为正确的字体但附件已消失.如果我使范围myString.length – 1,附件仍然存在,所以我可以用更多的工作(可能通过查找NSTextAttachmentCharacter)踩到任何附件.我想知道是否有更简单的东西我不知道.

解决方法

解决方案是不使用setAttributes:而是addAttributes:因为它会重置最初在字符串上设置的属性.从文本附件生成属性字符串时,它会设置显示图像所需的一些初始属性.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读