DPTagTextView 2.5.1

DPTagTextView 2.5.1

Datt 维护。



  • Datt1994

DPTagTextView

Platform Language: Swift 5 License Version Carthage compatible

使用 Textview 添加并检测标签/提及。

使用 CocoaPods 进行安装

CocoaPods 是 Objective-C 和 Swift 的依赖管理器。您可以使用以下命令安装它

$ gem install cocoapods

Podfile

要使用 CocoaPods 将 DPTagDetectionTextView 集成到您的 Xcode 项目中,请在您的 Podfile 中指定它

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'

target 'TargetName' do
use_frameworks!
pod 'DPTagTextView'
end

然后,运行以下命令

$ pod install

使用 Carthage 进行安装

Carthage 是一个集中式的依赖管理器,它构建您的依赖并提供二进制框架。

您可以使用以下命令使用 Homebrew 安装 Carthage

$ brew update
$ brew install carthage

要使用 Carthage 将 DPOTPView 集成到您的 Xcode 项目中,请在您的 Cartfile 中指定它

github "Datt1994/DPTagTextView"

运行 carthage 来构建框架并将框架(DPTagTextView.framework)拖放到您的 Xcode 项目中。

使用 Swift 包管理器安装

Swift 包管理器是一个用于自动分发 Swift 代码的工具,它集成到了 swift 编译器中。

要将库添加为 Xcode 项目的包依赖,请选择文件 > Swift 包 > 添加包依赖,并输入其仓库 URL https://github.com/Datt1994/DPTagTextView.git

手动添加

下载项目并将 DPTagTextView.swift 文件复制粘贴到您的项目中

如何使用

AddClass

👆将 DPTagTextView 添加到 UITextView 的自定义类中。

代码

设置

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")

代理方法

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]) {
    }
}