2012-09-26 15 views
8

कैसे आप पेस्टबोर्ड में एक NSAttributedString कॉपी कर सकता हूँ, उपयोगकर्ता को चिपकाने के लिए (- (void)paste:(id)sender साथ, UIResponderStandardEditActions प्रोटोकॉल से) प्रोग्राम के रूप में चिपकाने के लिए अनुमति देने के लिए, या में।कॉपी NSAttributedString UIPasteBoard

मैंने कोशिश की:

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

लेकिन साथ इस दुर्घटना:

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

जो उम्मीद की जा करने के लिए है, क्योंकि NSAttributedString एक संपत्ति सूची मूल्य नहीं है।

यदि उपयोगकर्ता मेरे ऐप में पेस्टबोर्ड की सामग्री पेस्ट करता है, तो मैं जिम्मेदार स्ट्रिंग के सभी मानकों और कस्टम विशेषताओं को रखना चाहता हूं।

+0

UIPasteBoard और NSAttributedString में कुछ ऐसी जानकारियां दी बनाया मूल्यवान हैं: http://stackoverflow.com/ए/38211885/1054573 –

उत्तर

9

मैं ने पाया है कि जब मैं (आवेदन की एक उपयोगकर्ता के रूप में) पेस्टबोर्ड में एक 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 

केवल लागू किया सेटर। यदि आप गेटर लिखना चाहते हैं, और/या इसे गिटहब पर रखना चाहते हैं, तो मेरा अतिथि बनें :)

+0

+1, लेकिन यह सेटटर है, गेटटर नहीं;) –

+0

@ एच 2CO3 धन्यवाद :) अपडेट किया गया वें ई उत्तर – Guillaume

+0

@ थॉमस टेम्पेलमैन धन्यवाद, संपादित! – Guillaume

-1

ओएसएक्स में पेस्टबोर्ड प्रबंधक कई टेक्स्ट और छवि प्रकारों के बीच स्वतः रूपांतरित हो सकता है।

समृद्ध टेक्स्ट प्रकारों के लिए, आप आमतौर पर आरटीएफ को पेस्टबोर्ड में रखेंगे। आप एक जिम्मेदार स्ट्रिंग से आरटीएफ प्रतिनिधित्व बना सकते हैं, और इसके विपरीत। "NSAttributedString अनुप्रयोग किट जोड़ संदर्भ" देखें।

यदि आपके पास छवियां भी शामिल हैं, तो आरटीएफ स्वाद के बजाय आरटीएफडी का उपयोग करें।

मैं इन के लिए MIME प्रकार पता नहीं है (मैं कार्बन पेस्टबोर्ड एपीआई के लिए इस्तेमाल किया कर रहा हूँ, नहीं कोको एक), लेकिन आप यूटीआई, Pboard और माइम प्रकार के बीच UTType एपीआई का उपयोग कर बदल सकते हैं।

आरटीएफ के लिए यूटीआई आरटीएफडी के लिए "public.rtf" है, यह "com.apple.flat-rtfd" है।

+0

यह एक आईओएस सवाल है। – Guillaume

+0

ओह - वह छोटा "आईओएस" टैग अनदेखा करना आसान है। और आईओएस में आरटीएफ रूपांतरण नहीं बनाया गया है। बमर। –

+0

सहमत हुए। आम तौर पर, जब टैग इसे देखने का एकमात्र तरीका है, तो मैं इसे प्रश्न में स्पष्ट करता हूं। मैंने नहीं किया क्योंकि मैंने सोचा था कि यूआईपीएस्टबोर्ड पर्याप्त होगा। अधिक स्पष्ट नहीं होने के लिए खेद है। – Guillaume

8

@ एचटीएमएल का उपयोग करके गिलाउम का दृष्टिकोण मेरे लिए काम नहीं करता है (कम से कम आईओएस 7.1 बीटा 5 में)।

- (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}]; 
} 

स्विफ्ट 2:

सफाई घोल का पेस्ट बोर्ड में आरटीएफ (प्लस प्लेन फ़ॉलबैक) के रूप में NSAttributedStrings डालने के लिए है।3

public extension UIPasteboard { 
    public func set(attributedString: NSAttributedString?) { 

    guard let attributedString = attributedString else { 
     return 
    } 

    do { 
     let rtf = try attributedString.dataFromRange(NSMakeRange(0, attributedString.length), documentAttributes: [NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType]) 
     items = [[kUTTypeRTF as String: NSString(data: rtf, encoding: NSUTF8StringEncoding)!, kUTTypeUTF8PlainText as String: attributedString.string]] 

    } catch { 

    } 
    } 
} 

स्विफ्ट 3

import MobileCoreServices 
public extension UIPasteboard { 
    public func set(attributedString: NSAttributedString?) { 

    guard let attributedString = attributedString else { 
     return 
    } 

    do { 
     let rtf = try attributedString.data(from: NSMakeRange(0, attributedString.length), documentAttributes: [NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType]) 
     items = [[kUTTypeRTF as String: NSString(data: rtf, encoding: String.Encoding.utf8.rawValue)!, kUTTypeUTF8PlainText as String: attributedString.string]] 

    } catch { 

    } 
    } 
} 
0

यह काफी सरल है:,

#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]; 
संबंधित मुद्दे