Krelborn / KILabel

A simple to use drop in replacement for UILabel for iOS 7 and above that highlights links such as URLs, twitter style usernames and hashtags and makes them touchable.
MIT License
470 stars 132 forks source link

Software rendered inline elements #15

Open benjohnbarnes opened 9 years ago

benjohnbarnes commented 9 years ago

I'm imagining being able to have an NSAttributedString where I can include an "element" that specifies a block that will be called to render and size that element. So, this would let me place arbitrary graphical elements in to a string (with the restriction that they play nicely with the line height and such?). I'm not sure what the "element" would be, but something you can stick in the string and forget about.

Example Use:

:-) For extra credit, support animation too, so I can have inline eyes that follow user taps.

Krelborn commented 9 years ago

OK so as of iOS 7 we can store custom attributes in NSAttributedString so that should be OK.

First pass would be have an attribute that specified an NSImage. The image would replace the range of text it was attached to. The surrounding text would wrap around the image (Maybe use an exclusion path). Since you can attach a block to an NSImage to draw the content this would get you close to what you want. The limitation is that the size of the image determines the layout of everything.

Second pass would probably to extend this idea so a block would be specified instead of an image. The block would be given a CGContextRef with a bounds that would correctly place the image on a line. There might be options for the bounds (e.g. Width in points, height in lines, baseline offset etc). With this implementation the size is properly calculated by the layout engine.

Krelborn commented 9 years ago

Looks like this is out-of-scope for KILabel. I've put together KITextAttachment that might do what you want. Will probably shut this issue down and continue in the new repo.

Build the project in KITextAttachmentExample and you should be able to work out how it works from the ViewController viewDidLoad code.

Let me know what you think.