11

pasteboard に NSAttributedString をコピーして、ユーザーが貼り付けたり、プログラムで貼り付けたりできるようにするにはどうすればよいですか ( - (void)paste:(id)senderUIResponderStandardEditActions プロトコルから)。

私は試した:

UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
[pasteBoard setValue:attributedString forPasteboardType:(NSString *)kUTTypeRTF];

しかし、これは次のようにクラッシュします:

-[UIPasteboard setValue:forPasteboardType:]: value is not a valid property list type'

NSAttributedString はプロパティ リストの値ではないため、これは当然のことです。

ユーザーがペーストボードのコンテンツをアプリに貼り付ける場合、属性付き文字列のすべての標準とカスタム属性を保持したいと思います。

4

4 に答える 4

10

HTML を使用する代わりに、NSAttributedString を RTF (およびプレーンテキストのフォールバック) として貼り付けボードに挿入するのがクリーンな解決策です。

- (void)setAttributedString:(NSAttributedString *)attributedString {
    NSData *rtf = [attributedString dataFromRange:NSMakeRange(0, attributedString.length)
                               documentAttributes:@{NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType}
                                            error:nil];
    self.items = @[@{(id)kUTTypeRTF: [[NSString alloc] initWithData:rtf encoding:NSUTF8StringEncoding],
                     (id)kUTTypeUTF8PlainText: attributedString.string}];
}

スイフト5

import MobileCoreServices

public extension UIPasteboard {
    func set(attributedString: NSAttributedString) {
        do {
            let rtf = try attributedString.data(from: NSMakeRange(0, attributedString.length), documentAttributes: [NSAttributedString.DocumentAttributeKey.documentType: NSAttributedString.DocumentType.rtf])
            items = [[kUTTypeRTF as String: NSString(data: rtf, encoding: String.Encoding.utf8.rawValue)!, kUTTypeUTF8PlainText as String: attributedString.string]]
        } catch {
        }
    }
}
于 2014-02-20T15:19:48.120 に答える
10

(アプリケーションのユーザーとして) リッチ テキストを 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に配置したい場合は、私のゲストになってください:)

于 2012-09-26T13:45:30.087 に答える
1

それは非常に簡単です:

  #import <MobileCoreServices/UTCoreTypes.h>

  NSMutableDictionary *item = [[NSMutableDictionary alloc] init];

  NSData *rtf = [attributedString dataFromRange:NSMakeRange(0, attributedString.length)
                             documentAttributes:@{NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType}
                                          error:nil];

  if (rtf) {
    [item setObject:rtf forKey:(id)kUTTypeFlatRTFD];
  }

  [item setObject:attributedString.string forKey:(id)kUTTypeUTF8PlainText];

  UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
  pasteboard.items = @[item];
于 2016-11-17T06:05:51.863 に答える
-1

OSX のペーストボード マネージャは、多くのテキスト タイプとイメージ タイプの間で自動変換できます。

リッチ テキスト タイプの場合、通常は RTF をペーストボードに配置します。属性付き文字列から RTF 表現を作成でき、その逆も可能です。「NSAttributedString アプリケーション キット追加リファレンス」を参照してください。

画像も含まれている場合は、RTF フレーバーの代わりに RTFd を使用してください。

これらの MIME タイプはわかりません (Cocoa API ではなく、Carbon Pasteboard API に慣れています) が、UTType API を使用して、UTI、Pboard、および MIME タイプの間で変換できます。

RTF の UTI は「public.rtf」、RTFd の場合は「com.apple.flat-rtfd」です。

于 2013-03-11T21:53:37.867 に答える