@"replace"
の部分文字列(例)NSAttributedString
を別の。に置き換えたいNSAttributedString
。
NSString
'sforstringByReplacingOccurrencesOfString:withString:
と同等のメソッドを探していNSAttributedString
ます。
@"replace"
の部分文字列(例)NSAttributedString
を別の。に置き換えたいNSAttributedString
。
NSString
'sforstringByReplacingOccurrencesOfString:withString:
と同等のメソッドを探していNSAttributedString
ます。
属性付き文字列を のインスタンスに変換しますNSMutableAttributedString
。
変更可能な属性付き文字列にはmutableString
プロパティがあります。ドキュメントによると:
「レシーバーはこの文字列への変更を追跡し、その属性マッピングを最新に保ちます。」
したがって、結果の変更可能な文字列を使用して、replaceOccurrencesOfString:withString:options:range:
.
属性を保持しながら、NSMutableAttributedString の文字列を変更する方法は次のとおりです。
迅速:
// first we create a mutable copy of attributed text
let originalAttributedText = nameLabel.attributedText?.mutableCopy() as! NSMutableAttributedString
// then we replace text so easily
let newAttributedText = originalAttributedText.mutableString.setString("new text to replace")
目的 C:
NSMutableAttributedString *newAttrStr = [attribtedTxt.mutableString setString:@"new string"];
私の場合、次の方法が唯一でした(iOS9でテスト済み):
NSAttributedString *attributedString = ...;
NSAttributedString *anotherAttributedString = ...; //the string which will replace
while ([attributedString.mutableString containsString:@"replace"]) {
NSRange range = [attributedString.mutableString rangeOfString:@"replace"];
[attributedString replaceCharactersInRange:range withAttributedString:anotherAttributedString];
}
もちろん、別のより良い方法を見つけることは素晴らしいことです。
タグ内のテキストを太字にする<b>
必要がありました。ここで私が行ったこと:
- (NSAttributedString *)boldString:(NSString *)string {
UIFont *boldFont = [UIFont boldSystemFontOfSize:14];
NSMutableAttributedString *attributedDescription = [[NSMutableAttributedString alloc] initWithString:string];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@".*?<b>(.*?)<\\/b>.*?" options:NSRegularExpressionCaseInsensitive error:NULL];
NSArray *myArray = [regex matchesInString:string options:0 range:NSMakeRange(0, string.length)] ;
for (NSTextCheckingResult *match in myArray) {
NSRange matchRange = [match rangeAtIndex:1];
[attributedDescription addAttribute:NSFontAttributeName value:boldFont range:matchRange];
}
while ([attributedDescription.string containsString:@"<b>"] || [attributedDescription.string containsString:@"</b>"]) {
NSRange rangeOfTag = [attributedDescription.string rangeOfString:@"<b>"];
[attributedDescription replaceCharactersInRange:rangeOfTag withString:@""];
rangeOfTag = [attributedDescription.string rangeOfString:@"</b>"];
[attributedDescription replaceCharactersInRange:rangeOfTag withString:@""];
}
return attributedDescription;
}
NSMutableAttributedString *result = [[NSMutableAttributedString alloc] initWithString:@"I am a boy."];
[result addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, [result length])];
NSMutableAttributedString *replace = [[NSMutableAttributedString alloc] initWithString:@"a"];
[replace addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, [replace length])];
[result replaceCharactersInRange:NSMakeRange(5, [replace length]) withAttributedString:replace];