【问题标题】:Parse and replace @usernames and URL's within iphone app在 iphone 应用程序中解析和替换 @usernames 和 URL
【发布时间】:2011-07-11 11:09:49
【问题描述】:

我正在开发一个 Twitter 应用程序,需要一些帮助。我想用粗体蓝色文本替换@usernames 和 URL,并链接到我的应用程序中的相应视图。我能够解析推文文本,并使用@usernames 和 URL 构造一个数组,但我还没有弄清楚如何解析和替换 @usernames 和 URL 的 w/ 内联风格化文本。

基本上,我希望文本看起来与官方 Twitter 应用(又名 Tweetie)上的推文文本相似。

这是我在 google 上找到的推文示例。您会注意到@usernames 和链接是蓝色的: http://www.tipb.com/images/stories/2009/02/twitter_celebs_iphone.jpg

我正在使用 Google Toolbox for Mac 中的 GTMRegex 作为正则表达式。

这是我当前的代码:

- (void)parseTweet {
NSString *text = message.text;
NSArray *a;

a = [text gtm_allSubstringsMatchedByPattern:@"@[[:alnum:]_]+"];
for (NSString *s in a) {
    NTLNURLPair *pair = [[NTLNURLPair alloc] init];
    pair.text = [NSString stringWithFormat:@"@%@", [s substringFromIndex:1]];
    pair.screenName = s;
    [links addObject:pair];
    [pair release];
}
a = [text gtm_allSubstringsMatchedByPattern:@"http:\\/\\/[^[:space:]]+"];
for (NSString *s in a) {
    NTLNURLPair *pair = [[NTLNURLPair alloc] init];
    pair.text = s;
    pair.url = s;
    [links addObject:pair];
    [pair release];
}
a = [text gtm_allSubstringsMatchedByPattern:@"https:\\/\\/[^[:space:]]+"];
for (NSString *s in a) {
    NTLNURLPair *pair = [[NTLNURLPair alloc] init];
    pair.text = s;
    pair.url = s;
    [links addObject:pair];
    [pair release];
}

}

【问题讨论】:

  • 您当前的代码不适合您怎么办?
  • @Ether 我可以使用上面的代码提取@usernames 和链接,但我无法弄清楚如何用蓝色链接替换文本。
  • 目前在 iOS 中设置文本样式并不容易。你对 Core Text 满意吗,如果是这样的话,它是可行的,但在 3.2 之前不行Three20 的标签样式可能适合您的情况,或者您可以考虑使用网络视图。
  • 我猜大多数适用于 iPhone 的开源 Twitter 客户端已经满足了您的需求。我的想法是:NatsuLiphone github.com/takuma104/ntlniph

标签: objective-c regex ios replace


【解决方案1】:

您使用什么类型的 ViewController? 不知道我是否理解您的问题,但也许使用 UIWebView 来显示您的数组的内容会有所帮助。

然后您可以轻松地包含一些本地样式表以按照您想要的方式设置链接样式。

【讨论】:

    猜你喜欢
    • 2013-01-07
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多