मैं ने पाया है कि जब मैं (आवेदन की एक उपयोगकर्ता के रूप में) पेस्टबोर्ड में एक UITextView से अमीर पाठ की प्रतिलिपि, पेस्टबोर्ड दो प्रकार के होते हैं: उस आधार पर
"public.text",
"Apple Web Archive pasteboard type
, मैं एक सुविधाजनक श्रेणी बनाई UIPasteboard पर।
(this answer से कोड के भारी उपयोग के साथ)।
यह काम करता है, लेकिन:
एचटीएमएल प्रारूप में रूपांतरण का मतलब है कि मैं कस्टम विशेषताओं को खो दूंगा। कोई भी साफ समाधान खुशी से स्वीकार किया जाएगा।
फ़ाइल UIPasteboard + AttributedString.h:
@interface UIPasteboard (AttributedString)
- (void) setAttributedString:(NSAttributedString *)attributedString;
@end
फ़ाइल UIPasteboard + AttributedString.m:
#import <MobileCoreServices/UTCoreTypes.h>
#import "UIPasteboard+AttributedString.h"
@implementation UIPasteboard (AttributedString)
- (void) setAttributedString:(NSAttributedString *)attributedString {
NSString *htmlString = [attributedString htmlString]; // This uses DTCoreText category NSAttributedString+HTML - https://github.com/Cocoanetics/DTCoreText
NSDictionary *resourceDictionary = @{ @"WebResourceData" : [htmlString dataUsingEncoding:NSUTF8StringEncoding],
@"WebResourceFrameName": @"",
@"WebResourceMIMEType" : @"text/html",
@"WebResourceTextEncodingName" : @"UTF-8",
@"WebResourceURL" : @"about:blank" };
NSDictionary *htmlItem = @{ (NSString *)kUTTypeText : [attributedString string],
@"Apple Web Archive pasteboard type" : @{ @"WebMainResource" : resourceDictionary } };
[self setItems:@[ htmlItem ]];
}
@end
केवल लागू किया सेटर। यदि आप गेटर लिखना चाहते हैं, और/या इसे गिटहब पर रखना चाहते हैं, तो मेरा अतिथि बनें :)
UIPasteBoard और NSAttributedString में कुछ ऐसी जानकारियां दी बनाया मूल्यवान हैं: http://stackoverflow.com/ए/38211885/1054573 –