我想爲UIWebView
創建一個UILabel
鏈接。我怎樣才能使UILabel
作爲超鏈接工作?Objective-C UILabel as HyperLink
回答
你可以去UITapGestureRecognizer,將工作類似的是你想要的東西 -
UILabel *myLabel = [[UILabel alloc] initWithFrame:];//Define label here as per needs
myLabel.userInteractionEnabled = YES;
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myAction:)];
[myLabel addGestureRecognizer:gr];
gr.numberOfTapsRequired = 1;
gr.cancelsTouchesInView = NO;
[self.view addSubview:myLabel];
現在採取行動 -
- (void) myAction: (UITapGestureRecognizer *) gr {
// Define actions here
}
一種可能的解決方案是使用UIButton
和UIButtonTypeCustom
而不是UILabel
。它看起來像一個標籤,可點擊。
您也可以考慮第三方實施,例如three20和Fancy UILabels的TTLabel。
謝謝你的幫助。 :) –
你只要檢查這個鏈接,這將有助於you.try一次,並告訴我們,如果它對你有幫助。
how to make a specific word touchable for its meaning in a text?
謝謝。這很有幫助。 –
再次感謝您。現在我想我可以用多種方式做UILabel/Hyperlink ..我發現rishi的答案更容易用作初學者。 –
你知道,應用觸摸和手勢是最簡單的方法。好的,因爲你的願望只是爲了獲得幫助而不是爲了你的聲望。 – vishiphone
我發現一些代碼,超鏈接UILabe。 ..希望這會有所幫助: 來源:http://sickprogrammersarea.blogspot.in/2014/03/adding-links-to-uilabel.html
#import "ViewController.h"
#import "TTTAttributedLabel.h"
@interface ViewController()
@end
@implementation ViewController
{
UITextField *loc;
TTTAttributedLabel *data;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(5, 20, 80, 25) ];
[lbl setText:@"Text:"];
[lbl setFont:[UIFont fontWithName:@"Verdana" size:16]];
[lbl setTextColor:[UIColor grayColor]];
loc=[[UITextField alloc] initWithFrame:CGRectMake(4, 20, 300, 30)];
//loc.backgroundColor = [UIColor grayColor];
loc.borderStyle=UITextBorderStyleRoundedRect;
loc.clearButtonMode=UITextFieldViewModeWhileEditing;
//[loc setText:@"Enter Location"];
loc.clearsOnInsertion = YES;
loc.leftView=lbl;
loc.leftViewMode=UITextFieldViewModeAlways;
[loc setDelegate:self];
[self.view addSubview:loc];
[loc setRightViewMode:UITextFieldViewModeAlways];
CGRect frameimg = CGRectMake(110, 70, 70,30);
UIButton *srchButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
srchButton.frame=frameimg;
[srchButton setTitle:@"Go" forState:UIControlStateNormal];
[srchButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
srchButton.backgroundColor=[UIColor clearColor];
[srchButton addTarget:self action:@selector(go:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:srchButton];
data = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(5, 120,self.view.frame.size.width,200) ];
[data setFont:[UIFont fontWithName:@"Verdana" size:16]];
[data setTextColor:[UIColor blackColor]];
data.numberOfLines=0;
data.delegate = self;
data.enabledTextCheckingTypes=NSTextCheckingTypeLink|NSTextCheckingTypePhoneNumber;
[self.view addSubview:data];
}
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url
{
NSString *val=[[NSString alloc]initWithFormat:@"%@",url];
if ([[url scheme] hasPrefix:@"mailto"]) {
NSLog(@" mail URL Selected : %@",url);
MFMailComposeViewController *comp=[[MFMailComposeViewController alloc]init];
[comp setMailComposeDelegate:self];
if([MFMailComposeViewController canSendMail])
{
NSString *recp=[[val substringToIndex:[val length]] substringFromIndex:7];
NSLog(@"Recept : %@",recp);
[comp setToRecipients:[NSArray arrayWithObjects:recp, nil]];
[comp setSubject:@"From my app"];
[comp setMessageBody:@"Hello bro" isHTML:NO];
[comp setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentViewController:comp animated:YES completion:nil];
}
}
else{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:val]];
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
if(error)
{
UIAlertView *alrt=[[UIAlertView alloc]initWithTitle:@"Erorr" message:@"Some error occureed" delegate:nil cancelButtonTitle:@"" otherButtonTitles:nil, nil];
[alrt show];
[self dismissViewControllerAnimated:YES completion:nil];
}
else{
[self dismissViewControllerAnimated:YES completion:nil];
}
}
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithPhoneNumber:(NSString *)phoneNumber
{
NSLog(@"Phone Number Selected : %@",phoneNumber);
UIDevice *device = [UIDevice currentDevice];
if ([[device model] isEqualToString:@"iPhone"]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phoneNumber]]];
} else {
UIAlertView *Notpermitted=[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Your device doesn't support this feature." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[Notpermitted show];
}
}
-(void)go:(id)sender
{
[data setText:loc.text];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"Reached");
[loc resignFirstResponder];
}
- 1. 做一個HMTL <a href> hyperlink not appear as a hyperlink
- 2. iOS @IBDesignable autorefresh UILabel params as title,font,color
- 3. 問題與HYPERLINK
- 4. Dynamic Hyperlink
- 5. Hyperlink Equivalent
- 6. Vlookup with Hyperlink - VBA
- 7. 從HYPERLINK運行VBA()
- 8. Excell Hyperlink接受「&」
- 9. GWT Wrap Hyperlink
- 10. Hyperlink vs按鈕
- 11. TextBlock HyperLink和Anchor?
- 12. HyperLink問題
- 13. Pear HTML BBcode Hyperlink
- 14. NumberFormatter - ObjectiveC
- 15. HyperLink點擊問題
- 16. dataTable row into hyperlink error
- 17. Swift:import ObjectiveC vs Foundation
- 18. ObjectiveC中的MulticastDelegate
- 19. 中的ObjectiveC
- 20. 在的ObjectiveC
- 21. 在的ObjectiveC
- 22. JSON ObjectiveC - 錯誤
- 23. Curl,Asana&ObjectiveC
- 24. 在的ObjectiveC
- 25. 在的ObjectiveC
- 26. Javascript - Div Box作爲HyperLink +嵌套文本HyperLink - 可能嗎?
- 27. HyperLink控件產生System.NullReferenceException
- 28. ObjectiveC隨機數字
- 29. 突破在的ObjectiveC
- 30. ObjectiveC項目組織
這實際上是我想要做的。謝謝。 –