How can I replace text in UITextView with NSUndoManager support?

前端 未结 4 596
清歌不尽
清歌不尽 2021-02-06 01:00

I want to be able to replace some text in an UITextView programatically, so I wrote this method as an UITextView category:

- (void) replaceCharactersInRange:(NSR         


        
4条回答
  •  旧巷少年郎
    2021-02-06 01:05

    Shouldn't it be

    [[self.undoManager prepareWithInvocationTarget:self] replaceCharactersInRange:NSMakeRange(range.location, newText.length) 
                                                                       withString:[self.text substringWithRange:range]];
    

    And you probably can remove the mutable string and just do,

    self.text = [self.text stringByReplacingCharactersInRange:range withString:newText];
    

提交回复
热议问题