steve228uk / MessengerKit

:speech_balloon: A UI framework for building messenger interfaces on iOS
MIT License
1.48k stars 129 forks source link

Typing indicators #18

Closed Steven4294 closed 5 years ago

Steven4294 commented 5 years ago

Any reference on how to add a typing indicator using this framework?

sjoness commented 5 years ago

@Steven4294 The following can be found in MSGMessengerViewController.swift

    // MARK: - Users Typing

    /// Sets the users that are currently typing.
    /// Can be overridden for additional control.
    ///
    /// - Parameter users: The users that are typing.
    open func setUsersTyping(_ users: [MSGUser]) {

        // TODO: add appearance proxy!!

        guard users.count > 0 else {
            collectionView.typingLabel.text = nil
            collectionView.layoutTypingLabelIfNeeded()
            return
        }

        var attributedText: NSMutableAttributedString!

        if users.count == 1 {
            attributedText = NSMutableAttributedString(string: users[0].displayName, attributes: [
                .font: UIFont.systemFont(ofSize: 14, weight: .bold),
                .foregroundColor: UIColor.darkText
            ])
        } else {
            attributedText = NSMutableAttributedString(string: "\(users.count) people", attributes: [
                .font: UIFont.systemFont(ofSize: 14, weight: .bold),
                .foregroundColor: UIColor.darkText
            ])
        }

        attributedText.append(NSAttributedString(string: users.count == 1 ? " is typing…" : " typing…", attributes: [
            .font: UIFont.systemFont(ofSize: 14, weight: .medium),
            .foregroundColor: UIColor.black
        ]))

        collectionView.typingLabel.attributedText = attributedText
        collectionView.layoutTypingLabelIfNeeded()

    }