NSAttributedString+QMEmojiExtension.m 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. //
  2. // NSAttributedString+QMEmojiExtension.m
  3. // IMSDK-OC
  4. //
  5. // Created by haochongfeng on 2017/5/18.
  6. // Copyright © 2017年 HCF. All rights reserved.
  7. //
  8. #import "NSAttributedString+QMEmojiExtension.h"
  9. #import "QMTextAttachment.h"
  10. @implementation NSAttributedString (QMEmojiExtension)
  11. - (NSString *)getRichString {
  12. NSMutableString *plainString = [NSMutableString stringWithString:self.string];
  13. __block NSUInteger base = 0;
  14. [self enumerateAttribute:NSAttachmentAttributeName inRange:NSMakeRange(0, self.length)
  15. options:0
  16. usingBlock:^(id value, NSRange range, BOOL *stop) {
  17. if (value && [value isKindOfClass:[QMTextAttachment class]]) {
  18. [plainString replaceCharactersInRange:NSMakeRange(range.location + base, range.length)
  19. withString:((QMTextAttachment *) value).emojiCode];
  20. base += ((QMTextAttachment *) value).emojiCode.length - 1;
  21. }
  22. }];
  23. return plainString;
  24. }
  25. @end