在 UITextView 的顶部插入(追加)文本 - Swift
Inserting (Appending) text on the top of a UITextView - Swift
我有一个 UITextView,我使用以下方法插入文本:
MyUITextView.insertText(my_string_to_be_inserted)
它工作正常,但它会将文本附加到 UITextView 的底部。
有什么方法可以在顶部添加文本(如堆栈),使最后添加的文本始终位于顶部?
您可以试试下面的代码片段。
if let position = textView.textRange(from: textView.beginningOfDocument, to: textView.beginningOfDocument) {
textView.replace(position, withText: "Your text goes here")
}
参考 api here.
我有一个 UITextView,我使用以下方法插入文本:
MyUITextView.insertText(my_string_to_be_inserted)
它工作正常,但它会将文本附加到 UITextView 的底部。
有什么方法可以在顶部添加文本(如堆栈),使最后添加的文本始终位于顶部?
您可以试试下面的代码片段。
if let position = textView.textRange(from: textView.beginningOfDocument, to: textView.beginningOfDocument) {
textView.replace(position, withText: "Your text goes here")
}
参考 api here.