64

@"replace"の部分文字列(例)NSAttributedStringを別の。に置き換えたいNSAttributedString

NSString'sforstringByReplacingOccurrencesOfString:withString:と同等のメソッドを探していNSAttributedStringます。

4

10 に答える 10

84
  1. 属性付き文字列を のインスタンスに変換しますNSMutableAttributedString

  2. 変更可能な属性付き文字列にはmutableStringプロパティがあります。ドキュメントによると:

    「レシーバーはこの文字列への変更を追跡し、その属性マッピングを最新に保ちます。」

    したがって、結果の変更可能な文字列を使用して、replaceOccurrencesOfString:withString:options:range:.

于 2011-11-22T18:05:45.957 に答える
20

属性を保持しながら、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"];
于 2013-06-05T14:47:05.707 に答える
20

私の場合、次の方法が唯一でした(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];
    }

もちろん、別のより良い方法を見つけることは素晴らしいことです。

于 2016-03-25T08:30:04.367 に答える
6

タグ内のテキストを太字にする<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;
}
于 2015-11-24T12:06:10.450 に答える
4
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];
于 2014-04-12T11:15:11.637 に答える