Reputation: 48
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.
Upvotes: 0
Views: 135
Reputation:
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.)
Upvotes: 2