xcode - 将图像从 JSON 添加到文本字段

标签 xcode swift

我有一个文本字段,我使用 JSON 将内容加载到该字段中。我将如何向其中添加图像?例如,如果我要为它使用图像链接。

enter image description here

这就是我想要的方式。我希望能够在文本之间显示图片。

最佳答案

您必须创建一个 json 解析器并为每个图像创建一个 NSTextAttachment:

 let textAttachment = NSTextAttachment()
 textAttachment.image = yourImage
 let attrStringWithImage = NSAttributedString.init(attachment: textAttachment)
 attributedString.replaceCharactersInRange(NSMakeRange(6, 1), withAttributedString: attrStringWithImage)
 textView.attributedText = attributedString

关于xcode - 将图像从 JSON 添加到文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32308492/

相关文章:

ios - 如何在 Swift 3 中加载/删除带有动画的自定义 View

iphone - 我只希望我的应用程序支持 iPhone 5 和 iPod touch 5th gen 我该如何设置它以不允许其他设备下载?

ios - 无法将 @IBOutlet 从 swift 文件拖到 Xcode 7.2 中的 Storyboard ViewController

xcode - 在 xcode 单元测试中加载文件

swift - 'main()' 仅适用于 iOS 14.0 或更高版本的应用程序扩展

ios - 自定义 UILabel 在 Interface Builder 中显示不正确

objective-c - 在 Interface Builder 中向 NSPopUpButton 添加选项

xcode - 如何在不重新下载Xcode的情况下更新iPhone SDK?

ios - PerformSegue 被调用但实际上并未转至 VC2? ( swift 3.0)

multithreading - Swift 属性的 didSet{} 在哪个线程上执行?