Skip to content

Commit

Permalink
feat: 评论支持显示图片
Browse files Browse the repository at this point in the history
  • Loading branch information
yichengchen committed Nov 9, 2024
1 parent 821c048 commit 1cf42a0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 14 deletions.
77 changes: 63 additions & 14 deletions BilibiliLive/Component/Video/ReplyDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
// Created by Yam on 2024/6/9.
//

import Kingfisher
import UIKit

class ReplyDetailViewController: UIViewController {
private var scrollView: UIScrollView!
private var contentView: UIView!
private var titleLabel: UILabel!
private var replyLabel: UILabel!
private var replyLabel: UIButton!
private var replyCollectionView: UICollectionView!
private var imageStackView: UIStackView!

var reply: Replys.Reply
private let reply: Replys.Reply

init(reply: Replys.Reply) {
self.reply = reply
Expand All @@ -25,41 +29,85 @@ class ReplyDetailViewController: UIViewController {
super.viewDidLoad()

setUpViews()
replyLabel.text = reply.content.message
replyLabel.setTitle(reply.content.message, for: .normal)
reply.content.pictures?.compactMap { URL(string: $0.img_src) }.forEach { url in
let imageView = UIImageView()
imageView.kf.setImage(with: url)
imageView.contentMode = .scaleAspectFit
imageView.snp.makeConstraints { make in
make.height.lessThanOrEqualTo(500)
}
imageStackView.addArrangedSubview(imageView)
}
}

// MARK: - Private

private func setUpViews() {
scrollView = {
let scroll = UIScrollView()
view.addSubview(scroll)
scroll.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
return scroll
}()

contentView = {
let view = UIView()
scrollView.addSubview(view)
view.snp.makeConstraints { make in
make.edges.equalToSuperview()
make.width.equalToSuperview()
}
return view
}()

titleLabel = {
let label = UILabel()
self.view.addSubview(label)
contentView.addSubview(label)
label.font = .boldSystemFont(ofSize: 60)
label.text = "评论"

label.snp.makeConstraints { make in
make.centerX.equalToSuperview()
make.top.equalTo(view.safeAreaLayoutGuide)
make.top.equalToSuperview().offset(20)
}

return label
}()

replyLabel = {
let label = UILabel()
self.view.addSubview(label)
label.numberOfLines = 0
label.font = .preferredFont(forTextStyle: .headline)

let label = UIButton()
contentView.addSubview(label)
label.titleLabel?.numberOfLines = 0
label.titleLabel?.textAlignment = .left
label.titleLabel?.font = .preferredFont(forTextStyle: .headline)
label.contentHorizontalAlignment = .left
label.snp.makeConstraints { make in
make.top.equalTo(self.titleLabel.snp.bottom).offset(60)
make.leading.equalTo(self.view.snp.leadingMargin)
make.trailing.equalTo(self.view.snp.trailingMargin)
make.leading.equalTo(contentView.snp.leadingMargin)
make.trailing.equalTo(contentView.snp.trailingMargin)
}

return label
}()

imageStackView = {
let stackView = UIStackView()
stackView.axis = .horizontal
stackView.distribution = .fillEqually
stackView.spacing = 10
contentView.addSubview(stackView) // 改为添加到 contentView

stackView.snp.makeConstraints { make in
make.top.equalTo(self.replyLabel.snp.bottom).offset(60)
make.leading.equalTo(contentView.snp.leadingMargin)
make.trailing.equalTo(contentView.snp.trailingMargin)
}
return stackView
}()

replyCollectionView = {
let flowLayout = UICollectionViewFlowLayout()
flowLayout.itemSize = CGSize(width: 582, height: 360)
Expand All @@ -68,14 +116,15 @@ class ReplyDetailViewController: UIViewController {
flowLayout.minimumInteritemSpacing = 10

let collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
self.view.addSubview(collectionView)
contentView.addSubview(collectionView)
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(UINib(nibName: ReplyCell.identifier, bundle: nil), forCellWithReuseIdentifier: ReplyCell.identifier)

collectionView.snp.makeConstraints { make in
make.leading.trailing.equalToSuperview()
make.top.equalTo(self.replyLabel.snp.bottom).offset(60)
make.top.equalTo(self.imageStackView.snp.bottom).offset(60)
make.height.width.equalTo(360)
make.bottom.equalToSuperview()
}

Expand Down
5 changes: 5 additions & 0 deletions BilibiliLive/Request/WebRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,11 @@ struct Replys: Codable, Hashable {

struct Content: Codable, Hashable {
let message: String
let pictures: [Picture]?

struct Picture: Codable, Hashable {
let img_src: String
}
}

let member: Member
Expand Down

0 comments on commit 1cf42a0

Please sign in to comment.