When the user selects some text in UITextView or UIWebView, UIMenuController is shown. Is there any way to access this selected text programmitically?
I need it because I want to add custom item to UIMenuController: 'Search' option which will be intended to search for selected text in database.
Is it possible without using 'Copy' item in order to copy the text to pasteboard and then getting it from UIPasteBoard with the next time? - I am not interested in such workaround.
What I did was to add this method to a UIWebView's category:
- (NSString *)selectedText {
return [self stringByEvaluatingJavaScriptFromString:#"window.getSelection().toString()"];
}
After that, I can use [webView selectedText] to access the current selection.
Did you have a look at Apple's documentation.You can use the MenuItems property provided to create your custom UIMenuController.Have a look at the image below
and follow this
link for details.
You can also have a look at the sample code provided by Apple to understand that feature.
http://developer.apple.com/library/ios/#samplecode/TableViewUpdates/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010139
In this sample code they have show "Email" in the UIMenuController.
So go ahead and code...All the best...
Cheers
To grab text from a PDF displayed in a UIWebView this will work
-(void)copiedString{
[[UIApplication sharedApplication] sendAction:#selector(copy:) to:nil from:self forEvent:nil];
NSString *copiedString = [UIPasteboard generalPasteboard].string;
NSLog(#"Copied String: %#",copiedString);
}
Related
I am importing all tags of a HTML page to UITextView. but i have few on click events like mail and phone in this HTML tags. how can i detect that on click event and open it in UIWebView.
suppose are you displaying String in UIWebView like bellow But First you must configure your webView outlet and connect proper Delegate then put below code in to ViewWillApear:-
[webview loadHTMLString:[NSString stringWithFormat:#"%# Have an enquiry for %#? Click hear and we will be in touch with you.* {margin:0;padding:0; font-family:Arial;font-size:15px;text-align:justify}", yourStringDiscription,YourHyperlinkWord] baseURL:nil];
in Above script this line:-
:[NSString stringWithFormat:#"<html><body text=\"#000000\">%# <br>Have an enquiry for %#? Click hear
in first %# is for String of contain and second %# for your Hyparlink
Now
you can get click event of this webViewdelegate method
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
//Got even here put breck point
return NO;
}
return YES;
}
you can got click event in above delegate hope you undastood what i am saying and hope its help's you
When you on click events, do you simply mean that you want to make things like the email address, phone # etc to be tappable, such that tapping a phone # would open the Phone app etc?
If so, have you tried simply setting the UITextView's properties in Interface Builder > Attributes Inspector.
See my image below:
My app will display some text, and I want to make the hyper-link be able to be clicked. I have some questions about this feature.
How do I parse the text to be aware this is a link?
Once a user click the link, I don't want the OS to switch to Safari and open the link, this is very bad because the user can not go back to my application. So I want to open the link within my application. As soon as the user click the link, my app will present a view modally to display the web content. Any advice would be appreciated.
You probably want to subclass UILabel. When you change the text, have it try to see if the text is a hyperlink, if it is, set it to enable user interaction and change the text color to blue. When the user taps on the link, send a message to the main view controller (Possibly through delegation) to open the link.
to display web content in your app, look into UIWebView.
If your text can be formatted as html with hyperlinks (<a> tags), you could use a UIWebView to display it.
The UIWebViewDelegate's webView:shouldStartLoadWithRequest:navigationType: method is called when a user taps a link. Usually you would make your controller implement this method, and set the UIWebView's delegate to the controller.
You're going to want to check out a Github project called LRLinkableLabel.
It will auto-detect any URLs that are inside the .text property.
You can use it like so:
LRLinkableLabel *label = [[LRLinkableLabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 20.0)];
label.delegate = self;
label.text = #"Check out http://dundermifflin.com to find some great paper deals!";
Then just make sure self implements this method:
- (void) linkableLabel:(LRLinkableLabel *)label clickedButton:(UIButton *)button forURL:(NSURL *)url {
[[UIApplication sharedApplication] openURL:url];
}
You can also use the linkColor and textColor properties to configure the appearance of the label. From this point you can use it just like any other UILabel.
Remember to set the delegate to nil when you're all done to make sure everything is all cleaned up.
Hope this helps.
I have a label on which I want to ensure that when user clicks the label, it opens a hyperlink attached with it in a webview.
Label and its hyperlink are fetched from an SQLite database.
Can you please let me know how to do that?
Give IBAction on your custome button
-(IBAction) goToLink
{
NSString *Links_name = #"Your link";
NSLog(#"Link : %#",Links_name);
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:Links_name]];
}
You can't since you can't pass an action event with Label.
So, for this you need to use Custom Button
or either the TextView using detection property of TextView.
USing of Custom Button is much easy then using TextView.
hope that will work... :)
I want to provide a copy button where the user can quickly copy the text from a UIWebView. When I hold my finger and move it to the edges, I have no problem selecting all of the text, and then hitting copy. Is there a way to programmatically do this?
I didn't try it, but this should work to get the already selected text:
NSString *webViewString = [webView stringByEvaluatingJavaScriptFromString:#"(function (){return window.getSelection().toString();})();"];
[[UIPasteboard generalPasteboard] setString:webViewString]; //Copy to pasteboard
NSString *pasteBoardString = [[UIPasteboard generalPasteboard] string]; //Paste from pasteboard
Just search for another javascript snippet to select all... You should find one very quickly.
You can get the text in the UIWebView with some javascript:
NSString *innerText = [webView stringByEvaluatingJavaScriptFromString:#"document.body.innerHTML"];
It will give you the HTML text, including tags and everything...
I am working on an iPhone app, and would like to be able to click on a link that is in UITableView. When the Link, and only the link is selected I want the Safari app to be opened to the selected link. Any suggestions on how to do this? Thanks so much!
There are multiple solutions to your problem.
If the links are the only object in the cell, then you could just make call the didSelectRowAtIndexPath:(NSIndexPath *)indexPath function of UITableView to gather the link from your array of table data, and then use
[[UIApplication sharedApplication] openURL:myURL];
to open the URL.
Alternatively you could create your own UITableCell subclass that contains a custom button (instead of a rounded rect button) that has no image or background (only text) so that it has the appearance of a link (you could even color the text blue, and underline it...). When the user clicks the button, your handler function would then call the same openURL function as above.
The above method works best if you have multiple items in each cell (which is why you would have to create a custom cell...
A naive approach would be to embed a tiny UIWebView into each cell. UIWebView has a delegate that lets you know when a link is clicked which you can implement to launch the Safari or navigate to a new controller hosting a full screen UIWebView.
This approach might be too resource intensive and I haven't tried it myself but if it does work it would offer a lot of flexibility. Would love to know the results if you try it.
To launch a link in safari use:
NSURL *url = [NSURL URLWithString:#"http://stackoverflow.com"];
if (![[UIApplication sharedApplication] openURL:url]) {
NSLog(#"%#%#", #"Failed to open url:", [url description]);
}
Is the link in its own row in the UITableView? If so, then you can handle it within didSelectRowAtIndexPath when the appropriate row is clicked.