Created
October 29, 2012 23:32
-
-
Save beccadax/3977323 to your computer and use it in GitHub Desktop.
Basic use of ANEntitySet and ANEntity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Note that I have never run this code; it may have bugs or even be syntactically invalid. | |
- (NSAttributedString*)attributedTextForPost:(ANPost*)post { | |
NSMutableAttributedString * attributedText = [[NSMutableAttributedString alloc] initWithString:post.text]; | |
for(ANEntity * entity in post.entities.all) { | |
[attributedText setAttributes:@{ | |
NSForegroundColorAttributeName: [UIColor blueColor], | |
NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle), | |
@"entity": entity | |
} range:entity.range]; | |
} | |
return attributedText; | |
} | |
// We'll assume you have a MyCustomLabel with an -attributedTextIndexAtPoint: method. | |
// UILabel has no equivalent method, so you can't use a plain UILabel to do what we need. | |
// MyCell has a .postTextLabel property of class MyCustomLabel. | |
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath { | |
MyCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Post" forRowAtIndexPath:indexPath]; | |
if(!cell) { | |
cell = [[MyCell alloc] initWithReuseIdentifier:@"Post"]; | |
UITapGestureRecognizer * postTextTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(postTextTapped:)]; | |
[cell.postTextLabel addGestureRecognizer:postTextTapRecognizer]; | |
} | |
ANPost * post = [self postAtIndexPath:indexPath]; | |
// ... fill in other fields... | |
cell.postTextLabel.attributedText = [self attributedTextForPost:post]; | |
return cell; | |
} | |
- (void)postTextTapped:(UITapGestureRecognizer*)gestureRecognizer { | |
MyCustomLabel * label = (id)gestureRecognizer.view; | |
CGPoint point = [gestureRecognizer locationInView:label]; | |
// This is the thing UILabel can't do. | |
NSUInteger index = [label attributedTextIndexAtPoint:point]; | |
ANEntity * tappedEntity = [label.attributedText attribute:@"entity" atIndex:index effectiveRange:NULL]; | |
if(!tappedEntity) { | |
// the user tapped an area that wasn't an entity | |
} | |
else { | |
// All ANEntities have a valid .URL field--AppNetKit inserts an appropriate alpha.app.net URL for | |
// mention and tag entities--so you could just launch Safari with tappedEntity.URL. But most apps | |
// will want to do something more sophisticated, like this: | |
switch(tappedEntity.entityType) { | |
case ANEntityTypeLink: | |
// The user tapped a link entity. Here we'll just launch Safari, but you could segue | |
// to your own custom web view instead. | |
[UIApplication.sharedApplication openURL:tappedEntity.URL]; | |
break; | |
case ANEntityTypeMention: | |
// The user tapped a mention. ShowUser is a segue to your user profile screen; | |
// -prepareForSegue:sender: knows to look for the username in the sender parameter. | |
[self performSegueWithIdentifier:@"ShowUser" sender:tappedEntity.name]; | |
break; | |
case ANEntityTypeTag: | |
// The user tapped a hashtag. ShowTag is a segue to your tag search screen; | |
// -prepareForSegue:sender: knows to look for the tag in the sender parameter. | |
[self performSegueWithIdentifier:@"ShowTag" sender:tappedEntity.name]; | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment