Calculating UILabel and UIButtons sizes to make a sentence? - iphone

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.

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.

how to make a word as hyperlink in iphone?

In my app, I have 4 values 0, 10, 20 and 30. each value contains diff url's.
I used UITextView to trigger the url when user taps it, but for value 20 I need a word should be present there instead of direct url.
Now the user will tap on that word it should connect to that url for value 20. In-short I want a word to be tapped that will connect to some url.
Any ideas???
Here is my code:
NSString *dot=[NSString stringWithFormat:#"%#",addressInfo.category];
NSString *dotstr=#"20";
if ([dot isEqualToString:dotstr] ) {
urlblue.text = #"eBrochure";
self.urlblue.dataDetectorTypes = UIDataDetectorTypeLink;
}
whenever user taps "eBrochure", my app should opens the associated url.....?
Yes,U can use UIWebView to do that.But if u just want to use Textfield,I think the link can help u.
Tappable URLs in Core Text
^-^
You can draw it using this code:
CGRect underlineFrame = CGRectMake(textLabel.frame.origin.x, textLabel.frame.origin.y+textLabel.frame.size.height, textLabel.frame.size.width, 1);
UIView *viewUnderline=[[UIView alloc] init];
viewUnderline.frame=underlineFrame;
viewUnderline.backgroundColor=[UIColor blackColor];
[self addSubview:viewUnderline];

TTStyledTextLabel

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

How to code a "numbers pad" like Call application

how can I do something like this in an iPhone app?
I need to input a number but I want something like this, not a simple UITextField.
How?
Thanks!
I Agree with Kevin. But if you decide to implement your own keyboard-like pad you may have to lose all those nice features provided by the original iOS keyboard.
You will have to create a custom UIView if you want it to look like what you sent. Basically add a set of subviews (UIButtons) for each control. Then create a delegate for the custom UIView that will notify of value changes. For example, here is some rough code to get you started:
// CustomNumbersView.m
- (void)button1DidClick:(id)sender
{
[self.delegate customNumbersView:self didSelectKeyWithValue:#"1"];
}
- (void)button2DidClick:(id)sender
{
[self.delegate customNumbersView:self didSelectKeyWithValue:#"2"];
}
// MainViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
CustomNumbersView *customNubmersView = [[CustomNumbersView alloc] initWithFrame:...];
customNumbersView.delegate = self;
}
- (void)customNumbersView:(CustomNumbersView *)customNumbersView didSelectKeyWithValue:(NSString *)value
{
self.mainTextField.text = [NSString stringWithFormat:#"%#%#", self.mainTextField.text, value];
}
As I need one in several situations in my programs, I wrote a delegate-driven KeyPad.
I've solved with new feature
UITextField.keyboardType = UIKeyboardTypeDecimalPad;

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