首页 > iOS 像微博中可以点击连接、#text#、@somebody是如何做的,该怎样实现?

iOS 像微博中可以点击连接、#text#、@somebody是如何做的,该怎样实现?

微博客户端中

  1. 连接
  2. #text#
  3. @somebody

是怎样实现的?


通常是嵌web页来实现


通过 CoreText 实现(推荐):

  1. 用正则解析纯文本生成 NSAttributedString
  2. 使用 CoreText 绘制到 CGContext 上;
  3. 点击时,通过 UIResponder 取到点击事件,并通过 CoreText API找到点击的具体文字,修改这部分的 NSMutableAttributedString(的颜色),并用 CoreText 重新绘制。

相关文档 http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/CoreText_Programming/Introduction/Introduction.html#//apple_ref/doc/uid/TP40005533

通过UIWebView实现:


解析可以用Twitter的开源库 Twitter Text,有各种语言的port:https://github.com/twitter/twitter-te...,显示方面可以用CoreText去做,可以自己实现或者用DTCoreText或TTTAttributedLabel之类的库


你可以做字符串解析,符合某种格式就输出为想要的形式,比如你要@某人,就分析,如果某段字符串满足“@用户名(用户id)”那么你就把这段字符串处理成超链接。


有现成的open source:TTTAttributedLabel
https://github.com/mattt/TTTAttribute...

【热门文章】
【热门文章】