NSAttributedString+EmojiExtension.m 1.0 KB

1234567891011121314151617181920212223242526272829
  1. //
  2. // Created by zorro on 15/3/7.
  3. // Copyright (c) 2015 tutuge. All rights reserved.
  4. //
  5. #import <UIKit/UIKit.h>
  6. #import "NSAttributedString+EmojiExtension.h"
  7. #import "EmojiTextAttachment.h"
  8. @implementation NSAttributedString (EmojiExtension)
  9. - (NSString *)getPlainString {
  10. NSMutableString *plainString = [NSMutableString stringWithString:self.string];
  11. __block NSUInteger base = 0;
  12. [self enumerateAttribute:NSAttachmentAttributeName inRange:NSMakeRange(0, self.length)
  13. options:0
  14. usingBlock:^(id value, NSRange range, BOOL *stop) {
  15. if (value && [value isKindOfClass:[EmojiTextAttachment class]]) {
  16. [plainString replaceCharactersInRange:NSMakeRange(range.location + base, range.length)
  17. withString:((EmojiTextAttachment *) value).emojiTag];
  18. base += ((EmojiTextAttachment *) value).emojiTag.length - 1;
  19. }
  20. }];
  21. return plainString;
  22. }
  23. @end