https://github.com/datt1994/dptagtextview
Add & detect Tag/mention and tag search for Textview.
https://github.com/datt1994/dptagtextview
mention mention-detection mention-people mentionsedittext search string-detection string-matching swift tag-detection tag-search tag-textview tags tagview textview uitextview
Last synced: 7 months ago
JSON representation
Add & detect Tag/mention and tag search for Textview.
- Host: GitHub
- URL: https://github.com/datt1994/dptagtextview
- Owner: Datt1994
- License: apache-2.0
- Created: 2018-04-05T05:05:22.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2022-08-18T14:56:47.000Z (almost 4 years ago)
- Last Synced: 2025-08-04T19:49:13.672Z (11 months ago)
- Topics: mention, mention-detection, mention-people, mentionsedittext, search, string-detection, string-matching, swift, tag-detection, tag-search, tag-textview, tags, tagview, textview, uitextview
- Language: Swift
- Homepage:
- Size: 13.3 MB
- Stars: 38
- Watchers: 4
- Forks: 13
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# DPTagTextView
[](http://cocoapods.org/pods/DPTagTextView)
[](https://developer.apple.com/swift)
[](https://github.com/Datt1994/DPTagTextView/blob/master/LICENSE)
[](http://cocoapods.org/pods/DPTagTextView)
[](https://github.com/Carthage/Carthage)
Add & detect tag/mention using Textview.

## Installation with CocoaPods
[CocoaPods](http://cocoapods.org) is a dependency manager for Objective-C & Swift. You can install it with the following command:
```bash
$ gem install cocoapods
```
#### Podfile
To integrate `DPTagTextView` into your Xcode project using CocoaPods, specify it in your `Podfile`:
```ruby
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
target 'TargetName' do
use_frameworks!
pod 'DPTagTextView'
end
```
Then, run the following command:
```bash
$ pod install
```
## Installation with Carthage
[Carthage](https://github.com/Carthage/Carthage) is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage with [Homebrew](http://brew.sh/) using the following command:
```bash
$ brew update
$ brew install carthage
```
To integrate `DPTagTextView` into your Xcode project using Carthage, specify it in your `Cartfile`:
```ogdl
github "Datt1994/DPTagTextView"
```
Run `carthage` to build the framework and drag the framework (`DPTagTextView.framework`) into your Xcode project.
## Installation with 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.
To add the library as package dependency to your Xcode project, select File > Swift Packages > Add Package Dependency and enter its repository URL `https://github.com/Datt1994/DPTagTextView.git`
## Add Manually
Download Project and copy-paste `DPTagTextView.swift` file into your project
## How to use

👆Add DPTagTextView to UITextView Custom Class.
## Code
**Set up**
```swift
tagTextView.dpTagDelegate = self // set DPTagTextViewDelegate Delegate
tagTextView.setTagDetection(true) // true :- detecte tag on tap , false :- Search Tags using mentionSymbol & hashTagSymbol.
tagTextView.mentionSymbol = "@" // Search start with this mentionSymbol.
tagTextView.hashTagSymbol = "#" // Search start with this hashTagSymbol for hashtagging.
tagTextView.allowsHashTagUsingSpace = true // Add HashTag using space
tagTextView.textViewAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black,
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 15)] // set textview defult text Attributes
tagTextView.mentionTagTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.blue,
NSAttributedString.Key.backgroundColor: UIColor.lightGray,
NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 15)] // set textview mentionTag text Attributes
tagTextView.hashTagTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red,
NSAttributedString.Key.backgroundColor: UIColor.lightGray,
NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 15)] // set textview hashTag text Attributes
//Set pre text and tags
let tag1 = DPTag(name: "Lorem Ipsum", range: NSRange(location: 41, length: 11))
let tag2 = DPTag(id: "567681647", name: "suffered", range: NSRange(location: 86, length: 9), data: ["withHashTag" : "#suffered"], isHashTag: true,customTextAttributes: [NSAttributedString.Key.foregroundColor: UIColor.green,NSAttributedString.Key.backgroundColor: UIColor.black, NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 15)])
let tag3 = DPTag(name: "humour", range: NSRange(location: 133, length: 7), isHashTag: true)
tagTextView.setText("There are many variations of passages of Lorem Ipsum available, but the majority have #suffered alteration in some form, by injected #humour, or randomised words which don't look even slightly believable.", arrTags: [tag1, tag2, tag3])
//Clear textview
tagTextView.setText(nil, arrTags: [])
//Add tag replacing serached string
//tagTextView.addTag(allText: String?, tagText: String, id: String, data: [String : Any], customTextAttributes: [NSAttributedString.Key : Any], isAppendSpace: Bool)
tagTextView.addTag(tagText: "User Name")
```
**Delegate Methods**
```swift
extension ViewController : DPTagTextViewDelegate {
func dpTagTextView(_ textView: DPTagTextView, didChangedTagSearchString strSearch: String, isHashTag: Bool) {
}
func dpTagTextView(_ textView: DPTagTextView, didInsertTag tag: DPTag) {
}
func dpTagTextView(_ textView: DPTagTextView, didRemoveTag tag: DPTag) {
}
func dpTagTextView(_ textView: DPTagTextView, didSelectTag tag: DPTag) {
}
func dpTagTextView(_ textView: DPTagTextView, didChangedTags arrTags: [DPTag]) {
}
}
```