Skip to content

Commit 1cf42a0

Browse files
committed
feat: 评论支持显示图片
1 parent 821c048 commit 1cf42a0

File tree

2 files changed

+68
-14
lines changed

2 files changed

+68
-14
lines changed

BilibiliLive/Component/Video/ReplyDetailViewController.swift

+63-14
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22
// Created by Yam on 2024/6/9.
33
//
44

5+
import Kingfisher
56
import UIKit
67

78
class ReplyDetailViewController: UIViewController {
9+
private var scrollView: UIScrollView!
10+
private var contentView: UIView!
811
private var titleLabel: UILabel!
9-
private var replyLabel: UILabel!
12+
private var replyLabel: UIButton!
1013
private var replyCollectionView: UICollectionView!
14+
private var imageStackView: UIStackView!
1115

12-
var reply: Replys.Reply
16+
private let reply: Replys.Reply
1317

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

2731
setUpViews()
28-
replyLabel.text = reply.content.message
32+
replyLabel.setTitle(reply.content.message, for: .normal)
33+
reply.content.pictures?.compactMap { URL(string: $0.img_src) }.forEach { url in
34+
let imageView = UIImageView()
35+
imageView.kf.setImage(with: url)
36+
imageView.contentMode = .scaleAspectFit
37+
imageView.snp.makeConstraints { make in
38+
make.height.lessThanOrEqualTo(500)
39+
}
40+
imageStackView.addArrangedSubview(imageView)
41+
}
2942
}
3043

3144
// MARK: - Private
3245

3346
private func setUpViews() {
47+
scrollView = {
48+
let scroll = UIScrollView()
49+
view.addSubview(scroll)
50+
scroll.snp.makeConstraints { make in
51+
make.edges.equalToSuperview()
52+
}
53+
return scroll
54+
}()
55+
56+
contentView = {
57+
let view = UIView()
58+
scrollView.addSubview(view)
59+
view.snp.makeConstraints { make in
60+
make.edges.equalToSuperview()
61+
make.width.equalToSuperview()
62+
}
63+
return view
64+
}()
65+
3466
titleLabel = {
3567
let label = UILabel()
36-
self.view.addSubview(label)
68+
contentView.addSubview(label)
3769
label.font = .boldSystemFont(ofSize: 60)
3870
label.text = "评论"
3971

4072
label.snp.makeConstraints { make in
4173
make.centerX.equalToSuperview()
42-
make.top.equalTo(view.safeAreaLayoutGuide)
74+
make.top.equalToSuperview().offset(20)
4375
}
4476

4577
return label
4678
}()
4779

4880
replyLabel = {
49-
let label = UILabel()
50-
self.view.addSubview(label)
51-
label.numberOfLines = 0
52-
label.font = .preferredFont(forTextStyle: .headline)
53-
81+
let label = UIButton()
82+
contentView.addSubview(label)
83+
label.titleLabel?.numberOfLines = 0
84+
label.titleLabel?.textAlignment = .left
85+
label.titleLabel?.font = .preferredFont(forTextStyle: .headline)
86+
label.contentHorizontalAlignment = .left
5487
label.snp.makeConstraints { make in
5588
make.top.equalTo(self.titleLabel.snp.bottom).offset(60)
56-
make.leading.equalTo(self.view.snp.leadingMargin)
57-
make.trailing.equalTo(self.view.snp.trailingMargin)
89+
make.leading.equalTo(contentView.snp.leadingMargin)
90+
make.trailing.equalTo(contentView.snp.trailingMargin)
5891
}
5992

6093
return label
6194
}()
6295

96+
imageStackView = {
97+
let stackView = UIStackView()
98+
stackView.axis = .horizontal
99+
stackView.distribution = .fillEqually
100+
stackView.spacing = 10
101+
contentView.addSubview(stackView) // 改为添加到 contentView
102+
103+
stackView.snp.makeConstraints { make in
104+
make.top.equalTo(self.replyLabel.snp.bottom).offset(60)
105+
make.leading.equalTo(contentView.snp.leadingMargin)
106+
make.trailing.equalTo(contentView.snp.trailingMargin)
107+
}
108+
return stackView
109+
}()
110+
63111
replyCollectionView = {
64112
let flowLayout = UICollectionViewFlowLayout()
65113
flowLayout.itemSize = CGSize(width: 582, height: 360)
@@ -68,14 +116,15 @@ class ReplyDetailViewController: UIViewController {
68116
flowLayout.minimumInteritemSpacing = 10
69117

70118
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
71-
self.view.addSubview(collectionView)
119+
contentView.addSubview(collectionView)
72120
collectionView.dataSource = self
73121
collectionView.delegate = self
74122
collectionView.register(UINib(nibName: ReplyCell.identifier, bundle: nil), forCellWithReuseIdentifier: ReplyCell.identifier)
75123

76124
collectionView.snp.makeConstraints { make in
77125
make.leading.trailing.equalToSuperview()
78-
make.top.equalTo(self.replyLabel.snp.bottom).offset(60)
126+
make.top.equalTo(self.imageStackView.snp.bottom).offset(60)
127+
make.height.width.equalTo(360)
79128
make.bottom.equalToSuperview()
80129
}
81130

BilibiliLive/Request/WebRequest.swift

+5
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,11 @@ struct Replys: Codable, Hashable {
707707

708708
struct Content: Codable, Hashable {
709709
let message: String
710+
let pictures: [Picture]?
711+
712+
struct Picture: Codable, Hashable {
713+
let img_src: String
714+
}
710715
}
711716

712717
let member: Member

0 commit comments

Comments
 (0)