UILabel with a hyperlink inside a UITableViewCell should open safari webbrowser? - iphone

I have a custom UITableViewCell with two labels (UILabel). The table cells are used to display information / text. Inside some of these cells (not all) there is text in this way set:
cell.myTextlabel.text = #"http://www.google.de"
Now I want if I click this text / link, a safari webbrowser should open this webpage. How can I do this?
Best Regards Tim.

Set userInteractionEnabled to YES of your label and add a gesture recognizer to it:
myLabel.userInteractionEnabled = YES;
UITapGestureRecognizer *gestureRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(openUrl:)];
gestureRec.numberOfTouchesRequired = 1;
gestureRec.numberOfTapsRequired = 1;
[myLabel addGestureRecognizer:gestureRec];
[gestureRec release];
Then implement the action method:
- (void)openUrl:(id)sender
{
UIGestureRecognizer *rec = (UIGestureRecognizer *)sender;
id hitLabel = [self.view hitTest:[rec locationInView:self.view] withEvent:UIEventTypeTouches];
if ([hitLabel isKindOfClass:[UILabel class]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:((UILabel *)hitLabel).text]];
}
}

If you use a UITextView instead of a UILabel it will automatically handle link detection. Set the view's dataDetectorTypes to UIDataDetectorTypeLink.

Inside your click event you can open safari browser by the following code
 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.google.com"]];

Related

iPhone sdk Tool Tip

Can i show a Tool tip like this:
Also, I want to show this tool tip when that area is pressed and HOLD. Is there a handler for this gesture?
You can do that in didSelectRowAtIndex method of TableViewController delegate method.
Look here. It is best implementation to have Popover controllers in iPhone. Download run and integrate into your code and change according to your requirement.
In addition to what #jennis said, there is indeed a way to capture long hold gestures you could use UILongPressGestureRecognizer
like this
UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longHold)];
[cell addGestureRecognizer:gesture];
and longHold method
- (void) longHold
{
//Cell has recieved gesture
}
It works, I agree with Omar Abdelhafith
-(void)viewWillAppear:(BOOL)animated
{ //gesture declared in .h file
gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longHold)];
[your view addGestureRecognizer:gesture];
}
-(void)longHlod
{
//do whatever you want
}

UILongPressGestureRecognizer stop handle without stop touching

