提及
在 UITextView 中添加提及的一种简单方法。
演示
要求
- Swift 5.7
- Xcode 14.1
- iOS 13.0+
示例
要运行示例项目,克隆仓库,然后从示例目录运行 pod install
。
安装
Swift 包管理器
Swift 包管理器自动化了 Swift 代码的分配。要使用 Mentions 与 SPM,添加对 https://github.com/magicmon/Mentions.git 的依赖项。
CocoaPods
use_frameworks!
pod 'Mentions'
使用
var mentionTextView = MentionTextView()
view.addSubview(mentionTextView)
// initial text with mention.
mentionTextView.setMentionText("www.google.com or [magicmon]",
pattern: .mention,
prefixMention: "@")
mentionTextView.deleteType = .cancel // or .delete
// add to mention.
mentionTextView.insert(to: "anonymous", with: mentionTextView.selectedRange)
如果您想显示包含提及的文字,请按照以下方式设置。
var textLabel = MentionLabel()
view.addSubview(textLabel)
textLabel.setMentionText(mentionTextView.mentionText,
pattern: .mention,
prefixMention: "@")
// or Add the text of the mention inside special characters "[]".
textLabel.setMentionText("[Brad Pitt]")
// show the mention text.
textLabel.tapHandler = { (mention) in
let alert = UIAlertController(title: "", message: mention, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
作者
magicmon, https://magicmon.tistory.com
许可证
提及可在MIT许可证下使用。有关更多信息,请参阅LICENSE文件。