目标C语言 需要一个方法来代替nssmutableattributedstring的数量与NSString的stringb



NSMutableAttributedString是否有类似于stringByReplacingOccurrencesOfString:替代NSString的方法?

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

我需要对NSMutableAttributedString使用相同的方法。(我需要用图像替换字符串)。

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];

这种方式只替换一次。

replaceCharactersInRange:withAttributedString:是正确的方法,你只需要找到你想要替换的范围。

为此,使用带属性字符串的string方法,然后使用rangeOf...方法之一来找到您想要替换的适当范围。

同样,你的例子显示你只是调用[[NSMutableAttributedString alloc] init],然后replaceCharactersInRange:withAttributedString:,但你显然应该有一些东西在可变字符串之前,你试图替换它。

相关内容

  • 没有找到相关文章

最新更新