背景
项目开发中,有时候会遇到一些输入要做特殊限制或处理,比如禁止输入中文,禁止粘贴等,本来我们可以通过系统方法设置keyboardType限制输入类型,和 - (BOOL)canPerformAction:(SEL)action withSender:(id)sender实现禁止粘贴;可是坑点在于,有些用户下载了搜狗输入法,以上方法就不攻自破了
普及一下常用的系统键盘
1、限制不能输入汉字:UIKeyboardTypeASCIICapable
2、中文键盘:UIKeyboardTypeNamePhonePad
3、数字键盘:UIKeyboardTypeNumberPad
4、搜索网页键盘:UIKeyboardTypeWebSearch
5、邮箱键盘: UIKeyboardTypeEmailAddress
换个思路,没法控制键盘,控制输入框的内容也可以啊
UITextFiled和UITextView是我们常用的文本输入控件,控制其输入的方法就是代理方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
这两个方法中,每次文本框插入新的字符串都会调用,且插入的字符串我们可以获取,所以:
1、禁止输入中文,我们可以检测string或者text如果包含中文,就返回YES即可
2、禁止粘贴,先来普及一下系统粘贴板的知识
2.1 UIPasteboard类有3个初始化方法,如下:
//获取系统级别的剪切板
+ (UIPasteboard *)generalPasteboard;
//获取一个自定义的剪切板 name参数为此剪切板的名称 create参数用于设置当这个剪切板不存在时 是否进行创建
+ (nullable UIPasteboard *)pasteboardWithName:(NSString *)pasteboardName create:(BOOL)create;
//获取一个应用内可用的剪切板
+ (UIPasteboard *)pasteboardWithUniqueName;
2.2 获取剪切板上的内容
//获取或设置剪切板中的字符串数据
@property(nullable,nonatomic,copy) NSString *string;
//获取或设置剪切板中的字符串数组
@property(nullable,nonatomic,copy) NSArray<NSString *> *strings;
//获取或设置剪切板中的URL数据
@property(nullable,nonatomic,copy) NSURL *URL;
//获取或设置剪切板中的URL数组
@property(nullable,nonatomic,copy) NSArray<NSURL *> *URLs;
//获取或s何止剪切板中的图片数据
@property(nullable,nonatomic,copy) UIImage *image;
//获取或设置剪切板中的图片数组
@property(nullable,nonatomic,copy) NSArray<UIImage *> *images;
//获取或设置剪切板中的颜色数据
@property(nullable,nonatomic,copy) UIColor *color;
//获取或设置剪切板中的颜色数组
@property(nullable,nonatomic,copy) NSArray<UIColor *> *colors;
//部分代码参考
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
//action 会返回很多,想用哪个就写那个(action == @selector(cut:) )
return (action == @selector(copy:) || action == @selector(paste:) );
}
-(void)copy:(id)sender{
UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setImage:self.image];
if ([self.delegate respondsToSelector:@selector(transSomeTing:)]) {
[self.delegate transSomeTing:pasteboard.image];
NSLog(@"%@",self.image);
}
NSLog(@"您点击的是拷贝%@",pasteboard.items);
}
-(void)paste:(id)sender{
UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
UIImage *image = [pasteboard image];
if (image) {
self.image = image;
}
NSLog(@"您点击的是粘贴");
}
- (void)cut:(id)sender {
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setImage:self.image];
NSLog(@"您点击的是剪切");
}
- (void)select:(id)sender {
NSLog(@"您点击的是选择");
}
-(void)selectAll:(id)sender {
NSLog(@"您点击的是全选");
}
3、禁止粘贴,第三方键盘有自己的粘贴按钮,不会走canPerformAction方法,同样我们也是在textView或者textField的代理方法中做拦截, 如果要插入的字符串和粘贴板上的字符串一样,就说明插入的内容来自剪切板,返回NO
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if ([text isEqualToString:[[UIPasteboard generalPasteboard]string]]) {
[SVProgressHUD showInfoWithStatus:@"禁止粘贴"];
return NO;
}
if ([ZKHelper includeChinese:text]) {
[SVProgressHUD showInfoWithStatus:@"该题型只支持输入英文"];
return NO;
}
return YES;
}
//是否含有中文
+ (BOOL)includeChinese:(NSString*)str
{
for(int i=0; i< [str length];i++)
{
int a =[str characterAtIndex:i];
// || (a >= 48 && a <= 57)
if( (a >0x4e00&& a <0x9fff)){
return YES;
}
}
return NO;
}
网友评论