NSString+TUIEmoji.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // NSString+TUIEmoji.h
  3. // TUIChat
  4. //
  5. // Created by harvy on 2021/11/15.
  6. // Copyright © 2023 Tencent. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import <UIKit/UIKit.h>
  10. #import "TIMDefine.h"
  11. NS_ASSUME_NONNULL_BEGIN
  12. #define kSplitStringResultKey @"result"
  13. #define kSplitStringTextKey @"text"
  14. #define kSplitStringTextIndexKey @"textIndex"
  15. @interface NSString (TUIEmoji)
  16. /**
  17. * Localize the emoji text in the current text and get the localized text
  18. * eg: The original text was @"你好, [大哭]"
  19. * - If it is currently in English, this method converts the text to @"Hello,[Cry]"
  20. * - If the current is Chinese, this method converts the text to @"你好,[大哭]"
  21. */
  22. - (NSString *)getLocalizableStringWithFaceContent;
  23. /**
  24. * Internationalize the emoji text in the current text and get the internationalized text. The internationalized text of the emoji is Chinese
  25. */
  26. - (NSString *)getInternationalStringWithfaceContent;
  27. /**
  28. *
  29. * Get the formatted emoticon text (after the image and text are mixed) The emoticon is stored in the NSTextAttachment object and cannot carry parameters
  30. */
  31. - (NSMutableAttributedString *)getFormatEmojiStringWithFont:(UIFont *)textFont
  32. emojiLocations:(nullable NSMutableArray<NSDictionary<NSValue *, NSAttributedString *> *> *)emojiLocations;
  33. /**
  34. *
  35. * Get the formatted emoji (after the image and text are mixed together) The emoji is stored in the TUIEmojiTextAttachment object, which can carry parameters.
  36. * For example: the original text is @"Hello,[cry]", then this method turns the text into @"Hello,😭"
  37. */
  38. - (NSMutableAttributedString *)getAdvancedFormatEmojiStringWithFont:(UIFont *)textFont
  39. textColor:(UIColor *)textColor
  40. emojiLocations:(nullable NSMutableArray<NSDictionary<NSValue *, NSAttributedString *> *> *)emojiLocations;
  41. - (NSString *)getEmojiImagePath;
  42. - (UIImage *)getEmojiImage;
  43. /**
  44. * Split string using both emoji and @user. For instance,
  45. * Origin string is @"hello[Grin]world, @user1 see you!", and users is @[@"user1"];
  46. * Return value is:
  47. * @{
  48. * kSplitStringResultKey: @[@"hello", @"[Grin]", @"world, ", @"user1 ", @"see you!"],
  49. * kSplitStringTextKey: @[@"hello", @"world, ", @"see you!"],
  50. * kSplitStringTextIndexKey: @[@0, @2, @4]
  51. * }
  52. * kSplitStringResultKey's value contains all elements after spliting.
  53. * kSplitStringTextKey'value contains all text elements in the split result, excluding emojis and @user infos.
  54. * kSplitStringTextIndexKey'value contains the location of text in split result.
  55. */
  56. - (NSDictionary *)splitTextByEmojiAndAtUsers:(NSArray *_Nullable)users;
  57. /**
  58. * Replace the element in array, whose index is in index with the corresponding value in replaceDict.
  59. * For instance,
  60. * array is @[@"hello", @"[Grin]", @"world, ", @"user1 ", @"see you!"]
  61. * index is @[@0, @2, @4]
  62. * replaceDict is @{@"hello":@"你好", @"world":@"世界", @"see you!":@"再见!"}
  63. * Return value is @"你好[Grin]世界, @user1 再见!"
  64. */
  65. + (NSString *)replacedStringWithArray:(NSArray *)array index:(NSArray *)index replaceDict:(NSDictionary *)replaceDict;
  66. @end
  67. @interface NSAttributedString (EmojiExtension)
  68. /**
  69. * @"你好,😭"" -> @"你好,[大哭]"
  70. * @"Hello,😭" -> @"Hello,[Cry]"
  71. */
  72. - (NSString *)tui_getPlainString;
  73. @end
  74. NS_ASSUME_NONNULL_END