我通过inputAccessoryView
实现键入并拥有键盘。我的问题是,当我键入文字时,会稍有延迟,当我写“你好”时,每个字母都不会与我实际打相应字母时同步。每个字母的显示方式存在滞后。任何有关解决方案的想法都将不胜感激...下面是一些实现代码供参考...
protocol ChatInputAccessoryViewDelegate {
func didSend(for message: String)
}
class ChatInputTextView: UITextView {
fileprivate let placeholderLabel: UILabel = {
let label = UILabel()
label.text = "Enter Message"
label.textColor = UIColor.lightGray
return label
}()
func showPlaceholderLabel() {
placeholderLabel.isHidden = false
}
override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
NotificationCenter.default.addObserver(self, selector: #selector(handleTextChange), name: UITextView.textDidChangeNotification, object: nil)
addSubview(placeholderLabel)
placeholderLabel.anchor(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, paddingTop: 8, paddingLeft: 8, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
}
@objc func handleTextChange() {
placeholderLabel.isHidden = !self.text.isEmpty
}
required init?(coder aDecoder: NSCoder) {
fatalError("init coder has not been implemented")
}
}
class ChatInputAccessoryView: UIView {
var delegate: ChatInputAccessoryViewDelegate?
func clearTextView() {
chatTextView.text = nil
chatTextView.showPlaceholderLabel()
}
let chatTextView: ChatInputTextView = {
let tv = ChatInputTextView()
tv.returnKeyType = UIReturnKeyType.send
tv.font = UIFont(name: "OpenSans-Regular", size: 14)
tv.isScrollEnabled = false
return tv
}()
let sendButton: UIButton = {
let sb = UIButton(type: .custom)
sb.setImage(#imageLiteral(resourceName: "chatroom_send_icon"), for: .normal)
sb.addTarget(self, action: #selector(handleSend), for: .touchUpInside)
return sb
}()
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .white
autoresizingMask = .flexibleHeight
addSubview(sendButton)
sendButton.anchor(top: topAnchor, left: nil, bottom: nil, right: rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 12, width: 50, height: 50)
addSubview(chatTextView)
chatTextView.anchor(top: topAnchor, left: leftAnchor, bottom: safeAreaLayoutGuide.bottomAnchor, right: sendButton.leftAnchor, paddingTop: 8, paddingLeft: 8, paddingBottom: 8, paddingRight: 0, width: 0, height: 0)
setupLineSeparatorView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override var intrinsicContentSize: CGSize {
return .zero
}
fileprivate func setupLineSeparatorView() {
let lineSeparatorView = UIView()
lineSeparatorView.backgroundColor = UIColor(red: 230/255, green: 230/255, blue: 230/255, alpha: 1)
addSubview(lineSeparatorView)
lineSeparatorView.anchor(top: topAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0.5)
}
@objc func handleSend() {
guard let message = chatTextView.text else { return }
delegate?.didSend(for: message)
}
}
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if(text == "\n") {
didSend(for: textView.text)
return false
} else {
return true
}
}