gpt4 book ai didi

iphone - UITextView 中模拟按键的性能问题

转载 作者:行者123 更新时间:2023-12-03 19:42:11 25 4
gpt4 key购买 nike

我使用以下代码来模拟 UITextView 中的按键(并将滚动设置到插入位置):

NSRange selectedRange = textview.selectedRange;

NSString *currentText = textview.text;

NSString *yourString = @"x";

NSString *firstPart = [currentText substringToIndex: selectedRange.location];
NSString *lastPart = [currentText substringFromIndex: selectedRange.location];

NSString *modifiedText = [firstPart stringByAppendingFormat:@"%@%@", yourString, lastPart];

textview.text = modifiedText;

NSInteger loc = selectedRange.location + 1;
NSInteger len = textview.selectedRange.length;

NSRange newRange = NSMakeRange(loc, len);
textview.selectedRange = newRange;

如您所见,我划分了textview.text,在光标位置插入@“x”并修改了整个文本。事实上,除非文本文件的长度很大,否则这非常有效。这听起来很合乎逻辑,因为我将整个事情分成几部分,每个模拟键也是如此。

因此,对于一个小文本文件,我没有任何问题,但对于一个大文本文件,我可以看到相当大的滞后。

有什么办法可以实现更好的性能吗?

最佳答案

您应该使用
,而不是在每次击键时重写整个文本-(void)insertText:(NSString*)text 方法

示例代码来自here :

@interface UIResponder(UIResponderInsertTextAdditions)
- (void) insertText: (NSString*) text;
@end

@implementation UIResponder(UIResponderInsertTextAdditions)

- (void) insertText: (NSString*) text
{
// Get a refererence to the system pasteboard because that's
// the only one @selector(paste:) will use.
UIPasteboard* generalPasteboard = [UIPasteboard generalPasteboard];

// Save a copy of the system pasteboard's items
// so we can restore them later.
NSArray* items = [generalPasteboard.items copy];

// Set the contents of the system pasteboard
// to the text we wish to insert.
generalPasteboard.string = text;

// Tell this responder to paste the contents of the
// system pasteboard at the current cursor location.
[self paste: self];

// Restore the system pasteboard to its original items.
generalPasteboard.items = items;

// Free the items array we copied earlier.
[items release];
}

@end

关于iphone - UITextView 中模拟按键的性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5520635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com