在异步线程或图像缓存库(如 SDwebimage)中动态加载图像。下面的代码是我尝试过的,从网络获取图像后它不会重新绘制。
let mutableAttributedString = NSMutableAttributedString()
if let _img = newsItem.img {
var attachment = NSTextAttachment()
attachment.bounds = CGRectMake(4, 4, expectedWidth, expectedWidth * _img.ratio)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
attachment.image = UIImage(data: NSData(contentsOfURL: NSURL(string: _img.src)!)!)
})
mutableAttributedString.appendAttributedString(NSAttributedString(attachment: attachment))
}
After image
of NSTextAttachment
设置完毕后,需要强制刷新textView
内容。为此你可以使用textView.layoutManager's
method invalidateDisplayCharacterRange
, where range
- 是你的 NSTextAttachment 子字符串的范围
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)