Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Social Text Storage

Social Text Storage

A.k.a how to customize NSTextStorage to fit your needs. See the accompanying video on Nodes channel on YouTube.

https://youtu.be/HSmbh9tM15A

Dominik Hádl

November 20, 2015
Tweet

More Decks by Dominik Hádl

Other Decks in Programming

Transcript

  1. Social Text Storage Icon was stolen from the internet without

    any consent of the original author and I have no idea what the license for it is.
  2. NSTextStorage (aka NSAttributedString) Informs about changes in string! Can be

    subclassed and changed to implement custom behaviour, by overriding just 4 methods. Can be used by UILabel & UITextView (with hacks!)
  3. NSLayoutManager The actual controller Takes all the info from text

    storage, text container (*) and does typesetting by consulting CoreText. When layout is finished, tells the view to draw (*) NSTextContainer is basically a frame with super powers
  4. Social Text Storage Custom subclass to be used in social

    network apps Highlights usernames(*), mentions, hashtags and URLs Quite lightweight, plug & play solution (at least for vanilla UITextView) (*) usernames have to be set manually as in an array of string
  5. How it works? Use of regex to process text Username

    regexes generated when usernames assigned Dependent on settings (OptionSetType), highlights elements Stores elements in an array with range and type
  6. Social Label Implements basic functionality of social text storage Adds

    user interactivity with highlighted elements Uses gesture recognizer and layout manager to figure out what you’ve pressed
  7. Social Text View Convenience subclass Makes setting up SocialTextStorage easier

    and overrides some properties to pass along the values
  8. Caveats UILabel requires subclassing to work with NSTextStorage Subclass of

    UITextView with custom NSTextStorage does NOT support loading from XIB/Storyboard Emoji’s require font size hack to display properly if text font size is small
  9. What’s missing? Documentation Small refactoring to make it even more

    generic Highlighted element color on touch on Social Label Tests?
  10. Resources objc.io - Issue 5, Getting to Know TextKit stackoverflow

    - all questions tagged with ‘TextKit’ iOS Developer Library - you know, “documentation” Ray Wenderlich - tutorials (lol, like we need them)