UITextView 添加文本到新行

发布于 2024-08-05 11:04:18 字数 260 浏览 0 评论 0原文

我想向我的 iPhone 应用程序的 UITextView 添加新的文本行。基本上我的文本视图以一行开始,随着应用程序的进步,会添加新行。但是,我只能添加到现有文本的末尾或开头。我怎样才能将这样的条目放入新行中。我可以在计算的范围/位置手动发送 \n 到 textview.text 吗?

例如我的意思。 第 1 行:这是文本的第一行。这是第二个 第 2 行:文本行。 - -

我想: 第 1 行:这是文本的第一行。 第 2 行:这是文本的第二行。

谢谢!

I would like to add new lines of text to my UITextView for my iPhone app. Basically my textview starts with a single line, and as app advances, new lines are added. However I can add only to the end or beginning of the existing text. How can I make such an entry into a new line. Can I manually send \n to the textview.text at a calculated range/location.

E.g. of what I mean.
line 1: This is first line of text. This is second
line 2: line of text. ---

I want:
line 1: This is first line of text.
line 2: This is second line of text.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

汐鸠 2024-08-12 11:04:18

您可以使用 -[NSString stringByReplacingCharactersInRange:withString:] 修改 UITextView 的文本。然后,您可以将该字符串指定为 UITextView 的新文本对象。

NSString *text = @"line 1. test line 2";

// Delete 'test' from the string
NSString *replacement = [text stringByReplacingCharactersInRange:NSMakeRange(8, 5) withString:@""];

You can use -[NSString stringByReplacingCharactersInRange:withString:] to modify the text of the UITextView. You can then assign that string as the new text object of the UITextView.

NSString *text = @"line 1. test line 2";

// Delete 'test' from the string
NSString *replacement = [text stringByReplacingCharactersInRange:NSMakeRange(8, 5) withString:@""];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文