Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSMutableAttributedString: How to delete last character programmatically on iOS?

I have a custom keyboard and I have to perform backspace action on textview having nsmutableattributed string as a text (combination of character and nstextattachments)

like image 429
Alok Singh Avatar asked May 26 '15 20:05

Alok Singh


2 Answers

As per Moxy: if your last character is large enough not to fit within a single UTF-16 unit then Dave's method will just truncate the description of that character. Emojis are an example but there are a bunch more.

So you'll more likely want:

[string deleteCharactersInRange:
    [string.string rangeOfComposedCharacterSequenceAtIndex:string.length - 1]]
like image 169
Tommy Avatar answered Nov 20 '22 12:11

Tommy


Swift 4

mutableAttributedString.deleteCharacters(in: NSRange(location:(mutableAttributedString.length) - 1,length:1))
like image 25
Yucel Bayram Avatar answered Nov 20 '22 13:11

Yucel Bayram