TTStyledTextLabel - iphone

I'm trying to get this control to work with a hypertext link with not much success. I have looked at TTCalaog and tried to replecate but does not work.
I have this working as far as displaying the hypertext link but it does not fire.
TTStyledTextLabel* label = [[[TTStyledTextLabel alloc] initWithFrame:CGRectMake(5, 0, 315, 175)] autorelease];
NSString* labelText = #"This should work";
label.text = [TTStyledText textFromXHTML:labelText lineBreaks:NO URLs:YES];
[self.view addSubview:label];
I thing I am missing the point here perhaps with the placement of the google url? I have seen a post on this forum that makes use of custom-uri://some/url that is then set up in TTURLMap and TTNavigator, but I need to open a url from the hypertext in a webview so I need the url to run a method in my class that creates my webview controller etc.
I have tried to cusomise TTURLMap to work without a TTNavigator but completely pickled?
Any help gratefullt appreciated ;-)
Thanks

I've just found myself a solution to catch the clicked URL on a TTStyledTextLabel. I hope this could help in your case too.
This's what I have done.
1. Create TTNavigator
TTNavigator *navigator = [TTNavigator navigator];
navigator.persistenceMode = TTNavigatorPersistenceModeNone;
navigator.delegate = self;
2. Create TTNavigatorDelegate
As you assigned self as delegate of the navigator object. Therefore, please remember to add protocol in the header file .h before you continue.
In the implementation, create this method
- (BOOL) navigator:(TTBaseNavigator *)navigator shouldOpenURL:(NSURL *)URL {
// Now you can catch the clicked URL, and can do whatever with it
// For Example: In my case, I take the query of the URL
// If no query is available, let the app open the URL in Safari
// If there's query, get its value and process within the app
NSString *query = URL.query;
if (query == nil) {
return YES;
} else {
// process the query
}
}
I hope this helps! Please vote for me if this helps to solve your issue!
Best Regards,
Thang

Related

Initial text and paperclipped-URL for action in UIActivityViewController & UIActivityItemSource?

