How to dynamically pass hyperlink URL to a label in iPhone SDK? - iphone

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... :)

Related

UIImagePicker add Caption

I can't test this code in the iphone simulator but I was wondering if this is the best way to add a caption to a photo someone has taken in the UIImagepickercontroller class.
- (void)imagePickerController:(UIImagePickerController *) Picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
_selectedImage.image = [info objectForKey:UIImagePickerControllerOriginalImage];
UITextField *caption= [[UITextField alloc] initWithFrame:CGRectMake( 60,640,200,40)];
caption.borderStyle = UITextBorderStyleRoundedRect;
caption.font = [UIFont systemFontOfSize:15];
caption.placeholder = #"Type Caption";
[caption release];
}
This is the flow I would take
Let user select / take a picture
Once user hits ok show a prompt to let them enter text.
Sample Project 1
Sample Projects 2
Create a UIImageView and add their selected picture to it
Create a UILabel and add it on top of the UIImageView
Add their text to UILabel.
If you need to save the image with text on top of it then you need some sort of custom photo editor code or just take a screenshot and save it.
The code you posted does not make much sense for what you are trying to do, since it does not look like you are actually linking the caption to the image in any way. I also do not know why you are releasing the text field - unless you turned Automatic Reference Counting off, that code should return an error.
What I would do (and did, to an extent, in a project of mine) is the following:
(1) Create some sort of object, like UserImg, which has two properties of type UIImage and NSString respectively.
(2) In the above function, when an image is selected, create a new UserImg and set the UIImage property to the select image.
(3) Display an UIAlertView with a text box and an okay and cancel button. Use the text box to get the caption you want the user to save with the image.
(4) Set up a function to listen to the UIAlertView for okay and cancel button presses. If the user cancels, remove the new UserImg. If the user hits okay, copy the input text to the UserImg's NSString property.
(5) Profit? I am not sure of the context of the image taking, so you'll have to figure out how you want to display things yourself.
You can test this functionality on the simulator as well. You just need to set the code up so that it picks images from the device's image folder if no camera is present. I forget exactly how to do this and do not currently have my source code on me. I'll edit the answer later to include that if you cannot figure it out yourself in that time.
You will also have to add images to the simulator to choose from. To do that, exit out of any apps running on the simulator (hit the home button, like you would on an actual device) and drag an image over on top of it. When you do, in the top right corner, there should be an option to save the image to the device.
EDIT:
I noticed the comment you added about putting the caption below the image. To do that, I would create a UserImgView which consisted of a UIImageView and a UILabel. That way, when displaying the image using the custom view, you can display the UIImage in its ImageView and the caption right below it in the label.
Best way would be to create a NSDictionary
NSMutableDictionary *photoCaptionDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:_selected.image,#"image",caption.text,#"caption",nil];
Whenever you want to show the picture you just do
[photoCaptionDict objectForKey:#"image"];
and
[photoCaptionDict objectForKey:#"caption"];
shows the text.

How to detect the user click a hyper-link

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.

Getting selected text when UIMenuController is shown

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);
}

Clickable links in UITableView that open up Safari

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.

How to navigate back to previous view after sending sms is done in iphone?

I am new to iphone development, i have created sms application using "Text Links(sms:) and i have created the action sheets and the buttons for email, sms. On clicking sms it navigates to the another view(UIView controller) and i wrote a code for SMS.And i faced some problem ,its not removed the view properly, it doesnt goes to the previous webview properly,(Action sheets loaded in webview)
Here my code and explanation,
I have created the custom send button programmatically for sending the sms.
I am getting phone numbers in a textfield and stores it to one string(phone) and i display the article title on the label, it worked dynamically.
now i want to share the article title through sms. and i have one problem, (ie)if i click send button it should go to the previous view(webview) but it display another view.. so i want to go to the previous page properly ,when i clicked the send button.
And i also want to display the address book(Eg,default address book in the device) in my sms view page.Please guide me.
-(void) sendbtnclk
{
self.String = txtfield.text;
NSString *phone = String;
NSString *arttitle = articleTitle;
NSString *sms = [NSString stringWithFormat:#"sms:%# %#",phone,arttitle];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:sms]];
[self.view removeFromSuperview];
}
thanks,
please help me out.
I think that when you use the openURL: method, the Text app is launched on the phone and thus the current app is dismissed.
Here is the relevant documentation.