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:
,但你显然应该有一些东西在可变字符串之前,你试图替换它。