iOS:如何自定义JSQMessagesCollectionview?

标签 ios swift messaging jsqmessagesviewcontroller

我想自定义 JSQMessagesCollectionView。我想在底部 View 中添加一些填充,以便 ScrollView 从某个底部边距开始。

我需要这个,因为我的观点是重叠的。这里是截图:

enter image description here

第二件事我想自定义时间戳。我想添加一个功能,就像 Facebook 一样。这是场景:

  1. 显示日期和时间。对于在一分钟内发送的消息,请使用“Just 现在”而不是实际的时间戳。
  2. 对于同一天发送的消息,使用今天时间。例如: 今天下午 4:47。
  3. 对于昨天发送的邮件,使用昨天,时间。例如: 昨天凌晨 5:01。
  4. 对于其他所有内容,请使用标准格式:日期、时间。例如: 2017 年 4 月 12 日,下午 4:01。

我想自定义时间戳标签,我想在那里添加“刚刚”文本。可能吗?

我可以更改 View 中的时间地点吗?我想在气泡下方显示时间戳?

提前致谢。如果可以请帮忙?

最佳答案

时间戳问题已在上述答案中解决。 我刚刚添加了这段代码:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = super.collectionView(collectionView, cellForItemAt: indexPath) as! JSQMessagesCollectionViewCell
        cell.textView.delegate = self
        let message = messages[indexPath.item]

       // let formatter = DateFormatter()
         // time zone if needed

        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        formatter.timeZone = NSTimeZone(abbreviation: "UTC") as TimeZone!
    let now = formatter.string(from: message.date as Date)

    let messageUTCDate = formatter.date(from: now)

    cell.cellBottomLabel.text =  formatter.timeAgoSinceDate(messageUTCDate!)
    return cell
}

我也能解决上面的问题,这里是下面给出的解决方案:

为了在 Collection View 中添加按钮,我使用了以下代码,它将被添加到 ChatViewController.swift 中:

 func addSocialPagesButtonsToView()
    {
        var scrollView : UIScrollView!

        let pageNames = NSArray(objects: "Open My Facebook Page","Open My TripAdvisor Page")
        scrollView = UIScrollView(frame:CGRect(x:0, y:10, width:Int(collectionView.frame.width+100), height:50))
        scrollView.sizeToFit()
      //  scrollView.setTranslatesAutoresizingMaskIntoConstraints(false)
        for i in 0..<2 {

           // let catogry = categoryList[i] as! CategoryModel
            defaultSocialMediaButtonsScrollContentSize = defaultSocialMediaButtonsScrollContentSize + 200
            scrollView.addSubview(self.createViewForSocialPagesButtons(name: pageNames[i] as! String, heightOfParent: Int(scrollView.frame.size.height), tag: i))
            scrollView.contentSize = CGSize(width: defaultSocialMediaButtonsScrollContentSize, height:40)

        }
        defaultSocialMediaButtonsScrollContentSize = defaultSocialMediaButtonsScrollContentSize + 40
        scrollView.contentSize = CGSize(width: defaultSocialMediaButtonsScrollContentSize, height:40)
        scrollView.showsHorizontalScrollIndicator = true
        scrollView.showsVerticalScrollIndicator = false

        socialbuttonsScrollView.addSubview(scrollView)
    }
    func createViewForSocialPagesButtons(name: String, heightOfParent: Int, tag: Int) -> UIButton {

        let button = UIButton(frame: CGRect(x:defaultXValueforView , y: 0, width: 220, height: 40))
        button.backgroundColor = UIColor.clear
        button.setTitle(name, for: .normal)
        button.tag = tag
        button.setTitleColor(UIColor.blue, for: .normal)
        button.layer.cornerRadius = 2;
        button.layer.borderWidth = 1;
        button.layer.borderColor = UIColor.blue.cgColor
        button.titleLabel?.textAlignment = NSTextAlignment.center
        let font = UIFont(name: "Rubik", size: 17)
        button.titleLabel?.font = font
        button.addTarget(self, action: #selector(openPageButtonTapped(_:)), for: .touchUpInside)

        defaultXValueforView = defaultXValueforView + 225;
        return button
    }

    func openPageButtonTapped(_ sender : UIButton){
        let buttonTapped = sender.tag
        if(Commons.connectedToNetwork())
        {
            let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
            let nextViewController = storyBoard.instantiateViewController(withIdentifier: "webVC") as! WebPageViewController
            if(buttonTapped==0)
            {
                nextViewController.pageUrlAdress = "https://www.facebook.com/TripAdvisor/"
            }
            else if(buttonTapped==1)
            {
              nextViewController.pageUrlAdress = "https://www.tripadvisor.com.ph/Restaurant_Review-g298460-d10229090-Reviews-Vikings_SM_City_Cebu_Restaurant-Cebu_City_Cebu_Island_Visayas.html"
            }

            self.navigationController?.pushViewController(nextViewController, animated:true)

        }
        else
        {
            Commons.showAlert("Please check your connection", VC: self)
        }
    }

viewdidload中调用addSocialPagesButtonsToView()方法。

要减少 Collectionview 滚动区域,您必须打开 pod 代码文件。打开JSQMessagesViewController.m文件,进入以下方法:

- (void)jsq_setCollectionViewInsetsTopValue:(CGFloat)top bottomValue:(CGFloat)bottom
{
    UIEdgeInsets insets = UIEdgeInsetsMake(top, 0.0f, bottom+60, 0.0f);
    self.collectionView.contentInset = insets;
    self.collectionView.scrollIndicatorInsets = insets;
}

只需添加常量值(你想减少多少面积)bottom+your-value。例如:bottom+60

希望对所有想做的人有所帮助。如果有人有任何疑问,请告诉我。

谢谢。

关于iOS:如何自定义JSQMessagesCollectionview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43730968/

相关文章:

ios - Xcode react-native架构x86_64的 undefined symbol

ios - 将 EFCircularSlider 的值更新为 UILabel

ios - Cocoapods-无此类模块“模块”(适用于任何模块)

java - Websphere MQ 主题 - 它们是否严格排序?

ios - 如何使用 FontAwesome 制作多行的 UIButton?

swift - 如何在 swift 中向 uicollectionview 添加复选框

ios - 在 Swift 中获取函数名称

ios - 使用工具栏启动 iPhone 相机应用程序

python - 使用 Django 将社区元素添加到网站

mysql - 消息系统查询以获取最后一条消息、未读消息的数量和对话中的用户数组