专业java、php、iOS、C++、网页设计、平面设计、网络营销、游戏开发、前端与移动开发培训机构
表情键盘整合到微博
- 将表情项目里面的
HMEmoticonKeyboard
拖动到Library
文件夹下 在
HMComposeViewController
控制器里面 懒加载表情键盘
HMEmoticonKeyboard
/// 自定义表情键盘 private lazy var emoticonKeyboard: HMEmoticonKeyboard = { let keyboard = HMEmoticonKeyboard.emoticonKeyboard() keyboard.textView = self.textView return keyboard }()
在
HMComposeViewController
的表情键盘按钮点击方法里面设置自定义的表情键盘
func emoticon(_ button: UIButton) { print("表情 = \(button)") // 设置textView.inputView并不会立马改变键盘,在下次弹出的时候才会改变 textView.resignFirstResponder() // 如果是系统键盘就切换到自定义的键盘,如果是自定义的键盘就切换到系统键盘 textView.inputView = textView.inputView == nil ? emoticonKeyboard : nil let image: UIImage let highlightedImage: UIImage if textView.inputView == nil { image = UIImage(named: "compose_emoticonbutton_background")! highlightedImage = UIImage(named: "compose_emoticonbutton_background_highlighted")! } else { image = UIImage(named: "compose_keyboardbutton_background")! highlightedImage = UIImage(named: "compose_keyboardbutton_background_highlighted")! } // 设置按钮图片 button.setImage(image, for: .normal) button.setImage(highlightedImage, for: .highlighted) // 延迟0.25秒将键盘弹出来 DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.25) { self.textView.becomeFirstResponder() } }