An easy way to add mentions in UITextView.
- Swift 5.7
- Xcode 14.1
- iOS 13.0+
To run the example project, clone the repo, and run pod install
from the Example directory first.
The Swift Package Manager automates the distribution of Swift code. To use Mentions with SPM, add a dependency to https://github.com/magicmon/Mentions.git
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)
If you want to show the text that contains the mention, set it as follows.
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
Mentions is available under the MIT license. See the LICENSE file for more info.