Replace substring of NSAttributedString with another NSAttributedString

前端 未结 9 1146
野趣味
野趣味 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:34

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

提交回复
热议问题