相关疑难解决方法(0)

如何使用NSUndoManager支持替换UITextView中的文本?

我希望能够以编程方式替换UITextView中的某些文本,因此我将此方法编写为UITextView类别:

- (void) replaceCharactersInRange:(NSRange)range withString:(NSString *)newText{

    self.scrollEnabled = NO;

    NSMutableString *textStorage = [self.text mutableCopy];
    [textStorage replaceCharactersInRange:range withString:newText];

    //replace text but undo manager is not working well
    [[self.undoManager prepareWithInvocationTarget:self] replaceCharactersInRange:NSMakeRange(range.location, newText.length) 
                                                                       withString:[textStorage substringWithRange:range]];
    NSLog(@"before replacing: canUndo:%d", [self.undoManager canUndo]); //prints YES
    self.text = textStorage; 
    NSLog(@"after replacing: canUndo:%d", [self.undoManager canUndo]); //prints NO
    if (![self.undoManager isUndoing])[self.undoManager setActionName:@"replace characters"];
    [textStorage release];

    //new range:
    range.location = range.location + newText.length;
    range.length = 0;
    self.selectedRange = range;

    self.scrollEnabled = YES;

}
Run Code Online (Sandbox Code Playgroud)

它工作但NSUndoManager在self.text=textStorage我找到一个私有API 之后就停止工作(它似乎被重置):-insertText:(NSString *)可以完成这项工作,但是谁知道如果我使用它,Apple是否会批准我的应用程序.有没有办法在UITextView中使用NSUndoManager支持替换文本?或许我在这里遗失了什么?

iphone objective-c uitextview nsundomanager ios

10
推荐指数
2
解决办法
3205
查看次数

NSString UITextView无需替换以前的文本

如何在UITextView不替换以前的文本的情况下添加文本?

到目前为止,我有一个UITextView和一个UIButton添加文本UITextView,但我希望文本字段每次按下按钮时附加更多文本,而不是完全删除文本并替换它.

iphone nsstring uitextview ios

7
推荐指数
1
解决办法
1万
查看次数

标签 统计

ios ×2

iphone ×2

uitextview ×2

nsstring ×1

nsundomanager ×1

objective-c ×1