I'm using UILongPressGestureRecognizer class to handle if one item is being selected.
The logic is as follows: User press during 1 second an item (UIView subclass). Once the gesture is detected, the item is highlighted and moveable.
The user must move this item across the screen without stop touching it.
The problem I'm facing is the gesture recognized shadows touchesBegan/Move/Ended necessary for the item class to arrange the movement.
I tried to remove the gesture recognized once is detected and the item selected. But still sending messages to the handle of gesture instead of call touches methods.
Anyone knows any way to stop "listening" the gesture recognizer without leave the finger of the screen?
Thanks.
Here the code:
-(void)addGestures
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleLongPress:)];
longPress.minimumPressDuration = iItemLongPressTime;
[self addGestureRecognizer:longPress];
[longPress release];
}
- (void)handleLongPress:(UILongPressGestureRecognizer*)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
NSLog(#"Long press Ended");
}
else {
if (self.isSelected) return;
if ([delegate respondsToSelector:#selector(singleTouch:)])
[delegate singleTouch:self];
[self removeGestureRecognizer:[self.gestureRecognizers objectAtIndex:0]];
NSLog(#"Long press detected.");
}
}
As you can see in the else branch the delegate calls enables all procedures to mark this item as selected, and just after remove the recognizers.
What I'm missing?
--EDIT--
Done! This works:
#pragma mark Gesture Functions
-(void)addGestures
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleLongPress:)];
longPress.minimumPressDuration = iItemLongPressTime;
[self addGestureRecognizer:longPress];
[longPress release];
}
- (void)handleLongPress:(UILongPressGestureRecognizer*)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
NSLog(#"Long press Ended");
}
else {
NSLog(#"Long press detected.");
if (self.isSelected) return;
if ([delegate respondsToSelector:#selector(singleTouch:)])
[delegate singleTouch:self];
[sender removeTarget:self action:#selector(handleLongPress:)];
sender.enabled = NO;
[self removeGestureRecognizer:sender];
}
}
Regards!
Does the custom UIView class have its own touch handling code? If not, a simple solution is to set the allowableMovement property of the UILongPressGestureRecognizer to CGFLOAT_MAX, or some big number, and use the gesture update callbacks to drag your custom view around. You can get the displacement using the - (CGPoint)locationInView:(UIView *)view method on the superview, and compare its position to when the recognizer began.
There are two solutions in my mind.
For animating uiview, please wrote a new class which is inherited from the UIView class and implement the touch delegates instead of writing the Gustures to handle animation(if the touch delegates are not triggering in the current class).
2.I have successfully removed the UILongPressGestureRecognizer after triggered it once.
Please refer the below code .ask me if you have any queries
Steps I have Done
I have added a UIView as "myView" to my main-view when main-view loads.
I have given the Tag to the myView (you can give 1,2,3…etc) to differentiate the tapped view from the main-view subviews.
Assigned the UILongPressGestureRecognizer gesture to myView and assigned target as "moveMe" method.
When user Pressed the myView long, the "moveMe" method will trigger.
Then I iterated the mainView Subviews with the condition Tag == 1
I have removed the UILongPressGestureRecognizer from the subview.As we can know that Tagged 1 main-view subView is myView.
So the NSLog(#"gesture removed"); and NSLog(#"moveMe"); will log in console only at one time.
The NSLog(#"touchesBegan"); will trigger first instead of triggering the "moveMe" method.
Then NSLog(#"touchesBegan"); will trigger always after removed the gesture . "moveMe" method will not trigger ever.
Code
- (void)viewDidLoad {
//Adding to UIView to main view when application is loading.
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 80, 80)];
myView.backgroundColor = [UIColor viewFlipsideBackgroundColor];
myView.tag = 1; //adding a tag to identify it.
//Adding Long Press Gesture to the UIView.
UILongPressGestureRecognizer *myGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(moveMe:)];
[myView addGestureRecognizer:myGesture];
[myGesture release];
myGesture = nil;
[self.view addSubview:myView];
[myView release];
myView = nil;
[super viewDidLoad];
}
//Method to trigger when user pressed long on the added UIView.
-(void)moveMe:(id)sender
{
for (UIView *subViews in [self.view subviews])
{
if (subViews.tag == 1) {
[subViews removeGestureRecognizer:sender];
NSLog(#"gesture removed");
}
}
NSLog(#"moveMe");
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesBegan");
}
or please refer Disable gesture recognizer iOS

How to set scroll position on uiwebview

I want to go to specify line on my uiwebview loaded.
I tried
[webView stringByEvaluatingJavaScriptFromString:#"window.scrollTo(0.0,
100.0)"];
but it's not working. My webview still start with top of the page.
I just wonder its because I load the UIWebview inside UIView.
Check my code below on my UIView:
- (void)loadView {
webview = [[WebViewDetail alloc]initWithFrame:[UIScreen mainScreen].applicationFrame]; //initialize a mainView is a UIWebVIew
webview.managedObjectContext = self.managedObjectContext;
webview.kitab = self.kitab;
webview.currentPasal = self.pasal;
self.title = [NSString stringWithFormat:#"%# %d", self.kitab, webview.currentPasal];
self.view=webview; //make the mainView as the view of this controller
}
and I put the scroll positioning script on my UIWebView:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
int height = [[webView stringByEvaluatingJavaScriptFromString:#"document.body.offsetHeight;"] intValue];
NSString* javascript = [NSString stringWithFormat:#"window.scrollBy(0, %d);", height];
[self stringByEvaluatingJavaScriptFromString:javascript];
}
in this case I want to put my uiwebview go to the bottom of the page when the view is loaded.
But it's totally not working, why?
Am I doing it wrong?
Try
webview.scrollView.contentOffset = CGPointMake(0, 100);
Make sure you set the view controller where you have your web view to be a UIWebViewDelegate, and declare this in the #interface of the header file (.h).
[yourWebView setDelegate:self];
Then, in the webViewDidFinishLoad: method, insert this code:
[yourWebView.scrollView scrollRectToVisible:CGRectMake(0, yourWebView.bounds.size.height + 100, 1, 1) animated:NO];

Hiding Number Pad in iPhone

How can I hide the Number Pad programmatically in iPhone?
As I have created a Registration Form and for the ZIP/Postal Code I have set keypad as NumberPad.
So on entering numbers I would like to hide the NumberPad?
//put in viewdidload.
-(void)viewDidLoad
{
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideKeyboard)];
[self.view addGestureRecognizer:gestureRecognizer];
[super.... ];
}
- (void) hideKeyboard
{
[textfieldname1 resignFirstResponder];
[textfieldname2 resignFirstResponder];
}
Send resignFirstResponder to the UI element to which the keyboard is linked (probably a UITextField/UITextView?).

Override swipe gesture on UITableView

I have a UITableView. I have added to this table a UIGestureRecognizer that looks for a swipe on a cell, and enables editing on that table if it detects a swipe. When I swipe right, it does indeed enable editing on the table. However, when I swipe left, I get the default red delete button appear on the right side of the cell. How can I disable this default behavior, and make it so that if I swipe left OR right, I get editing on either way?
- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
foldersTable.editing=YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
[foldersTable addGestureRecognizer:recognizer];
[recognizer release];
}
Just return UITableViewCellEditingStyleNone in your tableView:editingStyleForRowAtIndexPath: method.
There is a direction property on UISwipeGestureRecognizer. You can set that to both right and left swipes:
recognizer.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;
I am using another way to solve this problem.
NSMutableArray *tableViewGestures = [[NSMutableArray alloc]initWithArray:[self.tableView gestureRecognizers]];
[[tableViewGestures objectAtIndex:2] setDirection:UISwipeGestureRecognizerDirectionLeft];
NSLog(#"directions %#" , [tableViewGestures objectAtIndex:2] );
NSArray *newTableViewGestures = [[NSArray alloc]initWithArray:tableViewGestures];
[self.tableView setGestureRecognizers:newTableViewGestures];
I got the tableViewGestures NSArray and reset it.If I wanna use SwipeGestureRecognizerDirectionRigth,I just need to set the direction again.It's easy.Right?