【问题标题】:Inserting (Appending) text on the top of a UITextView - Swift在 UITextView 顶部插入(附加)文本 - Swift
【发布时间】:2020-02-12 13:48:27
【问题描述】:

我有一个 UITextView,我在其中插入文本:

MyUITextView.insertText(my_string_to_be_inserted)

它工作正常,但它会将文本附加到 UITextView 的底部。

有没有办法在顶部添加文本(如堆栈),最后添加的文本将始终位于顶部?

【问题讨论】:

    标签: ios swift append uitextview


    【解决方案1】:

    你可以试试下面的sn-p。

    if let position = textView.textRange(from: textView.beginningOfDocument, to: textView.beginningOfDocument) {
       textView.replace(position, withText: "Your text goes here")
    }
    

    请参考 apihere

    【讨论】:

    • 效果很好。你介意解释一下这个 sn-p 是如何工作的吗?谢谢
    • .replace 函数将用给定的字符串替换提供的文本范围内的内容。在您的情况下,要替换的文本范围为 0。我们使用 textrange 函数通过将 from 和 to 作为 beginOfDocument (因为我们需要的范围为零)从 textview 获得该文本范围。希望很清楚。如果您发现答案有帮助,您可以接受并投票,以便它也可以帮助其他人。干杯。
    猜你喜欢
    • 2011-09-02
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 2010-12-21
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多