UIPasteboard+YYText.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // UIPasteboard+YYText.m
  3. // YYText <https://github.com/ibireme/YYText>
  4. //
  5. // Created by ibireme on 15/4/2.
  6. // Copyright (c) 2015 ibireme.
  7. //
  8. // This source code is licensed under the MIT-style license found in the
  9. // LICENSE file in the root directory of this source tree.
  10. //
  11. #import "UIPasteboard+YYText.h"
  12. #import "NSAttributedString+YYText.h"
  13. #import <MobileCoreServices/MobileCoreServices.h>
  14. #if __has_include("YYImage.h")
  15. #import "YYImage.h"
  16. #define YYTextAnimatedImageAvailable 1
  17. #elif __has_include(<YYImage/YYImage.h>)
  18. #import <YYImage/YYImage.h>
  19. #define YYTextAnimatedImageAvailable 1
  20. #elif __has_include(<YYWebImage/YYImage.h>)
  21. #import <YYWebImage/YYImage.h>
  22. #define YYTextAnimatedImageAvailable 1
  23. #else
  24. #define YYTextAnimatedImageAvailable 0
  25. #endif
  26. // Dummy class for category
  27. @interface UIPasteboard_YYText : NSObject @end
  28. @implementation UIPasteboard_YYText @end
  29. NSString *const YYTextPasteboardTypeAttributedString = @"com.ibireme.NSAttributedString";
  30. NSString *const YYTextUTTypeWEBP = @"com.google.webp";
  31. @implementation UIPasteboard (YYText)
  32. - (void)setYy_PNGData:(NSData *)PNGData {
  33. [self setData:PNGData forPasteboardType:(id)kUTTypePNG];
  34. }
  35. - (NSData *)yy_PNGData {
  36. return [self dataForPasteboardType:(id)kUTTypePNG];
  37. }
  38. - (void)setYy_JPEGData:(NSData *)JPEGData {
  39. [self setData:JPEGData forPasteboardType:(id)kUTTypeJPEG];
  40. }
  41. - (NSData *)yy_JPEGData {
  42. return [self dataForPasteboardType:(id)kUTTypeJPEG];
  43. }
  44. - (void)setYy_GIFData:(NSData *)GIFData {
  45. [self setData:GIFData forPasteboardType:(id)kUTTypeGIF];
  46. }
  47. - (NSData *)yy_GIFData {
  48. return [self dataForPasteboardType:(id)kUTTypeGIF];
  49. }
  50. - (void)setYy_WEBPData:(NSData *)WEBPData {
  51. [self setData:WEBPData forPasteboardType:YYTextUTTypeWEBP];
  52. }
  53. - (NSData *)yy_WEBPData {
  54. return [self dataForPasteboardType:YYTextUTTypeWEBP];
  55. }
  56. - (void)setYy_ImageData:(NSData *)imageData {
  57. [self setData:imageData forPasteboardType:(id)kUTTypeImage];
  58. }
  59. - (NSData *)yy_ImageData {
  60. return [self dataForPasteboardType:(id)kUTTypeImage];
  61. }
  62. - (void)setYy_AttributedString:(NSAttributedString *)attributedString {
  63. self.string = [attributedString yy_plainTextForRange:NSMakeRange(0, attributedString.length)];
  64. NSData *data = [attributedString yy_archiveToData];
  65. if (data) {
  66. NSDictionary *item = @{YYTextPasteboardTypeAttributedString : data};
  67. [self addItems:@[item]];
  68. }
  69. [attributedString enumerateAttribute:YYTextAttachmentAttributeName inRange:NSMakeRange(0, attributedString.length) options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(YYTextAttachment *attachment, NSRange range, BOOL *stop) {
  70. // save image
  71. UIImage *simpleImage = nil;
  72. if ([attachment.content isKindOfClass:[UIImage class]]) {
  73. simpleImage = attachment.content;
  74. } else if ([attachment.content isKindOfClass:[UIImageView class]]) {
  75. simpleImage = ((UIImageView *)attachment.content).image;
  76. }
  77. if (simpleImage) {
  78. NSDictionary *item = @{@"com.apple.uikit.image" : simpleImage};
  79. [self addItems:@[item]];
  80. }
  81. #if YYTextAnimatedImageAvailable
  82. // save animated image
  83. if ([attachment.content isKindOfClass:[UIImageView class]]) {
  84. UIImageView *imageView = attachment.content;
  85. Class aniImageClass = NSClassFromString(@"YYImage");
  86. UIImage *image = imageView.image;
  87. if (aniImageClass && [image isKindOfClass:aniImageClass]) {
  88. NSData *data = [image valueForKey:@"animatedImageData"];
  89. NSNumber *type = [image valueForKey:@"animatedImageType"];
  90. if (data) {
  91. switch (type.unsignedIntegerValue) {
  92. case YYImageTypeGIF: {
  93. NSDictionary *item = @{(id)kUTTypeGIF : data};
  94. [self addItems:@[item]];
  95. } break;
  96. case YYImageTypePNG: { // APNG
  97. NSDictionary *item = @{(id)kUTTypePNG : data};
  98. [self addItems:@[item]];
  99. } break;
  100. case YYImageTypeWebP: {
  101. NSDictionary *item = @{(id)YYTextUTTypeWEBP : data};
  102. [self addItems:@[item]];
  103. } break;
  104. default: break;
  105. }
  106. }
  107. }
  108. }
  109. #endif
  110. }];
  111. }
  112. - (NSAttributedString *)yy_AttributedString {
  113. for (NSDictionary *items in self.items) {
  114. NSData *data = items[YYTextPasteboardTypeAttributedString];
  115. if (data) {
  116. return [NSAttributedString yy_unarchiveFromData:data];
  117. }
  118. }
  119. return nil;
  120. }
  121. @end