Я обнаружил, что когда я (как пользователь приложения) копирует богатый текст из UITextView в картотеку, картон содержит два типа:
"public.text",
"Apple Web Archive pasteboard type
Исходя из этого, я создал удобная категория на UIP борту. (С большим использованием кода из этот ответ ).
Он работает, но: преобразование в формат html означает, что я потеряю пользовательские атрибуты. Любое чистое решение будет с радостью принято.
Файл UIPasteboard + AttributedString.h:
@interface UIPasteboard (AttributedString)
- (void) setAttributedString:(NSAttributedString *)attributedString;
@end
Файл UIPastboard + AttributedString.m:
#import
#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, будьте моим гостем:)