美文网首页
swift 自定义融云聊天 cell

swift 自定义融云聊天 cell

作者: 松龄学编程 | 来源:发表于2020-06-26 14:44 被阅读0次

    使用融云uilib

    private let font = UIFont(fd_fontName: .pingFangSCRegular, fd_size: 14)
    private let headAndContentSpacing: CGFloat = 8
    private let textBubblePadding: CGFloat = 13
    private let portraitPadding: CGFloat = 10
    
    class TextMessageCell: RCMessageCell {
        let bubble = UIImageView()
        let label = UILabel()
        
        override init!(frame: CGRect) {
            super.init(frame: frame)
            setupViews()
        }
        
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        override class func size(for model: RCMessageModel!, withCollectionViewWidth collectionViewWidth: CGFloat, referenceExtraHeight extraHeight: CGFloat) -> CGSize {
            guard let model = model,
                let textMessage = model.content as? RCTextMessage else { return .zero }
            
            let size = TextMessageCell.getBubbleBackgroundViewSize(message: textMessage)
            var height = size.height
            
            height = max(height, RCIM.current.globalMessagePortraitSize.height)
            height += extraHeight
            
            return CGSize(width: collectionViewWidth, height: height)
        }
        
        override func setDataModel(_ model: RCMessageModel!) {
            super.setDataModel(model)
            
            doLayout()
        }
        
        private func doLayout() {
            guard let model = model,
                let textMessage = model.content as? RCTextMessage else { return }
            
            label.text = textMessage.content
            
            let labelSize = TextMessageCell.getTextLabelSize(message: textMessage)
            let bubbleSize = TextMessageCell.getBubbleSize(textLabelSize: labelSize)
            var messageContentViewRect = messageContentView.frame
            
            if messageDirection == .MessageDirection_RECEIVE {
                label.frame = CGRect(x: textBubblePadding, y: headAndContentSpacing, width: labelSize.width, height: labelSize.height)
                messageContentViewRect.size.width = bubbleSize.width
    
                messageContentView.frame = messageContentViewRect
                
                bubble.frame = CGRect(x: 0, y: 0, width: bubbleSize.width, height: bubbleSize.height)
                bubble.image = UIImage(named: "chat_from")?.resizableImage(withCapInsets: UIEdgeInsets(top: 18, left: 18, bottom: 18, right: 18), resizingMode: .stretch)
            } else {
                label.frame = CGRect(x: textBubblePadding, y: headAndContentSpacing, width: labelSize.width, height: labelSize.height)
                
                messageContentViewRect.size.width = bubbleSize.width
                messageContentViewRect.size.height = bubbleSize.height
                messageContentViewRect.origin.x = baseContentView.bounds.width
                    - messageContentViewRect.size.width
                    - CGFloat(headAndContentSpacing)
                    - RCIM.current.globalMessagePortraitSize.width
                    - portraitPadding
                
                
                messageContentView.frame = messageContentViewRect
    
                bubble.frame = CGRect(x: 0, y: 0, width: bubbleSize.width, height: bubbleSize.height)
                bubble.image = UIImage(named: "chat_to")?.resizableImage(withCapInsets: UIEdgeInsets(top: 18, left: 18, bottom: 18, right: 18), resizingMode: .stretch)
    
            }
        }
        
        private func setupViews() {
            setupBubble()
            setupLabel()
        }
        
        private func setupBubble() {
            messageContentView.addSubview(bubble)
        }
        
        private func setupLabel() {
            messageContentView.addSubview(label)
            
            label.numberOfLines = 0
            label.font = font
            label.textColor = UIColor(hexString: "#222222")
            label.lineBreakMode = .byWordWrapping
            label.textAlignment = .left
        }
        
        class func getTextLabelSize(message: RCTextMessage) -> CGSize {
            guard !message.content.isEmpty else { return .zero }
            
            let maxWidth = UIScreen.main.bounds.width - (10 + RCIM.current.globalMessagePortraitSize.width + 10) * 2 - 5 - 35
            let textRect = (message.content as NSString)
                .boundingRect(with: CGSize(width: maxWidth, height: 8000),
                              options: [.truncatesLastVisibleLine, .usesLineFragmentOrigin, .usesFontLeading],
                              attributes: [NSAttributedString.Key.font: font],
                              context: nil)
            
            return CGSize(width: textRect.size.width + 5, height: textRect.size.height + 5)
        }
        
        class func getBubbleSize(textLabelSize: CGSize) -> CGSize {
            return CGSize(width: textLabelSize.width + textBubblePadding * 2, height: textLabelSize.height + headAndContentSpacing * 2)
        }
        
        class func getBubbleBackgroundViewSize(message: RCTextMessage) -> CGSize {
            let size = TextMessageCell.self.getTextLabelSize(message: message)
            return TextMessageCell.self.getBubbleSize(textLabelSize: size)
        }
    }
    

    相关文章

      网友评论

          本文标题:swift 自定义融云聊天 cell

          本文链接:https://www.haomeiwen.com/subject/jhcufktx.html