textview's Skills
2017-09-07 本文已影响22人
goyohol
[A].禁用 拷贝、编辑
定义:
UITextView * tv = [[UITextView alloc] initWithFrame:SCREEN_Bounds];
tv.editable = NO; //不可编辑
[ 2 ].不可拷贝,不可编辑
tv.delegate = self; tv.editable = YES; //可编辑 #pragma mark - UITextViewDelegate -(BOOL)textViewShouldBeginEditing:(UITextView *)textView { return NO; //开始编辑就停止 }
[B].键盘遮挡住 TextView (⛔️报错⛔️)
报错:
2017-08-11 16:33:33.293831 cuteKids[10465:317797] 0x60800014d7e0 Copy matching assets reply: XPC_TYPE_DICTIONARY <dictionary: 0x60800014d7e0> { count = 1, transaction: 0, voucher = 0x0, contents = "Result" => <int64: 0x60800003e260>: 29 } 2017-08-11 16:33:33.294494 cuteKids[10465:317797] 0x60800014c8c0 Daemon configuration query reply: XPC_TYPE_DICTIONARY <dictionary: 0x60800014c8c0> { count = 2, transaction: 0, voucher = 0x0, contents = "Dictionary" => <dictionary: 0x60800014c130> { count = 1, transaction: 0, voucher = 0x0, contents = "ServerURL" => <dictionary: 0x60800014daa0> { count = 3, transaction: 0, voucher = 0x0, contents = "com.apple.CFURL.magic" => <uuid: 0x608000249f30> C3853DCC-9776-4114-B6C1-FD9F51944A6D "com.apple.CFURL.string" => <string: 0x60800024bee0> { length = 30, contents = "https://mesu.apple.com/assets/" } "com.apple.CFURL.base" => <null: 0x10ac19f20>: null-object } } "Result" => <int64: 0x608000037fe0>: 0 } 2017-08-11 16:33:33.294780 cuteKids[10465:317797] [MobileAssetError:29] Unable to copy asset information from https://mesu.apple.com/assets/ for asset type com.apple.MobileAsset.TextInput.SpellChecker
原因:
由于键盘会遮盖住textview,所以需要实现键盘弹出修改scrollview的位置。
解决:
_mytextView.autocorrectionType = UITextAutocorrectionTypeNo; //⭐️必须添加⭐️ _mytextView.spellCheckingType = UITextSpellCheckingTypeNo;
[C].禁用Emoji (输入忽略)
UITextField:
_placeTF = [[UITextField alloc] init];
_placeTF.placeholder = @"请输入活动地点";
_placeTF.clearButtonMode = UITextFieldViewModeWhileEditing;
_placeTF.delegate = self;
//监听UITextField的编辑状态
[_placeTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
- (void)textFieldDidChange:(UITextField *)textField {
//正则表达式
NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]" options:0 error:nil];
NSString *noEmojiStr = [regularExpression stringByReplacingMatchesInString:textField.text options:0 range:NSMakeRange(0, textField.text.length) withTemplate:@""];
if (![noEmojiStr isEqualToString:textField.text]) {
textField.text = noEmojiStr;
}
}
UITextView:
_detailTV = [[UITextView alloc] init];
_detailTV.scrollEnabled = NO;
_detailTV.bounces = NO;
_detailTV.font = [UIFont systemFontOfSize:17.f];
_detailTV.delegate = self;
#pragma mark - UITextViewDelegate (协议方法)
-(void)textViewDidChange:(UITextView *)textView{ //编辑状态改变
//正则表达式
NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]" options:0 error:nil];
NSString *noEmojiStr = [regularExpression stringByReplacingMatchesInString:textView.text options:0 range:NSMakeRange(0, textView.text.length) withTemplate:@""];
if (![noEmojiStr isEqualToString:textView.text]) {
textView.text = noEmojiStr;
}
}