UITextView 在textview文本中插入文本

发布于 2024-08-31 22:37:28 字数 148 浏览 3 评论 0原文

我想偶尔将文本插入 UITextView 文本对象中。例如,如果用户按下“新段落”按钮,我想插入双换行符,而不仅仅是标准的单换行符。

我该怎么办?我是否必须从 UITextView 读取字符串,对其进行变异,然后写回?那我怎么知道指针在哪里呢?

谢谢

I want to have to occasionally insert text into the UITextView text object. For example, if the user presses the "New Paragraph" button I would like to insert a double newline instead of just the standard single newline.

How can I go about such? Do i have to read the string from UITextView, mutate it, and write it back? Then how would I know where the pointer was?

Thanks

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

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

发布评论

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

评论(6

青芜 2024-09-07 22:37:28

由于 UITextViewtext 属性是不可变的,因此您必须创建一个新字符串并为其设置 text 属性。 NSString 有一个实例方法(-stringByAppendingString:),用于通过将参数附加到接收者来创建新字符串:

textView.text = [textView.text stringByAppendingString:@"\n\n"];

Since the text property of UITextView is immutable, you have to create a new string and set the text property to it. NSString has an instance method (-stringByAppendingString:) for creating a new string by appending the argument to the receiver:

textView.text = [textView.text stringByAppendingString:@"\n\n"];
审判长 2024-09-07 22:37:28

UITextview 有一个 insertText 方法,它尊重光标位置。

- (void)insertText:(NSString *)text

例如:

[myTextView insertText:@"\n\n"];

UITextview has an insertText method and it respects cursor position.

- (void)insertText:(NSString *)text

for example:

[myTextView insertText:@"\n\n"];
盗梦空间 2024-09-07 22:37:28

这是我实现它的方法,看起来效果很好。

- (void) insertString: (NSString *) insertingString intoTextView: (UITextView *) textView  
{  
    NSRange range = textView.selectedRange;  
    NSString * firstHalfString = [textView.text substringToIndex:range.location];  
    NSString * secondHalfString = [textView.text substringFromIndex: range.location];  
    textView.scrollEnabled = NO;  // turn off scrolling or you'll get dizzy ... I promise  

    textView.text = [NSString stringWithFormat: @"%@%@%@",  
      firstHalfString,  
      insertingString,  
      secondHalfString];  
    range.location += [insertingString length];  
    textView.selectedRange = range;  
    textView.scrollEnabled = YES;  // turn scrolling back on.  

}

Here's how I implemented it and it seems to work nicely.

- (void) insertString: (NSString *) insertingString intoTextView: (UITextView *) textView  
{  
    NSRange range = textView.selectedRange;  
    NSString * firstHalfString = [textView.text substringToIndex:range.location];  
    NSString * secondHalfString = [textView.text substringFromIndex: range.location];  
    textView.scrollEnabled = NO;  // turn off scrolling or you'll get dizzy ... I promise  

    textView.text = [NSString stringWithFormat: @"%@%@%@",  
      firstHalfString,  
      insertingString,  
      secondHalfString];  
    range.location += [insertingString length];  
    textView.selectedRange = range;  
    textView.scrollEnabled = YES;  // turn scrolling back on.  

}
叫思念不要吵 2024-09-07 22:37:28

对于 Swift 3.0

您可以通过调用 UITextView 实例的 insertText 方法来附加文本。

例子:

textView.insertText("yourText")

For Swift 3.0

You can append text by calling method insertText of UITextView Instance.

Example:

textView.insertText("yourText")
财迷小姐 2024-09-07 22:37:28

这是正确答案!

光标位置也正确。

滚动位置也正确。

- (void) insertString: (NSString *) insertingString intoTextView: (UITextView *) textView
{
    [textView replaceRange:textView.selectedTextRange withText:insertingString];
}

This is a correct answer!

The cursor position is also right.

A scroll position is also right.

- (void) insertString: (NSString *) insertingString intoTextView: (UITextView *) textView
{
    [textView replaceRange:textView.selectedTextRange withText:insertingString];
}
夜无邪 2024-09-07 22:37:28
- (void)insertStringAtCaret:(NSString*)string {
    UITextView *textView = self.contentCell.textView;

    NSRange selectedRange = textView.selectedRange;
    UITextRange *textRange = [textView textRangeFromPosition:textView.selectedTextRange.start toPosition:textView.selectedTextRange.start];

    [textView replaceRange:textRange withText:string];
    [textView setSelectedRange:NSMakeRange(selectedRange.location + 1, 0)];

    self.changesDetected = YES; // i analyze the undo manager in here to enabled/disable my undo/redo buttons
}
- (void)insertStringAtCaret:(NSString*)string {
    UITextView *textView = self.contentCell.textView;

    NSRange selectedRange = textView.selectedRange;
    UITextRange *textRange = [textView textRangeFromPosition:textView.selectedTextRange.start toPosition:textView.selectedTextRange.start];

    [textView replaceRange:textRange withText:string];
    [textView setSelectedRange:NSMakeRange(selectedRange.location + 1, 0)];

    self.changesDetected = YES; // i analyze the undo manager in here to enabled/disable my undo/redo buttons
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文