0

Is there a replace method for NSMutableAttributedString similar to stringByReplacingOccurrencesOfString: for NSString?

NSString *newString = [myString stringByReplacingOccurrencesOfString:@"," withString:@""];

I need the same method for NSMutableAttributedString. (I need replace string with image).

NSMutableAttributedString *mutableAttString = [[NSMutableAttributedString alloc] init];

NSTextAttachment *attachment = [[NSTextAttachment alloc] init];     
attachment.image = [UIImage imageNamed:@"a.png"];

NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment]; 

[mutableAttString replaceCharactersInRange:getanswer     
                      withAttributedString:attachmentString];

this way only replace one time. i need quantities replace

libai
  • 195
  • 1
  • 2
  • 12

1 Answers1

0

replaceCharactersInRange:withAttributedString: is the correct method to use, you just need to find the range that you want to replace first.

To do that, use the string method of the attributed string and then one of the rangeOf... methods to find the appropriate range you want to replace.

Also, your example shows you just calling [[NSMutableAttributedString alloc] init] and then replaceCharactersInRange:withAttributedString:, but you should obviously have something in the mutable string before you try to replace it.

Wain
  • 118,658
  • 15
  • 128
  • 151