How can I concatenate NSAttributedStrings?

前端 未结 8 1366
野性不改
野性不改 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;
    }
    

提交回复
热议问题