iOS 7 之Airdrop 分享Plain text and attributed string data

接上一篇 先介绍分享文字的 地址点击打开链接


OpenStringViewController.mand modify it as shown below. Each line notedbelow should be added to the top of the appropriate method: 


  • -  (void)viewDidLoad{

    // Update object to be shared.

    self.objectsToShare=@[self.textView.text];

    ... the rest of the code ...

    }

  • -  (void)textViewDidBeginEditing:(UITextView*)textView{

    self.objectsToShare=@[textView.text];

    ... the rest of the code ...

    }

  • -  (BOOL)textView:(UITextView*)textViewshouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
    {

    self.objectsToShare=@[textView.text];

    ... the rest of the code ...

    }

- (void)textViewDidEndEditing:(UITextView*)textView{

self.objectsToShare=@[textView.text];

... the rest of the code ...


All you are doing here is setting and updatingobjectsToShareon the superclass incritical places. The object you’ll share is simply theNSStringtext content of thetext view.

Build and run your app; enter some text in theStringtab and try to send it viaAirDrop with another device. When the transfer completes, the receiving device willopen the Notes.app, and save the transferred text as a new note, as shown below: 


OpenAttributedStringController.mand modify the following methods asindicated below: 


  • -  (void)viewDidLoad{

    ... add this before [super viewDidLoad] ...

    self.objectsToShare=@[self.textView.attributedText];[superviewDidLoad];

    }

  • -  (void)textViewDidBeginEditing:(UITextView*)textView{

    ... the rest of the code ...

    self.objectsToShare=@[textView.attributedText];}

  • -  (BOOL)textView:(UITextView*)textViewshouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
    {

    ... before return YES ...

    self.objectsToShare=@[attrStr];

    return YES;}

- (void)textViewDidEndEditing:(UITextView*)textView{

self.objectsToShare=@[textView.attributedText];

... the rest of the code ...

}

page561image19504

The changes here are similar to the code for plain text, except you’re using theattributedTextproperty as the object to send via AirDrop.

Build and run your app again; type some text into the Attributed tab and be sure toinclude the name of a color or two to exercise the text highlighting. The apprecognizes red, blue, green, purple, brownandyellowin its current state. Feelfree to explore the code and add support for more colors




注:

 AirDrop does not fully supportNSAttributedString. That means on the receiving device you will get a plaintext, which by default opens inNotes.app. If you need to transferattributions, you need to wrap your NSAttributedString in a custom object,save it with a custom file type, and pass the file URL to AirDrop. On thereceiving device, you need to register for your custom file type and take itfrom there. 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值