功能
- 支持自己大表情
- 良好用户体验
- 允许删除和连续删除
- 剪贴/粘贴/复制的示例
- 文本和表情键盘
- 默认为文本提供小表情
- 带有属性的纯字符串输出
- 表情直接显示在TextView中
如何使用
安装
MYPEmotionKeyboard可以通过CocoaPods获取。要安装它,请简单地添加以下行到您的Podfile:
pod 'MYPEmotionKeyboard'
使用
初始化和配置
// first import the module
import MYPEmotionKeyboard
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// MYPEmotionKeyboardView is the default emotion keyboard
self.emotionView = MYPEmotionKeyboardView
// set delegate to your controller. MYPEmotionInputDelegate
self.emotionView?.delegate = self
// forbidden the drag interaction for emotion in text
if #available(iOS 11.0, *) {
self.textView.textDragInteraction?.isEnabled = false
}
}
代理实现
public protocol MYPEmotionInputDelegate: class {
func emotionView(_ emotionView: MYPEmotionView, didClickEmotion emotion: MYPEmotion, isDefault: Bool)
func emotionViewdidClickDelete(_ emotionView: MYPEmotionView)
}
需要大表情吗?
// Just to set 'self.emotionView.emotionSet'
您需要了解MYPEmotion
类是用于表情的。 MYPEmotionSet
是一系列表情。
表情的描述是[smile]
样式。我们使用正则表达式匹配[XX]
样式的文本。
公共函数translateAttributedTextIntoEmotionText(with font: UIFont?)
将输出带有表情附件的attributedString。
公共函数public func plainText() -> String
将输出带有表情描述的纯文本。
调用键盘
if condition {
// emotion keyboard
self.textView.inputView = self.emotionView
self.textView.reloadInputViews()
}
else {
// text keyboard
self.textView.inputView = nil
self.textView.reloadInputViews()
}
如果想要对表情进行粘贴/复制/裁剪,您需要覆盖以下方法
override func cut(_ sender: Any?) {
let string = self.attributedText.myp_plainText(in: self.selectedRange) ?? ""
if !string.isEmpty {
UIPasteboard.general.string = string
let selectedRange = self.selectedRange
let attributedContent = NSMutableAttributedString(attributedString: self.attributedText)
attributedContent.replaceCharacters(in: self.selectedRange, with: "")
self.attributedText = attributedContent
self.selectedRange = NSMakeRange(selectedRange.location, 0)
self.delegate?.textViewDidChange!(self)
}
}
您可以深入研究示例,查看所有用法。
示例
要运行示例项目,请克隆仓库,然后先从 Example 目录中运行 pod install
。
作者
wakary, [email protected]
许可证
MYPEmotionKeyboard 遵循 MIT 许可证。更多信息请参阅 LICENSE 文件。