这是一个独立的 UIView 子类,根据 Twitter 的指南在 https://dev.twitter.com/terms/display-guidelines渲染推文的文本部分。
它解析以下推文实体并提供点击手势回调
这些回调传递包含您需要用于根据 Twitter 指南处理触摸的所有数据的 WMATweetEntity 子类实例。
编码的 & 也被替换成了普通的 "&" 字符。
注意:此视图仅渲染推文文本 - 您负责添加头像、作者名称、日期等。但这是简单的部分!示例
代码应在 iOS 4.0 及以上版本上运行。它甚至可能在 iOS 3.2 上工作(当时 CoreText 被公开),尽管我没有完全测试过。
它也支持在 ARC 和非 ARC Xcode 项目中使用 - 您不需要设置任何标志,一切将通过编译时宏处理(感谢 John Blanco)。
一旦将 WMATweetView.h
和 WMATweetView.m
文件添加到您的 Xcode 项目中,您需要将 CoreText.framework 的引用也添加进去。
目前,有两种初始化视图的方法
这是最简单的选项 - 假设您有一个包含推文数据的 NSDictionary
实例(例如,来自 Twitter 的 API),您可以将其直接传递给视图的 initWithTweet:
初始化器。
假设示例
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Set up window
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
// Get tweet dictionary from JSON - typically, this would come from Twitter's API rather than a resource file...
NSData *tweetData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"tweet" ofType:@"json"]];
NSDictionary *tweet = [NSJSONSerialization JSONObjectWithData:tweetData options:0 error:NULL];
// Build & add tweet view - let's emulate Tweetbot
WMATweetView *tweetView = [[WMATweetView alloc] initWithTweet:tweet frame:CGRectMake(10, 30, 300, 300)];
tweetView.backgroundColor = [UIColor colorWithRed:0.906 green:0.945 blue:0.980 alpha:1.];
tweetView.textColor = [UIColor colorWithRed:0.176 green:0.306 blue:0.431 alpha:1.];
tweetView.textFont = [UIFont systemFontOfSize:12];
tweetView.urlColor = [UIColor colorWithRed:0.153 green:0.431 blue:0.702 alpha:1.];
tweetView.hashtagColor = [UIColor colorWithRed:0.518 green:0.600 blue:0.690 alpha:1.];
tweetView.userMentionColor = [UIColor colorWithRed:0.267 green:0.349 blue:0.427 alpha:1.];
tweetView.userMentionFont = [UIFont boldSystemFontOfSize:12];
[self.window addSubview:tweetView];
// Resize view to encompass text only
[tweetView sizeToFit];
// Add tap gesture handlers for parsed tweet entities
tweetView.urlTapped = ^(WMATweetURLEntity *entity, NSUInteger numberOfTouches)
{
NSLog(@"Tapped entity: %@", entity);
};
tweetView.hashtagTapped = ^(WMATweetHashtagEntity *entity, NSUInteger numberOfTouches)
{
NSLog(@"Tapped entity: %@", entity);
};
tweetView.userMentionTapped = ^(WMATweetUserMentionEntity *entity, NSUInteger numberOfTouches)
{
NSLog(@"Tapped entity: %@", entity);
};
// Go!
[self.window makeKeyAndVisible];
return YES;
}
如果您是自虐者,您可以手动设置 .text
和 .entities
属性。您可能只想这样做,如果您没有从对 Twitter API 的调用中派生的 NSDictionary
。
有一天,我可能会做一些正则表达式解析,这样就不需要手动创建实体(尽管您会错过 Twitter API 提供的许多东西 - 原始 URL、显示 URL、@提及的全名等等等等)。
示例
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Set up window
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
// Build & add tweet view - let's emulate Tweetbot
WMATweetView *tweetView = [[WMATweetView alloc] initWithFrame:CGRectMake(10, 30, 300, 300)];
tweetView.text = @"Tweet with a link http://t.co/dQ06Fbx3, screen name @wemakeapps, #hashtag, more text, another link http://t.co/9GQa6ycA @ZarroBoogs #ios moo";
// Add entities (well, the last two anyway)
NSMutableArray *entities = [NSMutableArray array];
[entities addObject:[WMATweetUserMentionEntity entityWithScreenName:@"ZarroBoogs" name:@"Mark Beaton" idString:@"547490130" start:120 end:131]];
[entities addObject:[WMATweetHashtagEntity entityWithText:@"ios" start:132 end:136]];
tweetView.entities = entities;
tweetView.backgroundColor = [UIColor colorWithRed:0.906 green:0.945 blue:0.980 alpha:1.];
tweetView.textColor = [UIColor colorWithRed:0.176 green:0.306 blue:0.431 alpha:1.];
tweetView.textFont = [UIFont systemFontOfSize:12];
tweetView.urlColor = [UIColor colorWithRed:0.153 green:0.431 blue:0.702 alpha:1.];
tweetView.hashtagColor = [UIColor colorWithRed:0.518 green:0.600 blue:0.690 alpha:1.];
tweetView.userMentionColor = [UIColor colorWithRed:0.267 green:0.349 blue:0.427 alpha:1.];
tweetView.userMentionFont = [UIFont boldSystemFontOfSize:12];
[self.window addSubview:tweetView];
// Resize view to encompass text only
[tweetView sizeToFit];
// Add tap gesture handlers for parsed tweet entities
tweetView.urlTapped = ^(WMATweetURLEntity *entity, NSUInteger numberOfTouches)
{
NSLog(@"Tapped entity: %@", entity);
};
tweetView.hashtagTapped = ^(WMATweetHashtagEntity *entity, NSUInteger numberOfTouches)
{
NSLog(@"Tapped entity: %@", entity);
};
tweetView.userMentionTapped = ^(WMATweetUserMentionEntity *entity, NSUInteger numberOfTouches)
{
NSLog(@"Tapped entity: %@", entity);
};
// Go!
[self.window makeKeyAndVisible];
return YES;
}