ios - 向后使用自定义字符串

标签 ios objective-c nsstring

我为表情符号使用了一个字符串数组,如下所示:

NSArray *emoticons = @[@[微笑]",@[哭泣]",@[快乐]"...]

然后在 UITextView 中显示如下字符串:

我现在很开心[happy]现在[smile]

当我点击后退删除按钮时,如果最后一个字是表情符号,我想删除整个表情符号字符串,不仅仅是最后一个字符。

有什么想法吗?

最佳答案

试试这个,

NSString *string = self.textView.text;
__block NSString *deleteWord = nil;
__block NSRange rangeOfWord;

[string enumerateSubstringsInRange:NSMakeRange(0, self.textView.selectedRange.location + self.textView.selectedRange.length) options:NSStringEnumerationByWords | NSStringEnumerationReverse usingBlock:^(NSString *substring, NSRange subrange, NSRange enclosingRange, BOOL *stop) {
    deleteWord = substring;
    rangeOfWord = enclosingRange;
    *stop = YES;
}];


if ([emoticons containsObject:deleteWord]) {
    string = [string stringByReplacingCharactersInRange:rangeOfWord withString:@""];
    self.textView.text = string;
    self.textView.selectedRange = NSMakeRange(rangeOfWord.location, 0);
}

关于ios - 向后使用自定义字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23264999/

相关文章:

iphone - NSString 中的撇号和反撇号

ios - 无法识别的选择器发送到 UILabel 上的实例

ios - 从特定年份的给定月份获取每一天

ios - CLGeocoder 返回错误 : The operation couldn’t be completed.(kCLErrorDomain 错误 8。)

iphone - 在您的应用程序中打开附件时如何传递发件人电子邮件地址

iphone - 如何将字符串转换为日期?

iOS 蓝牙 scanForPeripheralsWithServices 没有结果

ios - 在实例化 UISwitcher 时尝试实例化 UILabel

objective-c - objective-c 访问已释放对象

ios - 长 UIButton 标题标签在中心而不是在右侧截断