#import "ViewController.h"
@interface CCTextObjc : NSObject
@property(nonatomic,copy) NSString *text;
@property(nonatomic,assign) NSUInteger op;
@property(nonatomic,assign) NSRange range;
@property(nonatomic,copy) NSString *textViewText;
@end
@interface ViewController ()<UITextViewDelegate>{
NSUndoManager *_undomanager;
NSInteger _length;
NSRange rg;
NSString *currentText;
NSString *addText;
}
- (IBAction)chexiao:(id)sender;
- (IBAction)fanchexiao:(id)sender;
@property (weak, nonatomic) IBOutlet UITextView *text;
@property (weak, nonatomic) IBOutlet UIButton *cheBtn;
@property (weak, nonatomic) IBOutlet UIButton *fanBtn;
@property(nonatomic,copy) NSString *useTitle;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.text.delegate = self;
_length =0;
//初始化NSUndoManager
_undomanager = [[NSUndoManager alloc] init];
//监听textview文本改动的通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeTextViewText) name:UITextViewTextDidChangeNotification object:nil];
// [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardShow:) name:UIKeyboardWillShowNotification object:nil];
// [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardHidden:) name:UIKeyboardWillHideNotification object:nil];
}
- (IBAction)fanchexiao:(id)sender {
[self redoitem];
}
- (IBAction)chexiao:(id)sender {
[self undoitem];
}
-(void)redoitem{
//反撤销
[_undomanager redo];
}
-(void)undoitem{
//撤销
[_undomanager undo];
}
-(void)keyBoardShow:(NSNotification *)noti{
NSDictionary *dic = noti.userInfo;
NSValue *aValue = [dic objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
int height = keyboardRect.size.height;
[_text setContentInset:UIEdgeInsetsMake(0,0, height, 0)];
}
-(void)keyBoardHidden:(NSNotification *)noti{
[_text setContentInset:UIEdgeInsetsMake(0,0, 0,0)];
}
- (void)setMyObjectTitle:(CCTextObjc *)obj{
// NSString *newTitle = obj.text;
//判断当前NSUndoManager的状态,是处于撤销或者反撤销的状态
if (_undomanager.isUndoing) {
if (_text.text.length>0) {
//获取
NSMutableString *tt = [[NSMutableString alloc] initWithString:obj.textViewText];
[tt replaceCharactersInRange:obj.range withString:@""];
_text.text = [NSString stringWithFormat:@"%@",tt];
[_undomanager registerUndoWithTarget:self
selector:@selector(setMyObjectTitle:)
object:obj];
}
}else if (_undomanager.isRedoing){
_text.text = obj.textViewText;
[_undomanager registerUndoWithTarget:self
selector:@selector(setMyObjectTitle:)
object:obj];
}else{
// NSString *currentText = obj.textViewText;
// if (newTitle != currentText) {
// _text.text = currentText;
//
// }else{
// _text.text = newTitle;
//
// }
[_undomanager registerUndoWithTarget:self
selector:@selector(setMyObjectTitle:)
object:obj];
}
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
rg = range;
currentText = textView.text;
addText = text;
return YES;
}
-(void)changeTextViewText{
if (_text.text.length>0) {
_cheBtn.enabled =YES;
}else{
_cheBtn.enabled =NO;
_fanBtn.enabled =NO;
}
// NSString *text ;
// if (_length !=0) {
// NSInteger textLength =_text.text.length;
// if (textLength >_length) {
// NSInteger newLength = textLength -_length;
// text = [NSString stringWithFormat:@"%@",[_text.text substringWithRange:NSMakeRange(_length, newLength)]];
// }else{
// text = _text.text;
// }
// }else{
// text = _text.text;
// }
_length =_text.text.length;
CCTextObjc *obj = [[CCTextObjc alloc] init];
obj.text = addText;
obj.textViewText = currentText;
obj.range = rg;
obj.op = obj.range.location;
[self setMyObjectTitle:obj];
}
@end
@implementation CCTextObjc
@end
ios NSUndoManager textView 撤销 反撤销
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1. Android Studio3.0.1的SVN插件图标和撤销反撤销图标 2. Android Studio升...
- 宝马男追砍电动车主遭反杀的事件近日引发热议,今天,昆山公安通报称:电动车主于海明的行为属于正当防卫,不负刑事责任,...
- 一、地理位置编码反编码项目:0601Gecoder导入库 编码/反编码步骤:1.创建地理编码器2.执行编码/反编码...