https://github.com/philip-bui/as-you-type-formatter
Format text or behaviours as you type in constant time, given certain character prefixes such as # and @.
https://github.com/philip-bui/as-you-type-formatter
character-prefixes hashtags ios mentions suggestions text-formatting
Last synced: 6 months ago
JSON representation
Format text or behaviours as you type in constant time, given certain character prefixes such as # and @.
- Host: GitHub
- URL: https://github.com/philip-bui/as-you-type-formatter
- Owner: philip-bui
- License: mit
- Created: 2019-02-16T08:57:24.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-03-30T05:51:56.000Z (over 6 years ago)
- Last Synced: 2024-03-14T19:31:03.449Z (over 1 year ago)
- Topics: character-prefixes, hashtags, ios, mentions, suggestions, text-formatting
- Language: Swift
- Homepage:
- Size: 17.6 KB
- Stars: 3
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# As You Type Formatter
[](https://travis-ci.org/philip-bui/as-you-type-formatter)
[](http://cocoapods.org/pods/AsYouTypeFormatter)
[](https://github.com/Carthage/Carthage)
[](http://cocoapods.org/pods/AsYouTypeFormatter)
[](https://github.com/philip-bui/as-you-type-formatter/blob/master/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](https://cocoapods.org) 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`:
```ruby
pod 'AsYouTypeFormatter'
```### Carthage
[Carthage](https://github.com/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`:
```ogdl
github "philip-bui/as-you-type-formatter"
```### Swift Package Manager
The [Swift Package Manager](https://swift.org/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`.
```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.
```swift
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](https://github.com/philip-bui/as-you-type-formatter/blob/master/LICENSE) for details.