1 | pod init |
1 | pod 'JSQMessagesViewController' |
1 | pod install |
1 2 3 4 | import JSQMessagesViewController class ViewController: JSQMessagesViewController { var messages = [JSQMessage]() } |
1 2 | <key>NSMicrophoneUsageDescription</key> <string>Need microphone to talk to Watson</string>< |
1 2 | extension ViewController { } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | func setupUI() { self.title = "Watson Chat" self.senderId = UIDevice.current.identifierForVendor?.uuidString self.senderDisplayName = UIDevice.current.identifierForVendor?.uuidString JSQMessagesCollectionViewCell.registerMenuAction(#selector(synthesize(sender)) // Create mic button let microphoneImage = UIImage(named:"microphone")! let microphoneButton = UIButton(type: .custom) microphoneButton.setImage(microphoneImage, for: .normal) microphoneButton.imageView?.contentMode = UIViewContentMode.scaleAspectFit self.inputToolbar.contentView.leftBarButtonItem = microphoneButton // Add press and release mic button microphoneButton.addTarget(self, action:#selector(didPressMicrophoneButton), for: .touchDown) microphoneButton.addTarget(self, action:#selector(didReleaseMicrophoneButton), for: .touchUpInside) setAudioPortToSpeaker() } |
1 2 3 4 5 6 7 8 9 | func didPressMicrophoneButton(sender: UIButton) { let microphonePressedImage = UIImage(named:"microphone_pressed")! sender.setImage(microphonePressedImage, for: .normal) AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate)) // Clear the input text self.inputToolbar.contentView.textView.text = "" // speech-to-text startStreaming sttStartStreaming() } |
1 2 3 4 5 6 | func didReleaseMicrophoneButton(sender: UIButton){ let microphoneImage = UIImage(named:"microphone")! sender.setImage(microphoneImage, for: .normal) // speech-to-text stop streaming self.sttStopStreaming() } |
1 2 3 4 5 6 7 8 | override func didPressSend( _ button: UIButton!, withMessageText text: String!, senderId: String!, senderDisplayName: String!, date: Date!) { send(text) } |
1 2 3 4 | override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return self.messages.count } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = super.collectionView(collectionView, cellForItemAt: indexPath) as! JSQMessagesCollectionViewCell let message = self.messages[indexPath.item] if !message.isMediaMessage { if message.senderId == self.senderId { cell.textView.textColor = UIColor(R: 0x72, G: 0x9B, B: 0x79) } else { cell.textView.textColor = UIColor(R: 0x47, G: 0x5B, B: 0x63) } let attributes : [String:AnyObject] = [NSForegroundColorAttributeName:cell.textView.textColor!, NSUnderlineStyleAttributeName: 1 as AnyObject] cell.textView.linkTextAttributes = attributes } return cell } |
1 2 3 4 5 6 7 8 9 10 | override func collectionView(_ collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAt indexPath: IndexPath!) -> JSQMessageBubbleImageDataSource! { let data = messages[indexPath.row] switch(data.senderId) { case self.senderId: return self.outgoingBubble default: return self.incomingBubble } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | override func collectionView(_ collectionView: JSQMessagesCollectionView!, attributedTextForMessageBubbleTopLabelAt indexPath: IndexPath!) -> NSAttributedString! { if let message = firstMessage(at: indexPath) { let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.alignment = NSTextAlignment.left let attrs = [ NSParagraphStyleAttributeName: paragraphStyle, NSBaselineOffsetAttributeName: NSNumber(value: 0), NSForegroundColorAttributeName: UIColor(R: 0x1e, G: 0x90, B: 0xff) ] return NSAttributedString(string: message.senderDisplayName, attributes: attrs) } else { return nil } } |
1 2 3 4 5 6 7 8 9 | override func collectionView(_ collectionView: JSQMessagesCollectionView!, layout collectionViewLayout: JSQMessagesCollectionViewFlowLayout!, heightForMessageBubbleTopLabelAt indexPath: IndexPath!) -> CGFloat { if let _ = firstMessage(at: indexPath) { return kJSQMessagesCollectionViewCellLabelHeightDefault } else { return 0.0 } } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |