文字气泡只允许2个字符,然后为接下来的2个字符创建新的换行符。这将随机发生。我没有对它看起来像这样的气泡字符做任何调整。提前感谢您的帮助
import UIKit
import JSQMessagesViewController
import MobileCoreServices
import AVKit
import Firebase
import Braintree
class messagesViewController: JSQMessagesViewController {
//braintree info
var braintreeClient: BTAPIClient?
var clientToken = String()
var formInfo = [String: AnyObject]()
// this is the id of the post id number
var previousViewMessageId:String!
)
var messages = [JSQMessage]()
//ref to retrieve message
var messageRef:FIRDatabaseReference! //
override func viewDidLoad() {
super.viewDidLoad()
//braintreeSetup()
navBar()
// tappedMyPayButton()
self.messageRef = fireBaseAPI().childRef("version_one/frontEnd/post/\(previousViewMessageId)")
let currentUser = fireBaseAPI().currentUserId()
self.senderId = currentUser
self.senderDisplayName = ""
let ref = fireBaseAPI().ref()
let messagRef = ref.child("version_one/frontEnd/post/\(previousViewMessageId)messages")
// messagRef.childByAutoId().setValue("first Message")
messagRef.observeEventType(.ChildAdded, withBlock: {snapshot in
//if let dict = snapshot.value as? String {
//}
})
observerveMessages()
}
}
extension messagesViewController {
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.messages.count
}
override func collectionView(collectionView: JSQMessagesCollectionView!, messageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageData! {
let data = self.messages[indexPath.row]
return data
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = super.collectionView(collectionView, cellForItemAtIndexPath: indexPath) as! JSQMessagesCollectionViewCell
return cell
}
override func collectionView(collectionView: JSQMessagesCollectionView!, didDeleteMessageAtIndexPath indexPath: NSIndexPath!) {
self.messages.removeAtIndex(indexPath.row)
}
override func collectionView(collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageBubbleImageDataSource! {
let bubbleFactory = JSQMessagesBubbleImageFactory()
let message = messages[indexPath.item]
if message.senderId == self.senderId {
return bubbleFactory.outgoingMessagesBubbleImageWithColor(UIColor(r: 43, g: 216, b: 225))
}else{
return bubbleFactory.incomingMessagesBubbleImageWithColor(UIColor(r: 125, g: 125, b: 125))
}
}
override func collectionView(collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageAvatarImageDataSource! {
return nil
}
}
//MARK - image
extension messagesViewController:UIImagePickerControllerDelegate,UINavigationControllerDelegate {
override func didPressSendButton(button: UIButton!, withMessageText text: String!, senderId: String!, senderDisplayName: String!, date: NSDate!) {
let newMessage = messageRef.child("messages")
let messageData = ["text":text,"senderId":senderId,"senderDisplayName":senderDisplayName, "mediaType":"TEXT"]
newMessage.childByAutoId().setValue(messageData)
self.finishSendingMessage()
}
func observerveMessages(){
let obRef = fireBaseAPI().childRef("version_one/frontEnd/post/\(previousViewMessageId)/messages")
obRef.observeEventType(.ChildAdded, withBlock: {snapshot in
//
if let dict = snapshot.value as? [String:AnyObject]{
let mediaType = dict["mediaType"] as! String
let senderId = dict["senderId"] as! String
let senderName = dict["senderDisplayName"] as! String
switch mediaType {
case "TEXT":
let text = dict["text"] as? String
self.messages.append(JSQMessage(senderId: senderId, displayName: senderName, text: text))
case "PHOTO":
let fileUrl = dict["fileUrl"] as! String
let url = NSURL(string: fileUrl)
let data = NSData(contentsOfURL: url!)
let picture = UIImage(data: data!)
let photo = JSQPhotoMediaItem(image: picture!)
self.messages.append(JSQMessage(senderId: senderId,displayName: senderName, media: photo))
if self.senderId == senderId {
photo.appliesMediaViewMaskAsOutgoing = true
}else{
photo.appliesMediaViewMaskAsOutgoing = false
}
case "VIDEO":
let fileUrl = dict["fileUrl"] as! String
let video = NSURL(string: fileUrl)
let videoItem = JSQVideoMediaItem(fileURL: video, isReadyToPlay: true)
self.messages.append(JSQMessage(senderId: senderId,displayName:senderName,media: videoItem))
if self.senderId == senderId {
videoItem.appliesMediaViewMaskAsOutgoing = true
}else{
videoItem.appliesMediaViewMaskAsOutgoing = false
}
default :
print("Unknown data")
}
self.collectionView.reloadData()
}
})
}
override func didPressAccessoryButton(sender: UIButton!) {
let sheet = UIAlertController(title: "Media Messages", message: "Please select an images", preferredStyle: .ActionSheet)
let cancel = UIAlertAction(title: "Cancel", style: .Cancel) { (alert) in
}
let photoLibrary = UIAlertAction(title: "Photo Library", style: .Default) { (alert) in
self.getMediafrom(kUTTypeImage)
}
let VideoLibrary = UIAlertAction(title: "Video Library", style: .Default) { (alert) in
self.getMediafrom(kUTTypeMovie)
}
sheet.addAction(photoLibrary)
sheet.addAction(VideoLibrary)
sheet.addAction(cancel)
self.presentViewController(sheet, animated: true, completion: nil)
// let imagePicker = UIImagePickerController()
// imagePicker.delegate = self
// self.presentViewController(imagePicker, animated: true, completion: nil)
//
}
func getMediafrom(type:CFString){
let mediaPicker = UIImagePickerController()
mediaPicker.delegate = self
mediaPicker.mediaTypes = [type as String]
self.presentViewController(mediaPicker, animated: true, completion: nil)
}
// Display video message
override func collectionView(collectionView: JSQMessagesCollectionView!, didTapMessageBubbleAtIndexPath indexPath: NSIndexPath!) {
let message = messages[indexPath.item]
if message.isMediaMessage {
if let mediaItem = message.media as? JSQVideoMediaItem{
let player = AVPlayer(URL: mediaItem.fileURL)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.presentViewController(playerViewController, animated: true, completion: nil)
}
}
}
func sendMedia(picture:UIImage?,video: NSURL?){
let filePath = "frontEnd/users/\(fireBaseAPI().currentUserId()!)/images/\(NSDate.timeIntervalSinceReferenceDate())/"
if let picture = picture{
let data = UIImageJPEGRepresentation(picture, 0.1)
let metaData = FIRStorageMetadata()
metaData.contentType = "image/jpg"
FIRStorage.storage().reference().child(filePath).putData(data!, metadata: metaData) { (metaData, error) in
if error != nil {
print(error)
return
}
let fileUrl = metaData?.downloadURLs![0].absoluteString
let newMessage = self.messageRef.child("messages")
let messageData = ["fileUrl":fileUrl,"senderId":self.senderId,"senderDisplayName":self.senderDisplayName, "mediaType":"PHOTO"]
newMessage.childByAutoId().setValue(messageData)
}
}else if let video = video{
let data = NSData(contentsOfURL: video)
let metaData = FIRStorageMetadata()
metaData.contentType = "video/mp4"
FIRStorage.storage().reference().child(filePath).putData(data!, metadata: metaData) { (metaData, error) in
if error != nil {
print(error)
return
}
let fileUrl = metaData?.downloadURLs![0].absoluteString
let newMessage = self.messageRef.child("messages")
let messageData = ["fileUrl":fileUrl,"senderId":self.senderId,"senderDisplayName":self.senderDisplayName, "mediaType":"VIDEO"]
newMessage.childByAutoId().setValue(messageData)
}
}
}
//photothe
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let picture = info[UIImagePickerControllerOriginalImage] as? UIImage{
// let photo = JSQPhotoMediaItem(image: picture)
//// messages.append(JSQMessage(senderId: senderId,displayName: senderDisplayName,media: photo))
sendMedia(picture,video:nil)
}else if let video = info[UIImagePickerControllerMediaURL] as? NSURL {
// let videoItem = JSQVideoMediaItem(fileURL: video, isReadyToPlay: true)
// messages.append(JSQMessage(senderId: senderId,displayName:senderDisplayName,media: videoItem))
sendMedia(nil, video: video)
}
self.dismissViewControllerAnimated(true, completion: nil)
collectionView.reloadData()
}
}
extension messagesViewController{
func dismissVc(){
self.dismissViewControllerAnimated(true, completion: nil)
}
//button setup
}
// button Actions