我创建了一个添加到 UIView 的 UITextView,这个 TextView 应该在右上角或左上角显示一个图像,并用一些文本包裹。
创建 TextView 不是问题(很明显),我只是无法理解 NSAttributedString。
至于图像,我使用了 NSTextAttachment;
var image:UIImage = UIImage(named: "placeHolder.png")
var textAttachment:NSTextAttachment = NSTextAttachment(data: nil, ofType: nil)
textAttachment.image = image
然后我们为 TextView 准备了一些文本;
var text:String = "Lorem ipsum"
现在是棘手的部分。我可以创建一个可以设置为 UITextView 的 NSAttributedString。它显示图像很好,我只是不知道如何设置文本。
var s:NSAttributedString = NSAttributedString(attachment: textAttachment)
textView.attributedText = s
有什么想法吗?
最佳答案
下面的代码展示了如何构造一个由文本和一些 NSTextAttachment 组成的 NSAttributedString:
然而...
this TextView should display an image at the top right or left corner wrapped by some text.
然后你想要的是 不是 NSTextAttachment,因为那是内联图像没有包装。
要在固定位置获取带环绕的图像,请使用图像 subview 并使用排除路径引起环绕。
关于ios - 带有 NSAttributedString 和 NSTextAttachment 的 UITextView。如何显示文字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25532549/