开发者

Component for mix text, links and clickable text

I have a problem. Need a component for mix text, links and clickable text.

In Facebook App can开发者_运维技巧 see a example. http://img193.imageshack.us/img193/1355/example1t.jpg

Someone know a than can do this. For example, UITextView can detect links in text, but can't insert clickable text.

Thanks for your help.


I have used CoreText for this purpose.

Calculating the tap offsets was a bit tricky, since I kept forgetting to flip the coordinate system, but I eventually got it, and here's a brief overview of how you can do it:

  1. Create your text as an NSMutableAttributedString (since you will have to modify it at some point).
  2. Scan it for the text you want to add a link to, insert two attributes: 1) A text colour that's for instance, blue; and 2) a custom link attribute (name it anything you like, make its value be the link you want to go to)
  3. Render the text in your drawRect:. You'll need to create a CTFramesetterRef and a CTFrameRef (the latter you'll want to keep around. Also note that creating a framesetter is an expensive process, do it only once (i.e., outside of drawRect:) if possible).
  4. In your touch handling code, as I mentioned earlier, you'll need to compensate for the different coordinate systems. From there, assuming you know where your text is using its coordinate system, you can figure out where on the text being rendered the tap was, which can then be intercepted in your touch handler, if the attribute over a particular CTRunRef contains your custom link attribute. If it does, simply get the attribute, and pass it to whatever -- typically this is where you'll want to use a delegate.

That's the crux of it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