(アプリケーションのユーザーとして) リッチ テキストを UITextView からペーストボードにコピーすると、ペーストボードに次の 2 つのタイプが含まれていることがわかりました。
"public.text",
"Apple Web Archive pasteboard type
それを踏まえて、UIPasteboardに便利なカテゴリを作りました。(この回答
のコードを多用して)。
動作しますが、
html 形式に変換すると、カスタム属性が失われます。クリーンなソリューションは喜んで受け入れられます。
ファイル 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
セッターのみ実装。ゲッターを作成したい場合、および/またはGitHubに配置したい場合は、私のゲストになってください:)