Skip to content

bluwave/SlackTextViewController

 
 

Repository files navigation

#SlackTextViewController

Build Status Coverage Status

Pod Version Carthage compatible License

A drop-in UIViewController subclass with a growing text input view and other useful messaging features. Meant to be a replacement for UITableViewController & UICollectionViewController.

Demo Gif

This library is used in Slack's iOS app. It was built to fit our needs, but is flexible enough to be reused by others wanting to build great messaging apps for iOS.

Features

Core

Additional

Compatibility

Installation

pod 'SlackTextViewController'
With Carthage:
github "slackhq/SlackTextViewController"
Manually:

There are two ways to do this:

  • Copy and drag the Source/ folder to your project.
  • or compile the project located in Builder/SlackTextViewController.xcodeproj to create a SlackTextViewController.framework package. You could also link the library into your project.

##How to use

###Subclassing SLKTextViewController is meant to be subclassed, like you would normally do with UITableViewController or UICollectionViewController or UIScrollView. This pattern is a convenient way of extending UIViewController. SlackTextViewController manages a lot behind the scenes while still providing the ability to add custom behaviours. You may override methods, and decide to call super and perform additional logic, or not to call super and override default logic.

Start by creating a new subclass of SLKTextViewController.

In the init overriding method, if you wish to use the UITableView version, call:

[super initWithTableViewStyle:UITableViewStylePlain]

or the UICollectionView version:

[super initWithCollectionViewLayout:[UICollectionViewFlowLayout new]]

or the UIScrollView version:

[super initWithScrollView:self.myStrongScrollView]

Protocols like UITableViewDelegate and UITableViewDataSource are already setup for you. You will be able to call whatever delegate and data source methods you need for customising your control.

Calling [super init] will call [super initWithTableViewStyle:UITableViewStylePlain] by default.

###Growing Text View

Growing

The text view expands automatically when a new line is required, until it reaches its maxNumberOfLinesvalue. You may change this property's value in the textView.

By default, the number of lines is set to best fit each device dimensions:

  • iPhone 4 (<=480pts): 4 lines
  • iPhone 5/6 (>=568pts): 6 lines
  • iPad (>=768pts): 8 lines

On iPhone devices, in landscape orientation, the maximum number of lines is changed to fit the available space.

###Inverted Mode

Some layouts may require to show from bottom to top and new subviews are inserted from the bottom. To enable this, you must use the inverted flag property (default is YES). This will actually invert the entire ScrollView object. Make sure to apply the same transformation to every subview. In the case of UITableView, the best place for adjusting the transformation is in its data source methods like:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:chatCellIdentifier];
    cell.transform = self.tableView.transform;
}

###Autocompletion

We use autocompletion for many things: names, channels, emoji, and more.

Autocompletion

To set up autocompletion in your app, follow these simple steps:

1. Registration

You must first register all the prefixes you'd like to support for autocompletion detection:

[self registerPrefixesForAutoCompletion:@[@"#"]];

2. Processing

Every time a new character is inserted in the text view, the nearest word to the caret will be processed and verified if it contains any of the registered prefixes.

Once the prefix has been detected, -didChangeAutoCompletionPrefix:andWord: will be called. This is the perfect place to populate your data source and show/hide the autocompletion view. So you must override it in your subclass, to be able to perform additional tasks. Default returns NO.