#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];
//監(jiān)聽textview文本改動(dòng)的通知
[[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;
//判斷當(dāng)前NSUndoManager的狀態(tài),是處于撤銷或者反撤銷的狀態(tài)
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 撤銷 反撤銷
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 1. Android Studio3.0.1的SVN插件圖標(biāo)和撤銷反撤銷圖標(biāo) 2. Android Studio升...
- 寶馬男追砍電動(dòng)車主遭反殺的事件近日引發(fā)熱議,今天,昆山公安通報(bào)稱:電動(dòng)車主于海明的行為屬于正當(dāng)防衛(wèi),不負(fù)刑事責(zé)任,...
- 一、地理位置編碼反編碼項(xiàng)目:0601Gecoder導(dǎo)入庫 編碼/反編碼步驟:1.創(chuàng)建地理編碼器2.執(zhí)行編碼/反編碼...