Skip to content

Commit 4677599

Browse files
committed
feat: 评论支持表情显示
1 parent 220afc9 commit 4677599

File tree

5 files changed

+52
-1
lines changed

5 files changed

+52
-1
lines changed

BilibiliLive.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
4947423B2906B308005D6885 /* BLTextOnlyCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4947423A2906B308005D6885 /* BLTextOnlyCollectionViewCell.swift */; };
4444
49508E0F2943420100D26812 /* CocoaLumberjack in Frameworks */ = {isa = PBXBuildFile; productRef = 49508E0E2943420100D26812 /* CocoaLumberjack */; };
4545
49508E112943420100D26812 /* CocoaLumberjackSwift in Frameworks */ = {isa = PBXBuildFile; productRef = 49508E102943420100D26812 /* CocoaLumberjackSwift */; };
46+
495E6E102CDF92AB00689A08 /* Replys+AttritubedString.swift in Sources */ = {isa = PBXBuildFile; fileRef = 495E6E0F2CDF92AB00689A08 /* Replys+AttritubedString.swift */; };
4647
496400D32943431E0098ACA6 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 496400D22943431E0098ACA6 /* Logger.swift */; };
4748
496E5A4F2C0194720062951B /* MaskViewPugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 496E5A4E2C0194720062951B /* MaskViewPugin.swift */; };
4849
496E5A512C0194CD0062951B /* BUpnpPlugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 496E5A502C0194CD0062951B /* BUpnpPlugin.swift */; };
@@ -189,6 +190,7 @@
189190
494742102905053E005D6885 /* BLMotionCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BLMotionCollectionViewCell.swift; sourceTree = "<group>"; };
190191
49474212290509F6005D6885 /* DateFormatter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateFormatter.swift; sourceTree = "<group>"; };
191192
4947423A2906B308005D6885 /* BLTextOnlyCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BLTextOnlyCollectionViewCell.swift; sourceTree = "<group>"; };
193+
495E6E0F2CDF92AB00689A08 /* Replys+AttritubedString.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Replys+AttritubedString.swift"; sourceTree = "<group>"; };
192194
496400D22943431E0098ACA6 /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = "<group>"; };
193195
496E5A4E2C0194720062951B /* MaskViewPugin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MaskViewPugin.swift; sourceTree = "<group>"; };
194196
496E5A502C0194CD0062951B /* BUpnpPlugin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BUpnpPlugin.swift; sourceTree = "<group>"; };
@@ -415,6 +417,7 @@
415417
492AD70C2BFF33DF007221C8 /* VideoPlayerViewController.swift */,
416418
492AD70E2BFF6761007221C8 /* NewVideoPlayerViewModel.swift */,
417419
497CF2332C16EDC0006E1488 /* Plugins */,
420+
495E6E0F2CDF92AB00689A08 /* Replys+AttritubedString.swift */,
418421
);
419422
path = Video;
420423
sourceTree = "<group>";
@@ -962,6 +965,7 @@
962965
498CF2972B63AABE0009793E /* encode.c in Sources */,
963966
49389D8C28B0A84500B9DAFD /* PersonalViewController.swift in Sources */,
964967
498CF2992B63AABE0009793E /* cluster.c in Sources */,
968+
495E6E102CDF92AB00689A08 /* Replys+AttritubedString.swift in Sources */,
965969
498CF2952B63AABE0009793E /* compress_fragment_two_pass.c in Sources */,
966970
F927ED772610395300EAB8E3 /* DanmakuQueuePool.swift in Sources */,
967971
497CF2382C16EDE5006E1488 /* BVideoInfoPlugin.swift in Sources */,

BilibiliLive/Component/Video/ReplyDetailViewController.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ class ReplyDetailViewController: UIViewController {
3030

3131
setUpViews()
3232
replyLabel.setTitle(reply.content.message, for: .normal)
33+
34+
if let attr = reply.createAttributedString(displayView: replyLabel) {
35+
replyLabel.setAttributedTitle(attr, for: .normal)
36+
}
37+
3338
reply.content.pictures?.compactMap { URL(string: $0.img_src) }.forEach { url in
3439
let imageView = UIImageView()
3540
imageView.kf.setImage(with: url)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// Replys+AttritubedString.swift
3+
// BilibiliLive
4+
//
5+
// Created by yicheng on 9/11/2024.
6+
//
7+
8+
import Kingfisher
9+
import UIKit
10+
11+
extension Replys.Reply {
12+
func createAttributedString(displayView: UIView) -> NSAttributedString? {
13+
guard let emote = content.emote, !emote.isEmpty else {
14+
return nil
15+
}
16+
let attr = NSMutableAttributedString(string: content.message)
17+
for (tag, emote) in emote {
18+
guard let url = URL(string: emote.url) else { continue }
19+
let ranges = attr.string.ranges(of: tag).reversed()
20+
for range in ranges {
21+
let textAttachment = NSTextAttachment()
22+
attr.replaceCharacters(in: NSRange(range, in: attr.string), with: NSAttributedString(attachment: textAttachment))
23+
// TODO: 文本对其,添加间距
24+
KF.url(url)
25+
.resizing(referenceSize: CGSize(width: 36, height: 36))
26+
.roundCorner(radius: .point(15))
27+
.set(to: textAttachment, attributedView: displayView)
28+
}
29+
}
30+
return attr
31+
}
32+
}

BilibiliLive/Component/View/ReplyCell.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ class ReplyCell: UICollectionViewCell {
2424
]
2525
)
2626
userNameLabel.text = replay.member.uname
27-
contenLabel.text = replay.content.message
27+
if let attr = replay.createAttributedString(displayView: contenLabel) {
28+
contenLabel.attributedText = attr
29+
} else {
30+
contenLabel.text = replay.content.message
31+
}
2832
}
2933
}

BilibiliLive/Request/WebRequest.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,9 +705,15 @@ struct Replys: Codable, Hashable {
705705
let avatar: String
706706
}
707707

708+
struct Emote: Codable, Hashable {
709+
let text: String
710+
let url: String
711+
}
712+
708713
struct Content: Codable, Hashable {
709714
let message: String
710715
let pictures: [Picture]?
716+
let emote: [String: Emote]?
711717

712718
struct Picture: Codable, Hashable {
713719
let img_src: String

0 commit comments

Comments
 (0)