设为首页 加入收藏

TOP

【代码笔记】iOS-禁止输入表情符号
2017-10-13 10:17:02 】 浏览:6245
Tags:代码 笔记 iOS- 禁止 输入 表情 符号

一,效果图。

二,工程图。

三,代码。

RootViewController.h

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController
<UITextViewDelegate>

@end

 

RootViewController.m

复制代码
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //初始化背景
    [self addView];
}
#pragma -mark -functions
-(void)addView
{
    UITextView *textView=[[UITextView alloc]initWithFrame:CGRectMake(50, 100, 200, 50)];
    textView.backgroundColor=[UIColor redColor];
    textView.delegate=self;
    [self.view addSubview:textView];
}
#pragma -mark -UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView
{
    NSRange textRange = [textView selectedRange];
    [textView setText:[self disable_emoji:[textView text]]];
    [textView setSelectedRange:textRange];
}
//禁止输入表情
- (NSString *)disable_emoji:(NSString *)text
{
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]" options:NSRegularExpressionCaseInsensitive error:nil];
    NSString *modifiedString = [regex stringByReplacingMatchesInString:text
                                                               options:0
                                                                 range:NSMakeRange(0, [text length])
                                                          withTemplate:@""];
    return modifiedString;
}
复制代码

 

 

 
 
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇iOS开发 tableView点击下拉扩展 +.. 下一篇swift 闭包循环引用

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目