Parla
正在构建文档
Parla 是一个现代且用户友好的 iOS 聊天 UI 库。它注重灵活性:您可以修改很多内容!
它非常容易使用,只需要进行少量配置,就可以开始使用!
要求
- iOS 9.0+
- Swift 5.0
- ARC
安装
CocoaPods
只需在 Podfile 中添加 ParlaKit 依赖项
pod 'ParlaKit'
use_frameworks!
然后在终端中(位于您的项目 Podfile 的同一目录下)运行 pod install
Carthage
Carthage 支持 将很快提供
用法
快速入门指南
在你想显示 Chat UI 的视图中,将自定义类设置为 ParlaView
:
不要忘记在自定义 ViewController 类中绑定 ParlaView 视图与出口。
然后在你的 ViewController 中,你需要实现至少一个 ParlaViewDatasource
类,但我强烈建议绑定 ParlaViewDelegate
以接收用户执行各种操作时的通知(例如,在按下发送按钮时、在录音语音消息时等)
class MyViewController : UIViewController, ParlaViewDataSource, ParlaViewDelegate {
// The main sender. His messages are considered as Outgoing, all the messages of other senders will be considerer as Incoming messages.
var mSender: PSender!
// The array of messages
var messages: [PMessage]!
// The core of the library: The ParlaView class
@IBOutlet var parlaView: ParlaView!
然后在你 viewDidAppear 中添加你的自定义逻辑,例如
override func viewDidLoad() {
// The avatars of the senders. If you do not want an avatar pass nil and disable avatar in the config
// class before initializing: Parla.config.avatar.isHidden = true
let domenicoAvatar = PAvatar(withImage: UIImage(named: "domenico.jpeg")!)
let chiaraAvatar = PAvatar(withImage: UIImage(named: "chiara.jpg")!)
// In this example we have 2 message senders
mainSender = Parla.outgoingSender(id: 10, name: "Domenico", avatar: domenicoAvatar)
let chiara = PIncomingSender(id: 11, name: "Chiara", avatar: chiaraAvatar)
let config = Parla.config
config.accessoryButton.preventDefault = false
config.cell.isBottomLabelHidden = false
config.avatar.isHidden = false
// This color will be used if you pass a nil avatar to a sender but do not set the isHidden property to true.
config.avatar.backgroundColor = UIColor.black
// Initialization of ParlaView class
parlaView.initialize(dataSource: self, delegate: self)
// This is a test video taken from the main bundle.
let mondello = Bundle.main.url(forResource: "mondello", withExtension: "mp4")!
// Adding some test messages
self.messages = [
Parla.newTextMessage(id: 1, sender: mainSender, text: "Hi Chiara! How are you? :)"),
Parla.newTextMessage(id: 2, sender: chiara, text: "Hi Domenico, all right! I'm sitting on a deckchiar here in the wonderful beach of Mondello, in Palermo (Italy) :)"),
Parla.newTextMessage(id: 3, sender: mainSender, text: "Waw! Tha's awesome! I can't wait to see a picture of you in this wonderful place!"),
Parla.newImageMessage(id: 4, sender: chiara, image: UIImage(named: "mondello-beach.jpg")!),
Parla.newVideoMessage(id: 5, sender: chiara, videoUrl: mondello),
Parla.newTextMessage(id: 6, sender: mainSender, text: "Amazing, i'm coming right now!"),
]
// Hide the top label every 4 times.
for i in 0 ..< messages.count {
messages[i].isTopLabelActive = (i % 4 == 0)
// messages[i].isTopLabelActive = false
}
最后实现 ParlaViewDatasource 所需的函数
func outgoingSender() -> POutgoingSender {
return mainSender
}
func messageForCell(at indexPath: IndexPath, collectionView: UICollectionView) -> PMessage {
return self.messages[indexPath.row]
}
func numberOfMessagesIn(collectionView: UICollectionView) -> Int {
return self.messages.count
}
可选,但强烈推荐,实现 ParlaViewDelegate 协议所需的函数
func didTapMessageBubble(at indexPath: IndexPath, message: PMessage, collectionView: UICollectionView) {
// You can choose if the binded action with the tap event should occur.
message.triggerSelection()
print("===>> DID TAP MESSAGE BUBBLE \(message.toString) << ===")
}
func didPressSendButton(withMessage message: PMessage, textField: UITextField, collectionView: UICollectionView) {
print("===>> DID PRESS SEND BUTTON \(message.toString) << ===")
// ** Example of possible implementation **
self.messages.append(message)
textField.text = ""
collectionView.reloadData()
collectionView.scrollToBottom(animated: true)
// **** //
}
func didPressAccessoryButton(button: UIView, collectionView: UICollectionView) {
print("===>> DID PRESS ACCESSORY BUTTON << ===")
}
贡献
欢迎贡献者!由于这是一个全新的库,我希望有人能帮助我维护项目并进行未来的功能发布。
联系方式
如果您想联系我获取任何信息,请将电子邮件发送至: [email protected]