AsYouTypeFormatter 1.1.1

AsYouTypeFormatter 1.1.1

Maintained by Philip Bui.



  • By
  • Philip Bui

As You Type Formatter

CI Status Version Carthage Compatible Platform License

As You Type Formatter. By assuming text is always in a state of correctness, this library aims to make the minimal amount of state changes; only overriding the text change process when multiple text attributes are needed or existing text needs to be changed.

  • Performant - O(newText.count + nextWord.count) worst case.
  • Customization - Provide own character prefixes and formats.
  • Suggestions - Detects when customized words has been selected, and methods to replace these words.
  • Delegate - Methods to detect when different text formats are in use, and new suggestions are required.

Requirements

  • iOS 8.0+ / tvOS 9.0+
  • Xcode 10.3+
  • Swift 4.2+

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate AsYouTypeFormatter into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'AsYouTypeFormatter'

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To integrate AsYouTypeFormatter into your Xcode project using Carthage, specify it in your Cartfile:

github "philip-bui/as-you-type-formatter"

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler. It is in early development, but AsYouTypeFormatter does support its use on supported platforms.

Once you have your Swift package set up, adding AsYouTypeFormatter as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/philip-bui/as-you-type-formatter.git", from: "1.1.0"))
]

Usage

  • Character Prefix. Default # @, words beginning with character prefixes use their assigned text attributes.

  • Delimiters. Default emojis and non-alphanumeric characters, delimiters indicate when a word has ended to use normal text attributes.

AsYouTypeFormatter overrides two UITextView methods, textView(shouldChangeTextIn:text:) and textViewDidChangeSelection(). You can delegate your UITextView or call the methods within your own delegate.

import AsYouTypeFormatter

// AppDelegate.swift - Modify global defaults.
AsYouTypeFormatter.normalAttributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16)]
AsYouTypeFormatter.tagAttributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 16)]
AsYouTypeFormatter.mentionAttributes = [NSAttributedString.Key.font: UIFont.italicSystemFont(ofSize: 16)]

// ViewController.swift - Customize own character prefixes and formats.
private lazy var typeFormatter: AsYouTypeFormatter = {
    // Implicit return.
    AsYouTypeFormatter(delegate: self, attributes: [
	"#": [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 16)],
	nil: [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16)]
    ])
}()

Design Decisions

  • Default Delimiters. The default delimiters are Emojis, and characters not in Unicode Category L* or 0-9. Delegate methods can customize this.
  • No suggestion support for UITextField. Suggestion support replies on detecting when the user selects a new word, and UITextFieldDelegate doesn't expose text selection events.
  • Link supports. Enabling selectable and clickable text is not very practical.
  • On multi-text selection, suggestions are disabled. The assumption is that text is usually selected when the user wants to copy and paste.

License

AsYouTypeFormatter is available under the MIT license. See LICENSE for details.