• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to get selected text in a WebView
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to get selected text in a WebView


  • Subject: Re: How to get selected text in a WebView
  • From: "Gerriet M. Denkmann" <email@hidden>
  • Date: Tue, 5 Oct 2010 07:54:07 +0700

On 5 Oct 2010, at 00:45, Keary Suska wrote:

> On Oct 4, 2010, at 1:15 AM, Gerriet M. Denkmann wrote:
>
>> I have a Webview, and I want to do something with the selected text.
>>
>> I tried:
>> DOMRange *dr = [ webView selectedDOMRange ];
>> NSString *m = [ dr markupString ];
>> this contains the selected characters, but buried in lots of markup language. But I just want the characters.
>>
>> NSString *s= [ dr stringRepresentation ];
>> just returns "Undefined".
>>
>> I could do [webView copy:nil ]; and then get the string from the general pasteboard, but I rather do not like to mess up the pasteboard - the user might have put something important there.
>
> The first thing I would try is use NSAttributedString to convert the HTML.

Well, the first thing I tried, was to remove all "<...>" from the markupString manually. It worked.
But I admit that your suggestion is certainly much better.

This seems to work:

DOMRange *ff = [ webView selectedDOMRange ];
NSString *marki = [ ff markupString];
NSData *data = [ marki dataUsingEncoding: NSUTF8StringEncoding ];
NSNumber *n = [ NSNumber numberWithUnsignedInteger: NSUTF8StringEncoding ];
NSDictionary *options = [ NSDictionary  dictionaryWithObject: n  forKey: NSCharacterEncodingDocumentOption ];
NSAttributedString *as = 	[ [ NSAttributedString alloc ] 	initWithHTML: 		data
															options: 			options
															documentAttributes: NULL
							];
NSString *selectedString = [ as string ];
[ as release ];

But the question remains:
Why does writeSelectionWithPasteboardTypes:toPasteboard: behave like a non-op ?

Anyway, thanks very much for your suggestion!


Kind regards,

Gerriet.

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >How to get selected text in a WebView (From: "Gerriet M. Denkmann" <email@hidden>)
 >Re: How to get selected text in a WebView (From: Keary Suska <email@hidden>)

  • Prev by Date: Re: allKeys and allValues
  • Next by Date: Re: How to get selected text in a WebView
  • Previous by thread: Re: How to get selected text in a WebView
  • Next by thread: Re: How to get selected text in a WebView
  • Index(es):
    • Date
    • Thread