日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOStextField/textView在输入时限制emoji表情的输入

發布時間:2023/12/20 编程问答 57 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOStextField/textView在输入时限制emoji表情的输入 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

https://www.jianshu.com/p/5227e6aab4d4

2017.02.27 13:08* 字數 146 閱讀 6109評論 6喜歡 14

又遇到輸入框輸入表情的情況了,之前寫了一篇文章“UITextView/UITextField檢測并過濾Emoji表情符號”http://www.jianshu.com/p/90d68e7e5d53,但是總覺得那兩種方式都各有弊端,這次又遇到之后,仔細考慮了下之后,想到了用兩種方式組合在一起使用,測試結果暫時沒什么問題,在輸入時就限制了emoji表情輸入,完全符合需求。在此貼出代碼,如果有什么問題,歡迎指正!

@interface JQTestViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *textField;

@property (nonatomic, assign) NSInteger textLocation;//這里聲明一個全局屬性,用來記錄輸入位置

@end

?

- (void)viewDidLoad {

[super viewDidLoad];

self.textField.delegate = self;

[self.textField addTarget:self action:@selector(textFieldDidChanged:) forControlEvents:UIControlEventEditingChanged];//注意:textFied沒有textFieldDidChanged代理方法,但是有UITextFieldTextDidChangeNotification通知,這里添加通知方法,textView有textFieldDidChanged代理方法,下面用法一樣

}

?

#pragma mark-- UITextFieldDelegate

//在輸入時,調用下面那個方法來判斷輸入的字符串是否含有表情

- (void)textFieldDidChanged:(UITextField *)textField

{

? ? if (textField.text.length > 20) {

? ? ? ? textField.text = [textField.text substringToIndex:20];

? ? ? ? [self showMessage:@"不可超過20字!"];

? ? }else {

? ? ? ? if (self.textLocation == -1) {

? ? ? ? ? ? NSLog(@"輸入不含emoji表情");

? ? ? ? }else {

? ? ? ? ? ? NSLog(@"輸入含emoji表情");

? ? ? ? ? ? //截取emoji表情前

? ? ? ? ? ? textField.text = [textField.text substringToIndex:self.textLocation];

? ? ? ? }

? ? }

}

?

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

{

? ? NSLog(@"location-->>%lu",(unsigned long)range.location);

? ? NSLog(@"replacementString-->>%@",string);

? ? //禁止輸入emoji表情

? ? if ([self stringContainsEmoji:string]) {

? ? ? ? self.textLocation = range.location;

? ? }else {

? ? ? ? self.textLocation = -1;

? ? }

? ? return YES;

}

?

//表情符號的判斷

- (BOOL)stringContainsEmoji:(NSString *)string {

?? ?

? ? __block BOOL returnValue = NO;

?? ?

? ? [string enumerateSubstringsInRange:NSMakeRange(0, [string length])

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? options:NSStringEnumerationByComposedCharacterSequences

? ? ? ? ? ? ? ? ? ? ? ? ? ? usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? const unichar hs = [substring characterAtIndex:0];

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (0xd800 <= hs && hs <= 0xdbff) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (substring.length > 1) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? const unichar ls = [substring characterAtIndex:1];

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (0x1d000 <= uc && uc <= 0x1f77f) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? returnValue = YES;

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else if (substring.length > 1) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? const unichar ls = [substring characterAtIndex:1];

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (ls == 0x20e3) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? returnValue = YES;

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (0x2100 <= hs && hs <= 0x27ff) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? returnValue = YES;

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else if (0x2B05 <= hs && hs <= 0x2b07) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? returnValue = YES;

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else if (0x2934 <= hs && hs <= 0x2935) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? returnValue = YES;

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else if (0x3297 <= hs && hs <= 0x3299) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? returnValue = YES;

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? returnValue = YES;

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? ? ? }];

?? ?

? ? return returnValue;

}

轉載于:https://www.cnblogs.com/sundaysgarden/p/10312844.html

總結

以上是生活随笔為你收集整理的iOStextField/textView在输入时限制emoji表情的输入的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。