NSMutableAttributedString:如何以编程方式删除 iOS 上的最后一个字符?

NSMutableAttributedString: How to delete last character programmatically on iOS?

我有一个自定义键盘,我必须对具有 nsmutableattributed 字符串作为文本(字符和 nstextattachments 的组合)的文本视图执行退格操作

根据 Moxy:如果您的最后一个字符足够大,无法容纳在单个 UTF-16 单元中,那么 Dave 的方法将截断该字符的描述。表情符号就是一个例子 but there are a bunch more

所以你更有可能想要:

[string deleteCharactersInRange:
    [string.string rangeOfComposedCharacterSequenceAtIndex:string.length - 1]]

Swift 4

mutableAttributedString.deleteCharacters(in: NSRange(location:(mutableAttributedString.length) - 1,length:1))