Finally been making it through Apple's (rather dismal) documentation on the new UIActivityViewController class and the UIActivityItemSource protocol, and I'm trying to send different data sets to different actions called from the activity view. To simplify things, I'm looking at two things.
A Facebook posting action, which should say "Check this out!" and also attach a URL to the post (with that cute little paperclip).
A Twitter posting action, which should say "Check this out, with #hashtag!" and also attach that same URL (with the same paperclip).
Here's the code I've got implemented right now.
- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType {
if ([activityType isEqualToString:UIActivityTypePostToFacebook]) {
return #"Check this out!";
} else if ([activityType isEqualToString:UIActivityTypePostToTwitter]) {
return #"Check this out, with #hashtag!";
}
return #"";
}
- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController {
return #"";
}
And then when I set up this activity view controller (it's in the same class), this is what I do.
UIActivityViewController *activityView = [[UIActivityViewController alloc] initWithActivityItems:#[self] applicationActivities:nil];
[self presentViewController:activityView animated:YES completion:nil];
My dilemma is how to attach that NSURL object. It's relatively easy when calling the iOS 6 SL-class posting modals; you just call the individual methods to attach a URL or an image. How would I go about doing this here?
I'll note that instead of returning NSString objects from -activityViewController:itemForActivityType, if I return just NSURL objects, they show up with that paperclip, with no body text in the post. If I return an array of those two items, nothing shows up at all.
Evidently it was as simple as this: passing in an array to the first argument of UIActivityViewController's init call, with each item in the array handling a different data type that will end up in the compose screen. self handles the text, and the second object (the NSURL) attaches the URL.
NSArray *items = #[self, [NSURL URLWithString:#"http://this-is-a-url.com"]];
UIActivityViewController *activityView = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
[self presentViewController:activityView animated:YES completion:nil];
Really wish there was more on this, but here it is.

Calculating UILabel and UIButtons sizes to make a sentence?

I'm trying to show in my app something like this:
Liked by #user1, #user2 and 3 more.
Many apps have this format either for comments like who posted it and their comment,
example: #me yeah apples are good
When you tap their name, it takes you somewhere in the app.
I would like #user1 #user2 and 3 more to be clickable and perform a selector.
I would also like them to be bold and a certain color.
UIWebView can stylize text but can I perform selectors by touching part of an attributed string?
I have been trying to find the best way to do this without making labels and buttons and calculating them dynamically by the length of each username like this:
Label = "Liked by "
Button = "#user1"
Label ", "
Button = "#user2"
Label "and "
Button "3 more"
Label "."
There must be a better way!
Thanks in advance.
You'd be better with an inline UIWebView -- provided you don't need to add this to a scroll view. You can detect actions/link clicks inside a webview by registering some delegate for them and then giving fake protocols/URLs as the link URL. Something like this:
UIWebView *wv = [[UIWebView alloc] initWithFrame:aFrame];
wv.delegate = self;
[wv loadHTMLString:#"<a href='userlike:user1'>#user1</a> hates this." baseURL:nil ]; // further parts of this method name omitted
// because it's long, look it up in UIWebView class reference.
[self.view addSubview:vw];
[wv release];
And the delegate method:
- (BOOL)webView:(UIWebView *)wv shouldStartLoadWithRequest:(NSURLRequest *)rq navigationType:(UIWebViewNavigationType)type
{
if ([[[rq URL] protocol] isEqualToString:#"userlike"])
{
NSString *userName = [[rq URL] host];
// extract username from the URL, then
return NO;
}
// else
return YES;
}
EDIT: I found this, exactly what you're looking for. (as it turns out, #regan also suggested the exact same thing.)
You can call selectors from the HTML code, see for example here and here.

entity nbsp not defined

I have been trying to load the url http://friscotxcoc.weblinkconnect.com/cwt/External/WCPages2/wcevents/eventsstartpage.aspx?oe=true&ce=true into a webview but it shows up an error 'Entity nbsp not found'.
The link works properly over Safari (Machine as well as simulator) but doesnt load properly when loaded through a webview. Can someone point me as to how to do it?
#define kEventsCalenderLink #"http://friscotxcoc.weblinkconnect.com/cwt/External/WCPages2/wcevents/eventsstartpage.aspx?oe=true&ce=true"
NSURL *eventsURL = [NSURL URLWithString:kEventsCalenderLink];
eventsWebView.delegate = self;
[eventsWebView loadRequest:[NSURLRequest requestWithURL:eventsURL]];
The server is serving that page with the Content-Type of "application/xhtml+xml" to the Simulator version of Safari. Safari is interpreting the page as strict XML, which does not have an entity.
Unfortunately, UIWebView manipulates its HTTP headers behind the scenes, and it's seemingly impossible to configure it to make a request that will cause your server to serve content as "text/html"
The one workaround I would suggest is to retrieve the content with an NSURLConnection, then feed it to your web view with the loadData:MIMEType:textEncodingName:baseURL: method, being sure to pass "text/html" as the MIME type.
This may also necessitate intercepting future requests via the webView:shouldStartLoadWithRequest:navigationType: delegate method (in order to cancel them and load them in the manner described above to prevent similar problems).
Or, if you have control of the server, you could configure it to serve HTML with the appropriate content type. Or convert the content to use proper XML entities.
Do it in this way:
NSString *kEventsCalenderLink #"http://friscotxcoc.weblinkconnect.com/cwt/External/WCPages2/wcevents/eventsstartpage.aspx?oe=true&ce=true"
NSLog(#"%#",kEventsCalenderLink);
eventsWebView.delegate = self;
eventsWebView.userInteractionEnabled = true;
[eventsWebView loadRequest:[[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:kEventsCalenderLink]]];
You have only defined the URL which you haven't mentioned that it's string type.
Here is the sample code which i implemented.Hope it would help you
- (void)loadView
{
UIView *vew=[[UIView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
self.view=vew;
//self.title=#"sanjay";
//self.navigationItem.title=#"afdsfasd";
self.view.backgroundColor=[UIColor redColor];
[vew release];
UIButton *smsbtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
smsbtn.frame=CGRectMake(50, 280, 100, 50);
[smsbtn setTitle:#"SMS APP" forState:UIControlStateNormal];
[smsbtn setTitleEdgeInsets:UIEdgeInsetsZero];
[smsbtn addTarget:self action:#selector(loadsms) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:smsbtn];
}
-(void)loadsms
{
NSString *url = [NSString stringWithFormat:#"http://friscotxcoc.weblinkconnect.com/cwt/External/WCPages2/wcevents/eventsstartpage.aspx?oe=true&ce=true"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: url]];
}
This code is working dude.Try this out.I have implemented this for you only.

understanding TTNavigator

following situation:
in a TTTableViewController i added some Cells with URLs.
they are opening a class with #"tt://photos" for example. this works quite fine.
the first thing is, i saw some urls in TT Examples like #"tt/photos/1". is it possible to fetch this "1" in my photos class and say, for example okay, please open picture one, ore is this only another URL that was declared in TTNavigatior to open a specific Class?
the other thing is: is it possible to forward an object to the linked class?
clicking a cell opens #"tt://photos" (the linked class in my TTNavigator)
working with normal tableviews i can overwrite my init method and send an object with my initialize method, is this also possible by clicking my TTItems?
thanks!
figured it out myself, for those who need it:
First (passing "subURLs" in your navigator map)
navigating to an URL with #"tt://photos/firstphoto" is possible, you can fetch the "firstphoto" like this:
//Prepare your Navigator Map like this
[map from:#"tt://photos/(initWithNumber:)" toViewController:[PhotoVC class]];
In your PhotoVC you can access this Number:
-(void) initWithNumber: (NSString*)number {
NSLog(#"%#",number);
}
calling your View Controller with this url would look:
PhotoVC* controller = [[PhotoVC alloc] initWithNumber:#"1"];
[navigationController pushViewController:controller animated:YES];
[controller release];
Second (passing objects in an TTTableViewController)
its a bit tricky, but you dont have to Subclass anything.
first, nil the URL in the TableItem
[TTTableLink itemWithText:#"TTTableLink" URL:nil]
in your TTTableViewController write down this method
- (void)didSelectObject:(id)object atIndexPath:(NSIndexPath*)indexPath {
TTURLAction *urlAction = [[[TTURLAction alloc] initWithURLPath:#"tt://photos"] autorelease];
urlAction.query = [NSDictionary dictionaryWithObject:#"firstphoto" forKey:#"photo"];
urlAction.animated = YES;
[[TTNavigator navigator] openURLAction:urlAction];
}
now in your your PhotoVC you need something like this
- (id)initWithNavigatorURL:(NSURL*)URL query:(NSDictionary*)query {
if (self = [super init]) {
NSLog(#"%#",query);
}
return self;
}
and you are done ;)
I was trying to implement choise's answer, learned a lot, and eventually had to get the callouts showing up and keep the implementation with many urls simple, so here's what i did.
Keep URL in the TableItem,
Use this code in the TTTableViewController subclass.
- (void)didSelectObject:(id)object atIndexPath:(NSIndexPath*)indexPath {
NSLog(#"Its url is %#", [object URL]);
TTURLAction *urlAction = [[[TTURLAction alloc] initWithURLPath:(NSString *)[object URL]] autorelease];
urlAction.query = [NSDictionary dictionaryWithObject:self.user forKey:#"user"];
urlAction.animated = YES;
[[TTNavigator navigator] openURLAction:urlAction];
}
- (BOOL)shouldOpenURL:(NSString*)URL {
return NO;
}
That "shouldOpenURL:" was discovered looking through TTTableViewController, I tried it out, and it worked. Now the table view is not opening a duplicate view, and there are callouts!
Thanks choise!
Although choice's answer works for multiple params when u are creating the TTURLAction in code it is not very useful when u want to embed links to view controllers in your TTStyledLabel.One solution to that is to use multiple params in a single string.
<a href='app://view2/param1=value1&param2=value2&...'>LabelName</a>
if you want the code to parse such urls and get the params please feel free to send me a message and I will send you my parser classes.
(or you can build your own with NSScanner!)
Also dont forget to escape the &s otherwise TTStyledLabel would not like it!
You don't need to run this on current version 1.0.6.2 for TTTableViewController. The "URL" option is working as expected. If it's not working for you, then your URL is broken or your are calling the wrong function on your ViewController. The function you have to call through URL must return an id (be a constructor for a ViewController) of a ViewController. Then it'll work as expected.
I'll changed the example form choise to be like TTNavigator expect it to be.
Add a mapping, which TTNavigator will use to navigate:
//Prepare your Navigator Map like this
[map from:#"tt://photos/(initWithNumber:)" toViewController:[PhotoVC class]];
Create a TTTableLink (or TTStyledText, or other) with an URL set, which should mach your map:
[TTTableLink itemWithText:#"TTTableLink" URL:#"tt://photos/1"]
Add this to your PhotoVC to be called by TTNavigator on the given URL
-(id) initWithNumber: (NSString*)number {
if (self = [super init]) {
self.title = #"Some Title";
NSLog(#"%#",number);
}
return self;
}
You don't need to overwrite the function didSelectObject, as the TTNavigator will call your ViewController through defined constructor function tt://photos/(initWithNumber:)

Just how to you use TTStyledTextLabel?

All I want is to display some simple text in my viewController and have the hyperlinks autoparsed. When the user clicks on the link I want the control to somehow do a callback where I can do something with the URL. How can I achieve this?
I've already looked through the TTCatalog for hours. I have also tried looking into the source code of three20 as well as looking at the stack trace. No help. I just can't figure out how my app can react to the click of the URL. Any hints please?
Hard to help without seeing what you've already tried, but you should be able to do something like the following:
TTStyledTextLabel* label = [[[TTStyledTextLabel alloc]
initWithFrame:someFrame] autorelease];
NSString* labelText = #"This should work";
label.text = [TTStyledText textFromXHTML:labelText lineBreaks:NO URLs:YES];
[someView addSubview:label];
You can then use TTNavigator and TTURLMap to map custom-uri://some/url to a particular controller in your application, or handle it yourself in your application delegate. The best place to find out how to do that is by looking at the TTNavigatorDemo sample application included in the Three20 source. Specifically, look at AppDelegate.m which is where all the URL mapping gets performed.
In addition to what Nathan says about URL mapping and links, you can also use CSS styles!
TTStyledTextLabel* label = [[[TTStyledTextLabel alloc] initWithFrame:someFrame] autorelease];
NSString* labelText = #"This should work and
<span class=\"redText\">this should be red</span>";
label.text = [TTStyledText textFromXHTML:labelText lineBreaks:NO URLs:YES];
[someView addSubview:label];
Then in your StyleSheet.m implement
- (TTStyle*) redText {
return [TTTextStyle styleWithFont:[UIFont systemFontOfSize:12] color:RGBCOLOR(255,0,0) next:nil];
}