iOS键盘删除健删除表情

1,判断要删除的是否是表情

- (BOOL)stringContainsEmoji:(NSString *)string
{
    NSMutableArray *arr = [NSMutableArray array];
    [string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {

        [arr addObject:substring];
    }];

    NSString *strin = [arr lastObject];
    if (strin.length ==2) {
        return YES;
    }
        return NO;
}

2,传入输入框字符串,调取是否含表情的方法

    BOOL isok = [self stringContainsEmoji:self.toolBar.contentText];
    if (isok) {
        range = NSMakeRange(self.toolBar.contentText.length-2, 2);
    }
    [self.toolBar deleteText:range];

3,如果是表情,调取删除方法

- (void)deleteText:(NSRange)range
{
    NSString *text = self.contentText;
    if (range.location + range.length <= [text length]
        && range.location != NSNotFound && range.length != 0)
    {
        NSString *newText = [text stringByReplacingCharactersInRange:range withString:@""];
        NSRange newSelectRange = NSMakeRange(range.location, 0);
        [self.inputTextView setText:newText];
        self.inputTextView.selectedRange = newSelectRange;
    }
}
展开阅读全文

没有更多推荐了,返回首页