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

cocoa-touch – 设置属性时NSTextAttachment图像消失,第1张

概述我在将属性应用于NSMutableAttributedStrings时遇到了麻烦.如果他们有图像附件,则在添加属性时图像会消失. 获取包含文本附件的NSMutableAttributedString,如下所示: let myString = NSMutableAttributedString(string: "Hello\n\n")let attachment = NSTextAttachmen 我在将属性应用于NSMutableAttributedStrings时遇到了麻烦.如果他们有图像附件,则在添加属性时图像会消失.

获取包含文本附件的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图像消失所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1007177.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存