Replace substring of NSAttributedString with another NSAttributedString

前端 未结 9 1167
野趣味
野趣味 2020-12-01 08:51

I want to replace a substring (e.g. @\"replace\") of an NSAttributedString with another NSAttributedString.

I am looking for a

9条回答
  •  广开言路
    2020-12-01 09:36

    In my case, the following way was the only (tested on 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];
        }
    

    Of course it will be nice to find another better way.

提交回复
热议问题