I have a text comins as " This is very nice #thing" ,I want that text contaning "#" would be on a different color and clicking on it should open a new View.
A:
If you use a UIWebView then you have html markup available to you. So you could do something like this:
NSString *html = @"This is a very nice <font color=\"green\">#thing</font>";
[myWebView loadHTMLString:html baseURL:[NSURL URLWithString: @"http://localhost"]];
Boiler Bill
2009-11-09 13:49:09
But as text are coming is random , than how to detect it at runtime ??
crystal
2009-11-10 04:57:30
I suppose you'd do a search and replace on the text to "detect at runtime"
wkw
2009-11-10 12:25:31
+1
A:
You need to use UIWebView and make the clickable items standard HTML anchors then monitor the link loading in UIWebViewDelegate method shouldStartLoadWithRequest
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL* u = [request URL];
if( [[u scheme] isEqualToString:@"showlicenses"] ) {
NSString *path = [[NSBundle mainBundle]
pathForResource:@"credits" ofType:@"html" inDirectory:@"help"];
LicensesWebviewController *vc = [[LicensesWebviewController alloc] initWithURL:[NSURL fileURLWithPath:path]];
vc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:vc animated:YES];
[vc release];
return NO; // DO NOT attempt to load URL
}
return YES; // if you want to allow the URL to load
}
You'll need to set the delegate on your UIWebView.
myWebView.delegate = self;
So anytime a link in my HTML page with the format: "showLicenses://blahblah" is tapped, I push a new view controller.
You can use any type of links you want, you just have to examine and trap the ones you want handled internally. e.g., "myscheme:///do/something/with/this/link"
wkw
2009-11-09 13:55:15
Well... that code provides an example, but [[LicensesWebviewController alloc] initWithURL:[NSURL fileURLWithPath:path]] is a ViewController in one of my apps. you have to write your own, or do whatever you want with the link.
wkw
2009-11-10 12:24:38