获取包含文本附件的NSMutableAttributedString,如下所示:
let myString = NSMutableAttributedString(string: "Hello\n\n")let attachment = NSTextAttachment()attachment.image = image // some UIImagelet 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:因为它会重置最初在字符串上设置的属性.从文本附件生成属性字符串时,它会设置显示图像所需的一些初始属性. 总结以上是内存溢出为你收集整理的cocoa-touch – 设置属性时NSTextAttachment图像消失全部内容,希望文章能够帮你解决cocoa-touch – 设置属性时NSTextAttachment图像消失所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)