How can I concatenate NSAttributedStrings?

前端 未结 8 1357
野性不改
野性不改 2020-12-04 08:58

I need to search some strings and set some attributes prior to merging the strings, so having NSStrings -> Concatenate them -> Make NSAttributedString is not an option, is t

相关标签:
8条回答
  • 2020-12-04 09:53
    // Immutable approach
    // class method
    
    + (NSAttributedString *)stringByAppendingString:(NSAttributedString *)append toString:(NSAttributedString *)string {
      NSMutableAttributedString *result = [string mutableCopy];
      [result appendAttributedString:append];
      NSAttributedString *copy = [result copy];
      return copy;
    }
    
    //Instance method
    - (NSAttributedString *)stringByAppendingString:(NSAttributedString *)append {
      NSMutableAttributedString *result = [self mutableCopy];
      [result appendAttributedString:append];
      NSAttributedString *copy = [result copy];
      return copy;
    }
    
    0 讨论(0)
  • 2020-12-04 09:54

    Try this:

    NSMutableAttributedString* result = [astring1 mutableCopy];
    [result appendAttributedString:astring2];
    

    Where astring1 and astring2 are NSAttributedStrings.

    0 讨论(0)
提交回复
热议问题